2

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:

click here

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)
Vardan Agarwal
  • 2,023
  • 2
  • 15
  • 27
hugo
  • 441
  • 5
  • 25

2 Answers2

5

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:

thresholded image

But this is not that clear and letters are very thin so we use cv2.erode. This gives:

eroded image

and now we perform cv2.bitwise_or between original image and eroded image to obtain noise free image.

result

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)
Vardan Agarwal
  • 2,023
  • 2
  • 15
  • 27
  • This a great example, but I'm working in C++. Playing around with parameters I was able to get good results with smaller threshold #'s using larger kernel size. Thx for the submit. – zipzit May 20 '19 at 07:34
0

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

Jon Guiton
  • 1,360
  • 1
  • 9
  • 11
  • 1
    True. I've updated my answer to reflect my current understanding. I have found this to be problematic in opencv previously. I will update when I have checked through some of my opencv stuff. – Jon Guiton May 10 '19 at 14:23