Reputation: 475
I am using opencv to do image processing on image.
I would like to transform my image in black and white only, but there is some gray color (noise) that I would like to remove
Here is my image:
I would like to have an Image in white and black only to get clearly the text:
" PARTICIPATION -3.93 C Redevance Patronale -1.92 C "
I have tried to change the threshold of the image with OpenCV but without success
#grayscale
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
#binary
ret,thresh = cv2.threshold(gray,175,255,cv2.THRESH_BINARY_INV)
Upvotes: 2
Views: 2687
Reputation: 2181
I think you mean to remove the noise of the image. For this you can choose a lower threshold value. I choose 64 using ret,thresh = cv2.threshold(img,64,255,cv2.THRESH_BINARY)
and I got this result:
But this is not that clear and letters are very thin so we use cv2.erode
. This gives:
and now we perform cv2.bitwise_or
between original image and eroded image to obtain noise free image.
The full code used is
img = cv2.imread('grayed.png', 0)
ret,thresh = cv2.threshold(img,64,255,cv2.THRESH_BINARY)
kernel = np.ones((5, 5), np.uint8)
erode = cv2.erode(thresh, kernel, iterations = 1)
result = cv2.bitwise_or(img, erode)
Upvotes: 5
Reputation: 1415
Your colour conversion converts to an RGB image with grey colour (according to GIMP it is still an RGB image). The opencv documentation says that the image must be grey scale not colour. Even though your image is grey it is still a colour image. Not sure that a color image with the GRAY colourspace is the same as a gray scale image.
This is really a duplicate of :-
Converting an OpenCV Image to Black and White
Upvotes: 0