-lat offset value unclear

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
Unas
Posts: 2
Joined: 2012-03-24T14:07:49-07:00
Authentication code: 8675308

-lat offset value unclear

Post by Unas »

In the usage reference for local adaptive thresholding the offset is followed by
a % sign, which really made me question the exact implementation.

Code: Select all

-lat widthxheight{+-}offset{%}
If the current pixel is lighter than this average plus the optional offset, then it is made white
Is the added offset a percentage of the window mean, or a percentage of the
maximum intensity, or is the offset an absolute intensity value?

I tried to trace the -lat implementation to a function, and I have only found the
following AdaptiveThresholdImage() function. In this function the offset or bias
is used as an absolute intensity value, and not a percentage:

Code: Select all

mean=(MagickRealType) (pixel/number_pixels+bias);
[/url]

I believe that the -lat documentation should clarify the following, if I am correct
that the offset is an absolute value:
If the current pixel is lighter than this average plus the optional offset, then it is made white, otherwise it is made black. The offset is an absolute intensity value and not a percentage.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: -lat offset value unclear

Post by fmw42 »

The percent is optional. Without it, it means that the threshold will be set to the absolute graylevel relative to the local mean for the area around that pixel. So that threshold=mean+graylevel. Where absolute graylevel is in the range for your Qlevel for your IM compile. Q16 is the default, so that the range is 0 to 65535. With the percentage, it means that the threshold will be set to the graylevel in the range 0-100% (0=black, 100=white) relative to the local mean. Thus the graylevel% value is then converted to absolute graylevel for your Qlevel. It is not a percent in terms of pixel count.

If you are looking to use -lat for cleaning the background of a text document for later OCR, then if on unix or windows with cygwin, see my script, textcleaner. It uses -lat plus other functions to clean the background of a text document. see link below to get to the script
Unas
Posts: 2
Joined: 2012-03-24T14:07:49-07:00
Authentication code: 8675308

Re: -lat offset value unclear

Post by Unas »

Thank you for the prompt reply.

Does -lat use the implicit Qlevel, i.e. the Qlevel of the
input image, to calculate the given percentage of the
maximum intensity? (I ask because of your mentioning
of a 'default' Qlevel)

If possible, could you point to the source code where
the bias is actually calculated before it is given to
AdaptiveThresholdImage() as an input parameter?

So just to clarify please confirm whether the following
statements are correct:

Code: Select all

convert 8bit.png -lat 50x50-30 new8bit.png
This will threshold each pixel at each window mean intensity (avg)
minus 30 absolute intensity levels: threshold=avg-30

Code: Select all

convert 8bit.png -lat 50x50-30% new8bit.png
This will threshold each pixel at each window mean intensity (avg)
minus 30% of the maximum intensity for the implicit Qlevel, which
in this case for Q8 is 256, to get: threshold=avg-0.3*256
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: -lat offset value unclear

Post by fmw42 »

As I understand it, your example commands and comments are correct.

If it was Q16, your 30 would be 30 out of 65535 and your 30% would be 0.3*65535
Post Reply