How to delete this background?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
devtra
Posts: 5
Joined: 2018-06-23T22:44:20-07:00
Authentication code: 1152

How to delete this background?

Post by devtra »

Hi,

I have the following images:
Image
source: https://imgur.com/58lyq2W
Image
source: https://imgur.com/sNdyHAn
I was unsuccessful to delete all the noisy background and let only the text. I have used this great script by Fred, textcleaner and it gave me sometimes good results sometimes no. So I thought I could delete all the colors in the image except Black and it's variants.
So what I did was

Code: Select all

convert image.jpg -fill white -fuzz 40% +opaque "#000000" removed.png 
and I start playing with the fuzz parameter. The problem was that the text are not detected as black, so I thought of converting all the variants of black and grey to only black then proceed with the removal of other colors.

I tried also to binarize the image and clean it as follows:

Code: Select all

localthresh -m 3 -r 25 -b 20 -n yes image.png output.png
textcleaner -b white -g -e normalize -f 35 -o 6 -t 50 -S 200 -T output.png binarize.png
denoise -f 2 -s "20x20+203+152" binarize.png binarize.png
But the background still appears as like so:

Image
source: https://imgur.com/HZsYI7G
Image
source: https://imgur.com/jA4R6vD

Sometimes even worse.
Is there anyway I could do that, I am still a newbie and searching on google didn't give me much.
Or if there is another procedure to follow to get better results it would be appreciated.

Thanks you.
Regards!
devtra
Posts: 5
Joined: 2018-06-23T22:44:20-07:00
Authentication code: 1152

Re: How to delete this background?

Post by devtra »

Update 1:
I found out that doing a threshold before using the textcleaner script affects the performance of the image, this is why the noise in the background appears.
So what I did is applying textcleaner directly with high threshold (-t parameter) and big filter size (-f parameter) and offset size (-o parameter).

Code: Select all

./textcleaner -b white -e normalize -f 41 -o 10 -t 90 -S 200 -T image.jpg binarize.png
Th result was outstanding with High quality images.

But I have faced another problem: now there are holes in my letters, small ones though :), but they affect badly the performance of the tesseract. So, is there any way to fill those holes with black color, like an average of nearing pixels, or a predetermined kernel: diamong, rectangle, etc?

Have a nice day.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to delete this background?

Post by fmw42 »

Please show input and output examples of your latest command with text cleaner
devtra
Posts: 5
Joined: 2018-06-23T22:44:20-07:00
Authentication code: 1152

Re: How to delete this background?

Post by devtra »

Image input:
Image
source: https://imgur.com/OLeaep7

Image output:
source: https://imgur.com/whKg0LE
Image

You see what I mean, look at the characters in the top left: N J5020
They are not clear enough. If you zoom in, you will see white holes.
Although the background was removed, it took some from the chars.

Hops that clears the problem.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to delete this background?

Post by fmw42 »

That is not an easy thing to solve. Your scan needs to be a higher resolution and not blurry. It is a bit blurry because it is in perspective and parts are out of focus. I do not know how to solve it.
devtra
Posts: 5
Joined: 2018-06-23T22:44:20-07:00
Authentication code: 1152

Re: How to delete this background?

Post by devtra »

I see. Thank you for your concern.
The problem is not with the text itself, I am more concerned with the background.
Is the only way to remove the background the one I used, isn't there any other consistent method.
I'd love to hear your opinion.
Regards!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to delete this background?

Post by fmw42 »

Background removal is very difficult except for a nearly constant color background. Any texture to the background makes it very hard if not impossible to remove adequately. Your dark background has bright spots in it that make it hard to remove since the bright spots are nearly like the color of your text background. However, you could try using canny edges and hough line detection to get the region of your license identified by the 4 corner intersections of the lines. Then make a mask of that and then use that to remove your background. See http://www.imagemagick.org/discourse-se ... =4&t=25476. But doing so will require a bit of scripting to find the intersections and draw the bounding box.
devtra
Posts: 5
Joined: 2018-06-23T22:44:20-07:00
Authentication code: 1152

Re: How to delete this background?

Post by devtra »

I can already detect all the bounding boxes that I want inside the image. I used openCV.
I can detect the outer box also. See the image below that I have generated.
Image
source: https://imgur.com/h04fU1c
Post Reply