Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV - Remove text from image

How do I remove text and markings from the below medical ultrasound image?

enter image description here

like image 955
sudha subramaniam Avatar asked Dec 03 '18 10:12

sudha subramaniam


People also ask

How do you remove text from an image in OpenCV?

When applying an inpainting algorithm using OpenCV we need to provide two images: The input image with the text we want to remove. The mask image, which shows where in the image the text that we want to remove is. This second image should have the same dimensions as the input.

How do I remove text from an image without removing the background in Python?

Click 'Shift+Backspace,' and it will open the Fill window. Now choose the option of 'content-aware and click Ok. Doing this will remove the text, and all you've to do is click 'Ctrl+D. ' You're good to go.


1 Answers

Thresholding to make a mask of the whiter areas and then inpainting will work for most cases in this image.

img = cv2.imread('ultrasound.png')
mask = cv2.threshold(img, 210, 255, cv2.THRESH_BINARY)[1][:,:,0]
dst = cv2.inpaint(img, mask, 7, cv2.INPAINT_NS)

Here's the mask:

enter image description here

Here's the inpainted image:

enter image description here

Notice the thresholding mask is not exact, and includes lighter regions where there are no letters. But more importantly, there is especially an issue if the mask does not include regions that need to be removed, such as the dark shadows of the crosses in the middle. Here's a zoom-in of that region.

enter image description here

The mask is just of the white region, and doesn't cover the dark areas. For problems like this where thresholding will not be enough, the mask can be adjusted manually. Here I take the original crosses in the mask and shift to also cover the shadows, and the inpainting is much better. (Similarly, if needed, the areas that shouldn't be included in the mask can be manually removed)

crosses = mask[235:267,290:320] | mask[233:265,288:318]
mask[235:267,290:318] = crosses
dst = cv2.inpaint(img, mask, 7, cv2.INPAINT_NS)

enter image description here

enter image description here

like image 200
A Kruger Avatar answered Oct 19 '22 18:10

A Kruger