Thresholding without using OpenCV

3k views Asked by At

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.

2

There are 2 answers

0
fmw42 On

You can use numpy to threshold in Python without OpenCV.

image[image>127] = 255
image[image!=255] = 0

if image is a grayscale image.

0
Sanlyn On

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.