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.

SEPARATE


Identifies each separate isolated white shape in a binary image.

Download Script

last modified: December 15, 2018



USAGE: separate [-m mode] [-t] [-v] [-l] [-b bgcolor] [-e exponent] infile outfile
USAGE: separate [-h or -help]

-m .... mode ......... output mode: 1=index number; 2=stretch index number;
...................... 3=colored index number; 4=output image for each
...................... stretched index number; 5=output image for each
...................... binarized index number; 6=output image for each
...................... colored index number; default=3
-g .... grid ......... grid spacing in both x and y as percent of
...................... image width and height; used to locate white areas;
...................... integer>0; default is not to use grid, but use
...................... precise but slower method
-t ................... trim to bounding box in method=4,5,6
-v ................... keep virtual canvas when trimming
-l ................... list image size, offset, area, centroid and morphology
...................... distance center when trimming for method=4,5,6
-b .... bgcolor ...... color for desired background in the output image(s);
...................... choices are: black or none; default=black
-e .... exponent ..... exponent used in non-linear log transformation to
...................... help shift colors away from black for mode=3 or 6;
...................... integer>0; default=1

PURPOSE: To identify each separate (isolated) white shape in a binary (white on black) image.

DESCRIPTION: SEPARATE identifies each separate (isolated) white shape in a binary (white on black) image. Any two-tone image may be used. The image will be converted to binary black/white. Multiple output formats are available. The background for the output image(s) can be either black or transparent (none).

ARGUMENTS:

-m mode ... MODE is the desired output format. It can one image or multiple images. Mode=1 simply sets the graylevel of each shape to an index from 1 to the number of shapes. The max number of shades is 255. The background will have index 0. For a small number of shapes, this will result in a visually black result. Mode=2 is the same as mode=1, except the resulting image is linearly stretched to full dynamic range so that the shapes has visible graylevels. Mode=3 is the same as mode=2, except the shapes are color coded. Mode=4 is the same as mode=2, but one output image is created for each shape. Mode=5 is the same as mode=4, except the each output is binarized. Mode=6 is the same as mode=4, except each output is color coded. The default=3

-g grid ... GRID is the grid spacing for testing points in the input image to see if they are white or black. The grid value is specified as an integer percent greater than 0 and less than 100 of the width and height of the input image. The default is not to use the grid, but to use a precise, but slower method.

-t ... TRIM to bounding box for methods 4, 5 or 6

-v ... Keep virtual canvas information with trimmed outputs. This requires that the output format be one that keeps a virtual canvas, such as PNG or GIF or TIFF.

-l ... List image size and offset (WIDTHxHEIGHT+XOFF+YOFF), area, centroid, and morphology distance center for trimmed outputs for methods 4, 5 or 6.

-b bgcolor ... BGCOLOR is the background color to be used in the output image(s). The choices are: black or none (i.e. transparency). The default is black.

-e exponent ... EXPONENT is used in a non-linear transformation of the graylevel values to map them to colors such that the colors are shifted away from black. Values are integers>0. The default=1. The more shapes/colors that are used, the larger the value. Typical value is about 6 for example, for 26 colors used.

REQUIREMENT: IM version 6.5.5-1 due to the use of auto-level, 6.5.0-9 due to the use of compare with unequal sized images, 6.7.5-7 for morphology distance for option -l.

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


Mode=1
(original image: http://www.codeproject.com/KB/GDI-plus/Image_Processing_Lab.aspx)

Original Image

Arguments:
-m 1

Histogram

 16638: (  0,  0,  0) #000000 black
  2917: (  1,  1,  1) #010101 rgb(1,1,1)
  6303: (  2,  2,  2) #020202 rgb(2,2,2)
   542: (  3,  3,  3) #030303 grey1
  1600: (  4,  4,  4) #040404 rgb(4,4,4)
		



Mode=2

Original Image

Arguments:
-m 2



Mode=3

Original Image

Arguments:
-m 3



Mode=4 Black Background
-m 4 -b black

original

index 0
(background)

index 1

index 2

index 3

index 4



Mode=4 Transparent Background
-m 4 -b none

original

index 0
(background)

index 1

index 2

index 3

index 4



Mode=5 Black Background
-m 5 -b black

original

index 0
(background)

index 1

index 2

index 3

index 4



Mode=5 Transparent Background
-m 5 -b none

original

index 0
(background)

index 1

index 2

index 3

index 4



Mode=6 Black Background
-m 6 -b black

original

index 0
(background)

index 1

index 2

index 3

index 4



Mode=6 Transparent Background
-m 6 -b none

original

index 0
(background)

index 1

index 2

index 3

index 4



Mode=3

Original Image

Arguments:
-m 3 -e 6



What the script does is as follows:

  • Copies the input image for processing as a temp file
  • Searches the input image for a white pixel
  • Gets its location
  • Floodfills the temp file with graylevel=1 at that location
  • Floodfills the infile with black at the location
  • Repeats with graylevel=2 etc, until no white pixels are found in infile
  • Linear stretches the temp image and assigns colors to each graylevel
  • Outputs the resulting temp image

This is equivalent to the following IM commands for the method=3

  • convert $infile $tmpA2
  • val=0
  • graylvl=1
  • while [ $val -eq 0 -a $graylvl -ne 256 ]; do
    # find white pixel and get its coords
    result=`compare -metric pae -dissimilarity-threshold 1 \
    $infile -size 1x1 xc:white null: 2>&1`
    val=`echo "$result" | cut -d\ -f 1`
    coords=`echo "$result" | cut -d\ -f 4`
    [ $val -ne 0 ] && break
    # floodfill $tmpA2 with graylevel index at coords
    convert $tmpA2 -fill "gray(${graylvl})" -draw "color ${coords} floodfill" $tmpA2
    # floodfill $infile with black at coords
    convert $infile -fill "black" -draw "color ${coords} floodfill" $infile
    graylvl=$(($graylvl + 1))
    done
  • convert \( $tmpA2 -auto-level \) \
    \( -size 1x1 xc:black xc:red xc:orange xc:yellow xc:green1 xc:cyan \
    xc:blue xc:blueviolet +append -filter Cubic -resize 256x1! \) \
    -interpolate nearest-neighbor -clut -black-threshold ${blackthresh}% \
    $outfile