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.

LOCATECOLORS


Modifies an image showing only those pixels which are within the specified color range

Download Script

last modified: December 15, 2018



USAGE: locatecolors -b begincolor -e endcolor [-m mode] infile outfile
USAGE: locatecolors [-h or -help]

-b .... begincolor ...... first color for range of colors; any valid
......................... IM opaque color
-e .... endcolor ........ second color for range of colors; any valid
......................... IM opaque color
-m .... mode ............ mode for combining channel ranges into mask;
......................... choices are: "and" or "or"; for "and" use
......................... overlap of channel color ranges; for "or" use
......................... all ranges independently; default=and

PURPOSE: To modify an image to show only those pixels which are within the specified color range.

DESCRIPTION: LOCATECOLORS modifies an image to show only those pixels which are within the specified color range. A count of the number of pixels will be presented at the terminal.

ARGUMENTS:

-b begincolor ... BEGINCOLOR is the first color used to specify the desired range of colors. Any valid IM opaque color is allowed.

-e endcolor ... ENDCOLOR is the second color used to specify the desired range of colors. Any valid IM opaque color is allowed.

-m mode ... MODE specifies how the ranges of colors are to be combined into a mask. Choices are: "and" or "or". If "and" is specified, then the mask will be created only where the channels overlap. If "or" is specified, then the mask will be created from the sum of all the channel ranges. The default=and.

Note: the channel ranges will be manipulated in 8-bit colors to create the mask.

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


Original Image

Arguments:
-b "rgb(230,20,20)" -e "rgb(250,50,50)" -m and

Arguments:
-b "rgb(230,20,20)" -e "rgb(250,50,50)" -m or



Original Image

Arguments:
-b "rgb(96,96,96)" -e "rgb(162,162,162)" -m and

Arguments:
-b "rgb(96,96,96)" -e "rgb(162,162,162)" -m or



What the script does is as follows:

  • Converts the image to 8-bit rgb colorspace
  • Separates the rgb channels
  • Thresholds each channel to isolate the specified range of graylevel values
  • Composites the r,g,b masks into one mask and puts that into the alpha channel

This is equivalent to the following IM commands for the case of mode="and".

  • For each r,g,b channel do the following:
  • val1=$1
  • val2=$2
  • if [ $val1 -eq 0 -a $val2 -eq 255 ]; then
  • proc="-fill white -colorize 100%"
  • elif [ $val1 -eq $val2 ]; then
  • proc="-fill red -opaque rgb($val1,$val1,$val1) -fill black +opaque red -fill white -opaque red"
  • else
  • pct1=`convert xc: -format "%[fx:100*$val1/255]" info:`
  • pct2=`convert xc: -format "%[fx:100*$val2/255]" info:`
  • if [ "$pct2" = "100" ]; then
  • proc="-black-threshold $pct1% -fill white +opaque black"
  • elif [ "$pct1" = "0" ]; then
  • proc="-fill rgb(1,1,1) -opaque black -white-threshold $pct2% -fill black -opaque white -fill white +opaque black"
  • else
  • proc="-black-threshold $pct1% -white-threshold $pct2% -fill black -opaque white -fill white +opaque black"
  • fi
  • fi
  • convert $infile -alpha off -colorspace rgb -depth 8 -separate \
    \( -clone 0 $redproc \) \
    \( -clone 1 $greenproc \) \
    \( -clone 2 $blueproc \) \
    -delete 0-2 \
    \( -clone 0 -clone 1 -compose $function -composite \
    -clone 2 -compose $function -composite \) \
    -delete 0-2 $tmp2A
  • count=`convert $tmp2A -format "%[fx:floor(mean*w*h)]" info:`
  • percent=`convert $tmp2A -format "%[fx:100*mean]" info:`
  • echo ""
  • echo "Count Of Matching Pixels = $count"
  • echo "Percentage Of Matching Pixels = $percent%"
  • echo ""
  • convert $infile $tmp2A -alpha off -compose copy_opacity -composite $outfile