Skip to content

Instantly share code, notes, and snippets.

@palaashatri
Last active August 12, 2020 06:16
Show Gist options
  • Save palaashatri/9d7d9d71bca05130f07598b6767af155 to your computer and use it in GitHub Desktop.
Save palaashatri/9d7d9d71bca05130f07598b6767af155 to your computer and use it in GitHub Desktop.
Slow Binary Thresholding (Binary Segmentation) of an image using OpenCV
import numpy as np
import cv2
bw = cv2.imread("image.pmg",0) # read as BmW
height,width = bw.shape[0:2]
cv2.imshow("Original BW",bw)
binary = np.zeros([height,width,1],'uint8')
thresh = 85
for row in range (0,height):
for col in range(0,width):
if bw[row][col]>thresh:
binary[row][col]=255
cv2.imshow("Slow Binary",binary)
# Faster Method
ret,thresh = cv2.threshold(bw,thresh,255,cv2.THRESH_BINARY)
cv2.imshow("CV Threshold",thresh)
cv2.waitKey(0)
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment