Skip to content

Instantly share code, notes, and snippets.

@THEFASHIONGEEK
Created March 26, 2020 08:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save THEFASHIONGEEK/03d9e5da8ececcbf5a19a674c0142777 to your computer and use it in GitHub Desktop.
Save THEFASHIONGEEK/03d9e5da8ececcbf5a19a674c0142777 to your computer and use it in GitHub Desktop.
import numpy as np
import cv2
from matplotlib import pyplot as plt
img = cv2.imread("imgs/chapter5/sudoku.png", 0);
img = cv2.blur(img, (3, 3));
# threshold -> 127
# maxval -> 255
# Output:
# 0-127 -> 0
# 127-255 -> 255
ret,thresh1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY)
# threshold -> 127
# maxval -> 255
# Output:
# 0-127 -> 255
# 127-255 -> 0
ret,thresh2 = cv2.threshold(img,127,255,cv2.THRESH_BINARY_INV)
# threshold -> 127
# Output:
# 0-127 -> same
# 127-255 -> 127
ret,thresh3 = cv2.threshold(img,127,255,cv2.THRESH_TRUNC)
# threshold -> 127
# Output:
# 0-127 -> 0
# 127-255 -> same
ret,thresh4 = cv2.threshold(img,127,255,cv2.THRESH_TOZERO)
# threshold -> 127
# Output:
# 0-127 -> same
# 127-255 -> 0
ret,thresh5 = cv2.threshold(img,127,255,cv2.THRESH_TOZERO_INV)
f = plt.figure(figsize=(15,25))
f.add_subplot(3, 2, 1).set_title('Original Image');
plt.imshow(img, cmap = "gray")
f.add_subplot(3, 2, 2).set_title('THRESH_BINARY');
plt.imshow(thresh1, cmap = "gray");
f.add_subplot(3, 2, 3).set_title('THRESH_BINARY_INV');
plt.imshow(thresh2, cmap = "gray");
f.add_subplot(3, 2, 4).set_title('THRESH_TRUNC');
plt.imshow(thresh3, cmap = "gray");
f.add_subplot(3, 2, 5).set_title('THRESH_TOZERO');
plt.imshow(thresh4, cmap = "gray");
f.add_subplot(3, 2, 6).set_title('THRESH_TOZERO_INV');
plt.imshow(thresh5, cmap = "gray");
plt.show();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment