Fred's ImageMagick Scripts



    Licensing:

    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: http://www.imagemagick.org/script/license.php

MORPHOLOGY


Performs binary or grayscale morphologic operations on an image, including dilate, erode, open and close.

Download Script

last modified: August 27, 2013



USAGE: morphology [-m mode] [-t type] [-i iterations] [-r ramp] infile outfile
USAGE: morphology [-h or -help]

-m .... mode ........... binary or grayscale morphology; default=binary
-t .... type ........... dilate, erode, open, close, majority, edgein,
........................ edgeout, gradient, feather, average, spread, tophat,
........................ bottomhat, thin, prune, thicken; default=close
-i .... iterations ..... number of iterations to perform; default=1 for
........................ all but thin, prune and thicken for which the
........................ default is to iterate until convergence.
-r .... ramp ........... ramp distance; for type=feather, the white areas
........................ of a binary image will be ramped linearly to
........................ black from ramp distance inside the white area up
........................ to the black edge; ramp>=0; default=0

PURPOSE: To perform binary or grayscale morphologic operations on an image, including dilate, erode, open, close and others.

DESCRIPTION: MORPHOLOGY applies any one of the following morphologic operations to an image: dilate, erode, open or close, and other similar types of operations, depending on the values in a 3x3 neighborhood of each pixel in the image. The dilate operation returns the maximum value in the neighborhood. The erode operation returns the minimum value in the neighborhood. The open operation performs a dilate followed by an erode. The close operations performs an erode followed by a dilate. If the mode is set to binary, the script assumes the input image is binary (thresholded to black and white) and applies a much more efficient algorithm.

ARGUMENTS:

-m mode ... MODE specifies the algorithmic approach to use. The allowed values are binary and grayscale. If mode is set to binary, the script assumes the input image will be binary (thresholded to 0 and max quantum value) and will apply a more efficient algorithm. If mode is set to grayscale, the script will apply a slightly less efficient algorithm that can be used on grayscale or color images. The default is binary.

-t type ... TYPE specifies the morphologic technique to apply to the image. The morphological operators: dilate, erode, open, close, tophat, bottomhat, and gradient can be used in any mode. For 'binary' mode you can also use edgein, edgeout, majority, feather, thin, prune and thicken. For 'grayscale' mode you can also use average and spread.

The dilate operation returns the maximum value in the neighborhood.

The erode operation returns the minimum value in the neighborhood.

The open operation performs a dilate followed by an erode.

The close operation performs an erode followed by a dilate.

The tophat operation is the result produced from the open operation subtracted from the original image.

The bottomhat operation is the result produced from the close operation subtracted by the original image.

The gradient operation is the result produced by subtracting the erode result from the dilate result.

The edgein operation return the interior edge at the transition between white and black. (Binary mode only)

The edgeout operation return the exterior edge at the transition between white and black. (Binary mode only)

The majority operation just selects the majority from all the neighborhood values. (Binary mode only)

The thin operation strips the outer layers of the white areas and if iterated until convergence produces a skeleton. (Binary mode only)

The prune operation is generally used to remove spurs left from the thinning operation. It is typically used only a specific number of iterations. If iterated until convergence, it will produce a totally black image unless the thinned white area is a closed curve. (Binary mode only)

The thicken operation produces a 45 degree convex hull, if continued to convergence. (Binary mode only)

The feather operation will linearly ramp from white to black, but just within the white area. Only one iteration is allowed in this case. (Binary mode only, but produces a grayscale result)

The average operation generates a simple average of the neighborhood. (Grayscale mode only, but works on color images, also)

The spread operation does the same thing as average, but assumes the input image has an alpha channel and resets pixels with a defined color back to their original color after each iteration. That is, it spreads the color in an average way out into the transparent areas of the image. (Grayscale mode only, but works on color images, also)

When the input is a binary image, dilate/erode will add/remove white pixels at all transitions from white to black. Similarly, open/close will make interior black areas larger or smaller, but keep the overall outer shape about the same.

When the input image is grayscale or color, dilate/erode and close/open will brighten/darken the color transitions.

When the input is a binary image, dilate/erode will add/remove white pixels at all transitions from white to black. Similarly, open/close will make interior black areas larger or smaller, but keep the overall outer shape about the same.

When the input image is grayscale or color, dilate/erode and close/open will brighten/darken the color transitions.

