Reputation: 43
I need to threshold my image without using OpenCV function.
I know this way only, but in this case I am using cv2.threshold function from OpenCV:
img = cv2.imread('filename', 0)
_, thresh = cv2.threshold(img,127,255,cv.THRESH_BINARY)
How can I code thresholding without using cv2.threshold function.
I tried this one:
def thresholdimg(img, n):
img_shape = img.shape
height = img_shape[0]
width = img_shape[1]
for row in range(width):
for column in range(height):
if img[column, row] > s:
img[column, row] = 0
else:
img[column, row] = 255
return
Where, n is equal to 127
Thanks in advance.
Upvotes: 0
Views: 3264
Reputation: 169
You can threshold like this:
thresholdIMG = image[:, :, <color channel>] > <threshold>
But if you are going to do that with a RGB image you will get weird results.
Upvotes: 0
Reputation: 53089
You can use numpy to threshold in Python without OpenCV.
image[image>127] = 255
image[image!=255] = 0
if image is a grayscale image.
Upvotes: 3