Fred's ImageMagick Scripts



    Licensing:

    Copyright © Fred Weinhaus

    My scripts are available free of charge for non-commercial (non-profit) use, ONLY.

    For use of my scripts in commercial (for-profit) environments or non-free applications, please contact me (Fred Weinhaus) for licensing arrangements. My email address is fmw at alink dot net.

    If you: 1) redistribute, 2) incorporate any of these scripts into other free applications or 3) reprogram them in another scripting language, then you must contact me for permission, especially if the result might be used in a commercial or for-profit environment.

    Usage, whether stated or not in the script, is restricted to the above licensing arrangements. It is also subject, in a subordinate manner, to the ImageMagick license, which can be found at: http://www.imagemagick.org/script/license.php

    Please read the Pointers For Use on my home page to properly install and customize my scripts.

LOCALTHRESH


Thresholds an image to binary (b/w) format using a moving window adaptive thresholding approach.

Download Script

last modified: May 23, 2019



USAGE: localthresh [-m method] [-r radius] [-b bias] [-n negate] infile outfile
USAGE: localthresh [-help]

-m .... method .......... method for computing threshold statistics;
......................... method=1 uses only the local mean;
......................... method=2 uses the local mean and standard deviation;
......................... method=3 used the local mean and mean absolute deviation;
......................... default=1
-r .... radius .......... radius of window to use; float; radius>=3; default=15
-b .... bias ............ bias parameter (in percent) for thresholding;
......................... float; bias>=0; default=20
-n .... negate .......... negate indicates whether to negate the image
......................... before and after processing; negate=yes or no;
......................... default=no

PURPOSE: To automatically thresholds an image to binary (b/w) format using a moving window adaptive thresholding approach.

DESCRIPTION: LOCALTHRESH thresholds an image to binary (b/w) format using a moving window adaptive thresholding approach. For each window placement the center pixel is compared to some measure of either mean or combination of mean and either standard deviation or mean absolute deviation within the window. If the center pixel is larger than this measure by some bias value, then the center pixel is made white; otherwise it is made black. The moving window is a circle with Gaussian profile. NOTE: the image MUST have the "objects" or foreground as white and the "non-objects" or background as black. Thus the image must either be preprocessed using the IM function -negate or have the script do that using its negate parameter. IMPORTANT: For acceptable results, the window size generally should be larger than the dimension of the "objects" to be detected in the image by the thresholding. Consequently, this method is best applied to images of text, small objects or edges.

ARGUMENTS:

-m method ... METHOD specifies what statistical measure to use to compute the threshold for each window placement. Method 1 compares the center pixel to the window mean, and if larger than the bias, the center is made white; otherwise black. Method 2 compares the center pixel to the window mean plus the bias times the window standard deviation, and if the center pixels is larger, it is made white; otherwise black. Method 3 compares the center pixel to the window mean plus the bias times the square root of window mean absolute deviation and if the center pixel is larger, it is made white; otherwise black. The default is method=1. Note that method=1 is similar to the IM function -lat (but uses a circular gaussian weighted window rather than a square uniform weighted window and more importantly does not suffer from the image shift resulting with the IM -lat function).

-r radius ... RADIUS specifies the radius for the gaussian profile window. The value may be a float, but must be greater than or equal to 3. The default=15. For acceptable results, the radius generally must be larger than the feature dimension that is to be detected by the thresholding.

-b bias ... BIAS is the bias parameter used in each of the two methods to determine the threshold. Larger bias values will have the effect of removing more "noise" from the result, but too large a value may remove parts of the foreground objects that are detected. Values for bias are expressed as (percent) floats where bias>=0. The default=20.

-n negate ... NEGATE indicates whether to negate the image before and after processing, since this technique only works when the "object" or foreground is white and the "non-object" or background is black. Values may be either yes or no. The default=no.

REFERENCES: see the following:
http://www.dfki.uni-kl.de/~shafait/papers/Shafait-efficient-binarization-SPIE08.pdf
http://www.busim.ee.boun.edu.tr/~sankur/SankurFolder/Threshold_survey.pdf

CAVEAT: No guarantee that this script will work on all platforms, nor that trapping of inconsistent parameters is complete and foolproof. Use At Your Own Risk.


EXAMPLES


Local Adaptive Thresholding Of Various Images

Pictures Were Obtained from:
parts.gif from http://www.ph.tn.tudelft.nl/Courses/FIP/noframes/fip-Segmenta.html
rice.jpg from http://www.istanbul.edu.tr/eng/ee/jeee/main/pages/issues/is62/62008.pdf
textsample.jpg from http://signal.ece.utexas.edu/seminars/dsp_seminars/01fall/211_seeger_mf.pdf
lena2g_edge1.jpg was created using the IM function -edge 1

Original Image

Arguments:
-m 1 -r 11 -b 20

Arguments:
-m 2 -r 11 -b 100

Arguments:
-m 3 -r 11 -b 45

Original Image

Arguments:
-m 1 -r 45 -b 20 -n yes

Arguments:
-m 2 -r 45 -b 100 -n yes

Arguments:
-m 3 -r 45 -b 40 -n yes

Original Image

Arguments:
-m 1 -r 25 -b 5

Arguments:
-m 2 -r 25 -b 30

Arguments:
-m 3 -r 25 -b 20

Original Image

Arguments:
-m 1 -r 25 -b 5 -n yes

Arguments:
-m 2 -r 25 -b 60 -n yes

Arguments:
-m 3 -r 25 -b 20 -n yes



See A Comparison Of Each Image Against Each Thresholding Technique



What the script does is as follows:

  • Converts the image to grayscale
  • Negates the image if the "objects" are dark
  • Computes the local mean and optionally the local standard deviation
    or local square root of the mean absolute deviation for every window position
  • As necessary to the method, compares the center pixel in the window
    to one of the mean plus bias, mean plus bias times the stardard deviatioin
    or mean plus bias times the square root of the mean absolute deviation
  • If the center pixel is larger than one of these measures, then
    make the center pixel white; otherwise make it black
  • Negates the result if the "objects" in the original image were dark

This is equivalent to the following IM commands for method 1 where the "objects" are bright.

  • convert $infile -colorspace Gray -alpha off $tmpA1
  • size=`convert xc: -format "%[fx:$radius/3]" info:`
  • convert $tmpA1 -blur 0x${size} $tmpM1
  • convert $tmpA1 $tmpM1 +swap -compose minus -composite -threshold ${bias}% $outfile