-i iterations ... ITERATIONS specifies the number of iterations to apply for the specified technique. Note that for open and close, the iterations are applied successively to the first erode or dilate and then repeated successively for the second dilate or erode. Iteration is ignored for binary mode edgein and edgeout. The default is 1 for all but thin, prune and thicken, which run until convergence as a default.

-r ramp ... RAMP distance for type=feather only. The white areas of a binary image will be linearly ramped to black from ramp distance inside the white area up to the black edge. Ramp is a float>=0; The default=0.

NOTE: The thin, prune and thicken operations can be slow due to each iteration containing 8 complex operations.

References:
http://homepages.inf.ed.ac.uk/rbf/HIPR2/morops.htm
http://www.naun.org/journals/computers/ijcomputers-11.pdf
http://www.ene.unb.br/~juliana/cursos/pimagens/chapter6.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


Binary Morphology

Original Image

Arguments:
-m binary -t majority -i 1

Arguments:
-m binary -t dilate -i 1

Arguments:
-m binary -t erode -i 1

Arguments:
-m binary -t close -i 1

Arguments:
-m binary -t open -i 1

Arguments:
-m binary -t edgein -i 1

Arguments:
-m binary -t edgeout -i 1

Arguments:
-m binary -t gradient -i 1

 

 


Grayscale Morphology

Original Image

Arguments:
-m grayscale -t dilate -i 1

Arguments:
-m grayscale -t erode -i 1

Arguments:
-m grayscale -t close -i 1

Arguments:
-m grayscale -t open -i 1



Example Use - Mask Creation
If we use a thresholded difference image, it may leave holes.
So we follow this operation using the morphology close to
fill the holes. However, we need to pad the binary image to
expand its size so that the white part is not too close to
the image boundary or it would cause some bleeding of the
white area.

Original Image

difference from background
convert cyclops.png \
\( +clone -fx 'p{0,0}' \) \
-compose Difference -composite \
-modulate 100,0 +matte \
cyclops_diff.png

thresholded difference
convert cyclops_diff.png \
-threshold 15% \
cyclops_diff_mask1.png

pad border to avoid bleeding
convert cyclops_diff_mask1.png \
-bordercolor black -border 10 \
cyclops_diff_mask2.png

morphologic close operation
morphology -t close \
cyclops_diff_mask2.png cyclops_diff_mask3.png

remove padding
convert cyclops_diff_mask3.png[100x100+10+10] \
cyclops_diff_mask.png



Thinning

Original Image

Arguments:
-m binary -t thin
(12 iteration convergence)

Animation



Thinning Of Shape With No Holes

Original Image

Arguments:
-m binary -t thin
(19 iteration convergence)

Animation



Pruning Of Thinned Shape With No Holes

Original Image
(result from example above)

Arguments:
-m binary -t prune -i 1

Arguments:
-m binary -t prune -i 5



Generate Thick Outline Of Shape With No Holes

Original Shape With No Hole

Outline
Arguments:
-m binary -t edgeout -i 1

Dilated Outline
Arguments:
-m binary -t dilate -i 1



Thinning Of Thick Outline Of Shape

Thick Outline Of Shape
(result from example above)

Arguments:
-m binary -t thin
(5 iteration convergence)

Animation



Pruning Of Thinned Outline Of Shape

Thinned Outline Of Shape
(result from example above)

Arguments:
-m binary -t prune
(4 iteration convergence)

Animation



Can You Guess What The Skeleton Of This Images Will Be?
(move your cursor over the image to see the skeleton)



Thickening of Characters Image

Original Image

Arguments:
-m binary -t thicken
(11 iteration convergence)

Animation



Using Open And Tophat To Remove And Extract Patterns

Original Image

Hatch Pattern Removed
Arguments:
-m grayscale -t open -i 4

Original Image

Hatch Pattern Extracted
Arguments:
-m grayscale -t tophat -i 4
result is then thresholded at 4%



What the script does is as follows:

  • Applies either a 3x3 local minimum, maximum or a
    a combination of the two to an image
  • For binary morphology this can be achieved by a
    combination of convolution and thresholding

This is equivalent to the following IM commands for
the case of a binary open operation for Q16 IM.

  • convert $infile -convolve "1,1,1,1,1,1,1,1,1" -threshold 99% $tmp0
  • convert $tmp0 -convolve "1,1,1,1,1,1,1,1,1" -threshold 0 $outfile