Fred's ImageMagick Scripts


 

 

MORPHOLOGY


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

Download Script

last modified: May 30, 2008



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

-m .... mode ........... binary or grayscale morphology; default=binary
-t .... type ........... dilate, erode, open, close, majority, edgein, edgeout;
........................ majority, edgein, edgeout only in binary mode; ........................ default=close -i .... iterations ..... number of iterations to perform; default=1

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

DESCRIPTION: MORPHOLOGY applies any one of the following morphologic operations to an image: dilate, erode, open or close using 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 0 and max quantum value) 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 choices are dilate, erode, open and close. 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. Binary mode also allows for majority, edgein and edgout. The majority operation returns white if the majority of pixels is white and black otherwise. The edgein and edgeout operations return the interior or exteriour edge at the transition between white and black. The default is open.

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.

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



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



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 65534 $tmp0
  • convert $tmp0 -convolve "1,1,1,1,1,1,1,1,1" -threshold 1 $outfile