Last active
August 12, 2020 06:16
-
-
Save palaashatri/9d7d9d71bca05130f07598b6767af155 to your computer and use it in GitHub Desktop.
Slow Binary Thresholding (Binary Segmentation) of an image using OpenCV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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