"convert" command help - image to b&w text

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
superdx
Posts: 2
Joined: 2012-05-07T01:28:35-07:00
Authentication code: 13

"convert" command help - image to b&w text

Post by superdx »

Hello,

I want to use the "convert" command to produce clear black & white images which I can then use to train OCR software.

The images contain numbers in the format NN.NN (i.e. 12.19 or 1.4 or 2.39 or 78.5), and they are taken from a robotic camera. I am looking for black & white output which shows the digits clearly. Here's a sample:
Image

I tried to C&P some code I found for captchas:

Code: Select all

convert test.png -colorspace Gray -gaussian-blur 9.0 -level 0,30%,0,1 -paint 1 -depth 1 -quality 100 -shave 40x20 test+_c.png
And after some fiddling, I get this:
Image

Notice that the digit 9 is now missing a chunk up top. I'd appreciate any help as I'm almost completely oblivious to image filters and their settings!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: "convert" command help - image to b&w text

Post by fmw42 »

try this

convert to HSB, separate the brightness channel (probably similar to -colorspace gray), apply median filter 5x5, use -lat with a large filter size (larger than the width of the letters) and play with the threshold a little, then use -morphology close to remove small noise remaining. Adjust the disk size as desired.

convert number1.png -colorspace HSB -channel blue -separate +channel -median 5x5 -lat 50x50-2% -morphology close disk:1.5 number1_result.png

Image

see
http://www.imagemagick.org/script/comma ... ns.php#lat
http://www.imagemagick.org/Usage/morphology/
superdx
Posts: 2
Joined: 2012-05-07T01:28:35-07:00
Authentication code: 13

Re: "convert" command help - image to b&w text

Post by superdx »

Wow thank you so very much. This worked perfectly. Truly appreciate the help.
Post Reply