Ask Your Question
0

Doing an adaptive thresholding to a grayscale image

asked 2016-12-25 04:27:09 -0500

Azy gravatar image

I have a png image which is in grayscale 'test.png'. I need to do an adaptive thresholding to this image. I am using opencv2.

image = cv2.imread('test_big.png')
im = cv2.adaptiveThreshold(image, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2)

I am not able to do thresholding since the image is not in grayscale. So I tried to read the image as grayscale

image = cv2.imread('test_big.png',1)

Now I can do the thresholding but the output will be a blue and red image instead of black and white. Can anyone help Plsss

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2016-12-25 04:43:15 -0500

berak gravatar image

please use:

image = cv2.imread('test_big.png', cv2.IMREAD_GRAYSCALE)

(and both cv2.imread('test_big.png') and cv2.imread('test_big.png', 1) do the same thing - load an image in color)

edit flag offensive delete link more

Comments

I am getting a blue illuminated image when I use this. The problem is the source image is already in grayscale (black & white). So Is there any hack to solve this.

Azy gravatar imageAzy ( 2016-12-25 05:28:28 -0500 )edit

the source image is already in grayscale

even if so, if you use '1' for the flag (or none at all), imread will force it into a 3 channel image.

berak gravatar imageberak ( 2016-12-25 05:39:18 -0500 )edit

Ok thanks! Is there any way to solve my problem? pls

Azy gravatar imageAzy ( 2016-12-25 05:42:04 -0500 )edit

cv2.IMREAD_GRAYSCALE = 0 just use berak solution

image = cv2.imread('test_big.png', cv2.IMREAD_GRAYSCALE)

or

image = cv2.imread('test_big.png', 0)

essamzaky gravatar imageessamzaky ( 2016-12-25 07:16:40 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2016-12-25 04:27:09 -0500

Seen: 427 times

Last updated: Dec 25 '16