Skip to content

Instantly share code, notes, and snippets.

@THEFASHIONGEEK
Created March 27, 2020 05:32
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/ae88616e59eb5b6663685bea8c9df990 to your computer and use it in GitHub Desktop.
Save THEFASHIONGEEK/ae88616e59eb5b6663685bea8c9df990 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/text2.png", 0);
#img = cv2.blur(img, (3, 3));
# global thresholding
ret1,th1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY)
# Otsu's thresholding
ret2,th2 = cv2.threshold(img,127,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
# Otsu's thresholding after Gaussian filtering
blur = cv2.GaussianBlur(img,(3,3),0)
ret3,th3 = cv2.threshold(blur,127,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
f = plt.figure(figsize=(15,15))
f.add_subplot(2, 2, 1).set_title('Original Image');
plt.imshow(img, cmap = "gray")
f.add_subplot(2, 2, 2).set_title('Simple Threshold');
plt.imshow(th1, cmap = "gray");
f.add_subplot(2, 2, 3).set_title("Otsu's thresholding");
plt.imshow(th2, cmap = "gray");
f.add_subplot(2, 2, 4).set_title("Otsu's thresholding after Gaussian filtering");
plt.imshow(th3, cmap = "gray");
plt.show();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment