Contrast gradient

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
djd
Posts: 41
Joined: 2011-11-12T22:20:18-07:00
Authentication code: 8675308

Contrast gradient

Post by djd »

I photographed a notice at an awkward angle (this is actually a representative test case).
Using the perspective operator with convert, the image aligned OK.
The image remains darker to the right (as expected).
I want to OCR the image for which reduction to two colours is best.
However the right side remains too dark to OCR at all.
I have tried various ways of equalising the contrast without success.
Thus I would appreciate any suggestions for handling this (representative) problem.
The image is newhealth.gif at: http://www.discway.com.au/cmad/
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Contrast gradient

Post by snibgo »

First point: you have used GIF as an intermediate format. This has reduced the number of colours from the thousands or millions produced by your camera to 256. I suggest you use a different format.

To remove the gray background, here's a Windows bat script. I have kept it simple so it is easily translated to other languages. I find the colours at each corner, and create mask.png with those colours at the corners, with a bilinear transition. Finally, "compose divide" removes this from the original. A little more processing could make the letters black.

Code: Select all

%IM%identify newhealth.gif

rem Image is 1800x1176

%IM%convert ^
  newhealth.gif ^
  -format "%%[pixel:u.p{0,0}]\n" -write info: ^
  -format "%%[pixel:u.p{0,1175}]\n" -write info: ^
  -format "%%[pixel:u.p{1799,0}]\n" -write info: ^
  -format "%%[pixel:u.p{1799,1175}]\n" -write info: ^
  NULL:

%IM%convert -size 1800x1176 ^
  xc: ^
  -sparse-color Bilinear "0,0 srgb(189,189,195) 0,1175 srgb(164,164,164) 1799,0 srgb(117,117,114) 1799,1175 srgb(103,107,90)" ^
  mask.png

%IM%convert ^
  mask.png ^
  newhealth.gif ^
  -compose Divide -composite ^
  nh.png
snibgo's IM pages: im.snibgo.com
djd
Posts: 41
Joined: 2011-11-12T22:20:18-07:00
Authentication code: 8675308

Re: Contrast gradient - solution

Post by djd »

Thanks snibgo, most helpful.
Although I have used IM off and on for years, sometimes with quite complex problems, there are still many options I have never seen.
Even after searching for the %[pixel:...] option just now, all I could find was %[pixel: expression] but had no luck finding a list of valid expressions for %[pixel:...].

Using your example, I had to move the first coordinates to about halfway across the image in order to result in sufficiently equalised illumination. Then, using a GUI, I increased contrast a little, then reduced to two colours. The result OCR'd well with only two errors, mistaking an f for a t. I know IM can do the contrast and colour reduction steps as well, but I would need to spend some time working out how that is done.
Do you have a ready answer for global contrast adjustment and colour reduction?

Another way?
I noticed that each of the rgb values of the text were less than a certain maximum (around 80) and the rgb values of the remainder of the image were greater than a certain minimum (around 120). So I was wondering if there was a way to change the smaller pixel value set to 0 and the larger to 255. I feel confident that IM can do it, but have been unable to discover how?
Do you have an answer to that?

Thanks again for your script.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Contrast gradient

Post by fmw42 »

%[pixel: expression]
As far as I know, the only expression for pixel: is p{x,y} so as to get the color at some coordinate.

see
http://www.imagemagick.org/Usage/transform/#fx_escapes
http://www.imagemagick.org/script/fx.php (accessing pixels)
Do you have a ready answer for global contrast adjustment and colour reduction?
Contrast operators are: -brightness-contrast, -contrast, -sigmoidal-contrast
Colorreduction operators are primarily: -modulate, -colors

see
http://www.imagemagick.org/Usage/color_mods/
http://www.imagemagick.org/Usage/quantize/


Another option is to convert to gray and remove the background using -colorspace gray -lat ...

If on unix (or Windows w/Cygwin), try my script textcleaner

Here is your image processed with my script.

textcleaner -g -f 25 -o 10 newhealth.gif newhealth_f25_o10.gif

http://www.fmwconcepts.com/misc_tests/t ... 25_o10.gif

You can threshold to make the text binary afterwards.

Here is the -lat example

convert newhealth.gif -colorspace gray -negate -lat 25x25+10% -negate newhealth_lat_25x25+10.gif

http://www.fmwconcepts.com/misc_tests/t ... x25+10.gif
Last edited by fmw42 on 2013-11-08T23:06:33-07:00, edited 1 time in total.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Contrast gradient

Post by snibgo »

As far as I know, "pixel:" can use any expression that "fx:" can use, but it always returns a colour. For expression syntax, see http://www.imagemagick.org/script/fx.php

For example:
D:\web\im>%IM%convert xc: xc:black -append -format "%[pixel:mean]" info:
srgb(50%,50%,50%)

D:\web\im>%IM%convert xc: xc:black -append -format "%[fx:mean]" info:
0.5

D:\web\im>%IM%convert xc: xc:black -append -format "%[pixel:3/4]" info:
srgb(75%,75%,75%)

D:\web\im>%IM%convert xc: xc:black -append -format "%[fx:3/4]" info:
0.75
"-level {level1},{level2}" will change contrast/brightness so pixels that were {level1} become black and pixels that were {level2} become white. I generally use percentages as these are independent of the image's bit-depth. Values <0 or >100% can be used. See http://www.imagemagick.org/script/comma ... .php#level

Colour reduction should usually be the final step, unless you want a special effect. See "-posterize" and "-colors". For two colours (black and white), you could use "-threshold". But you may find the OCR works better without it.
snibgo's IM pages: im.snibgo.com
djd
Posts: 41
Joined: 2011-11-12T22:20:18-07:00
Authentication code: 8675308

Re: Contrast gradient - solutions

Post by djd »

Thanks again snibgo and Fred.

Both suggestions work quite well. Fred's text tool, being a complete script is naturally, easy to use `out of the box'.

Regards
Post Reply