Fred's ImageMagick Scripts


    Copyright © Fred Weinhaus

    My scripts are available free of charge for non-commercial 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:


Automatically thresholds an image to binary (b/w) format using the k-means technique.

Download Script

last modified: September 26, 2017

USAGE: kmeansthresh [-g graph] infile outfile
USAGE: kmeansthresh [-help]

-g ..... graph .......... graph specifies whether to generate a
......................... histogram graph image displaying the
......................... location and value of the threshold;
......................... choices are: view or save;
......................... default is no graph

PURPOSE: To automatically thresholds an image to binary (b/w) format using the k-means technique.

DESCRIPTION: KMEANSTHRESH automatically thresholds an image to binary (b/w) format. It assume the histogram is bimodal, i.e. is the composite of two bell-shaped distributions representing the foreground and background classes. The k-means appoach iteratively thresholds the image, computes the means of the foreground (above threshold data) and background (at and below threshold value), computes a new threshold equal to the average of these two means and repeats until there is no change in threshold between successive iterations. This script is equivalent to the isodatathresh script, which is implemented using image masking rather than from the histogram of the image. The isodatathresh script is faster than the kmeansthresh script for smaller images. The timing transition occurs for images somewhere between 1000-2000 pixels on a side. The kmeansthresh script is moderatly insensitive to image size, but pays an initial timing penalty in order to compute the histogram related data and get the means for each class from the histogram using shell computations.


-g graph ... GRAPH specifies whether to generate a graph (image) of the histogram, displaying the location and value of the threshold. The choices are: view, save and none. If graph=view is selected, the graph will be created and displayed automatically, but not saved. If graph=save is selected, then the graph will be created and saved to a file using the infile name, with "_histog_kmeans.gif" appended, but the graph will not be displayed automatically. If -g option is not specified, then no graph will be created.

NOTE: It is highly recommended that the output not be specified as a JPG image as that will cause compression and potentially a non-binary (i.e. a graylevel) result. GIF is the recommended output format.

REFERENCES: see the following:

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.


K-Means Thresholding Of Various Images

Pictures Were Obtained from:
blood.jpg from
fingerprint.jpg from
flower.jpg from
house.jpg from's_method
kanji.jpg from
parts.gif from
rice.jpg from
tank.jpg from
textsample.jpg from
lena2g_edge1.jpg was created using the IM function -edge 1

Original Image

Thresholded Image


See A Comparison Of Each Image Against Each Thresholding Technique

What the script does is as follows:

  • Converts the image to grayscale
  • Generates the histogram
  • Computes the normalized histogram counts by dividing each bin count
    by the total pixels in the image
  • Generates cumulative arrays for each bin from the normalized count
    and from the product of normalized counts times bin graylevel
    starting from each end of the histogram. These are called histogram
    moments zero and one
  • Computes the cumulative histogram mean arrays from these moment arrays
  • Starts with the initial threshold equal to the average of the
    min and max image graylevels
  • From the two cumulative mean arrays, finds the mean above and below the threshold bin
  • Computes a new threshold from the average of these two means
  • Repeats until the threshold does not change between successive iterations
  • Thresholds the image at the converged threshold value