Skip to content

Instantly share code, notes, and snippets.

@Yuktha-Majella
Created October 29, 2021 11:13
Show Gist options
  • Save Yuktha-Majella/02ea4a4d72008f26963798e77f2b0a74 to your computer and use it in GitHub Desktop.
Save Yuktha-Majella/02ea4a4d72008f26963798e77f2b0a74 to your computer and use it in GitHub Desktop.
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('C:\\Users\\Admin\\Downloads\\thresholding\\landscape.jpg',0)
#Global thresholding
ret1,thresh1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY)
#Otsu's thresholding
ret2,thresh2 = cv2.threshold(img,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
print("Global Threshold:", ret1)
print("Otsu's Threshold:", ret2)
# plot histogram of image
plt.hist(img.ravel(),256)
plt.show()
vis1 = np.concatenate((thresh1, thresh2), axis=1)
cv2.imshow("ORIGINAL", img)
cv2.imshow("THRESH_BINARY vs THRESH_OTSU", vis1)
cv2.waitKey(0)
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment