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.

RANGETHRESH


Converts an image to binary using range values for each channel of the input.

Download Script

last modified: August 16, 2019



USAGE: rangethresh [-r redrange] [-g greenrange] [-b bluerange] [-u units] [-c colorspace] infile outfile
USAGE: rangethresh [-h or -help]

-r ... redrange ..... range values for the red channel expressed as a comma separated
..................... pair of numbers "low,high"; can be expressed in range 0 to 255
..................... or 0 to 100 depending upon the units argument; 0<=float<=255;
..................... default="0,255"; (no spaces)
-g ... greenrange ... range values for the green channel expressed as a comma separated
..................... pair of numbers "low,high"; can be expressed in range 0 to 255
..................... or 0 to 100 depending upon the units argument; 0<=float<=255;
..................... default="0,255" (no spaces)
-b ... bluerange .... range values for the blue channel expressed as a comma separated
..................... pair of numbers "low,high"; can be expressed in range 0 to 255
..................... or 0 to 100 depending upon the units argument; 0<=float<=255;
..................... default="0,255" (no spaces)
-u ... units units for the ranges; choices are: 8bit or percent; default="8bit"
-c ... colorspace ... colorspace in which to process the image; any valid 3-channel
..................... colorspace is allowed; default=sRGB; note that values must
..................... correspond to the channels of the colorspace

PURPOSE: To convert an image to binary using range values for each channel of the input.

DESCRIPTION: RANGETHRESH converts an image to binary using range values for each channel of the input. This script only works for 3-channel color images, not single channel grayscale images.

ARGUMENTS:

-r redrange ... REDRANGE is the range values for the red channel expressed as a comma separated pair of numbers "low,high". Values can be expressed in range 0 to 255 or range 0 to 100 depending upon the units argument. Values are 0<=float<=255; The default="0,255". (no spaces)

-g greenrange ... GREENRANGE is the range values for the red channel expressed as a comma separated pair of numbers "low,high". Values can be expressed in range 0 to 255 or range 0 to 100 depending upon the units argument. Values are 0<=float<=255; The default="0,255". (no spaces)

-b bluerange ... BLUERANGE is the range values for the red channel expressed as a comma separated pair of numbers "low,high". Values can be expressed in range 0 to 255 or range 0 to 100 depending upon the units argument. Values are 0<=float<=255; The default="0,255". (no spaces)

-u units ... UNITS for the ranges. The choices are: 8bit (8) or percent (p). The default="8bit".

-c colorspace ... COLORSPACE in which to process the image. Any valid 3-channel Imagemagick colorspace is allowed. The default=sRGB. Note that range values must correspond to the channels of the colorspace.

REQUIREMENTS: This script only works for 3-channel color images, not single channel grayscale images. For grayscale images, use the Imagemagick 7 -range-threshold.

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.


EXAMPLE


Original Image
(source)

arguments:
-r "21,179" -g "0,255" -b "0,209" -u 8bit -c hsv



What the script does is as follows:

  • Reads the input image
  • Separates the low and high values from the 3 ranges
  • Use -range-threshold for each channel
  • Reads the input image
  • Writes the output

This is equivalent to the following IM commands for the case of
units=percent and Imagemagick 7 after the low and high values
have been separated from the ranges.

  • convert $tmpA -colorspace $colorspace -separate +channel \
    \( -clone 0 -range-threshold "%[fx:$redlow],%[fx:$redlow],%[fx:$redhigh],%[fx:$redhigh]%" \) \
    \( -clone 1 -range-threshold "%[fx:$greenlow],%[fx:$greenlow],%[fx:$greenhigh],%[fx:$greenhigh]%" \) \
    \( -clone 2 -range-threshold "%[fx:$bluelow],%[fx:$bluelow],%[fx:$bluehigh],%[fx:$bluehigh]%" \) \
    -delete 0-2 \
    -evaluate-sequence multiply "$outfile"