Fred's ImageMagick Scripts


    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:

    Please read the Pointers For Use on my home page to properly install and customize my scripts.


Applies a zero crossing edge detector to an image.

Download Script

last modified: March 01, 2024

USAGE: zerocrossing [-w width ] [-b brightness] [-s smoothing] [-g] infile outfile
USAGE: zerocrossing [-h or -help]

-e ... edge ...... edge type; choices are sobel (s) or morphologic (m);
.................. default=sobel
-l ... laplace ... laplacian type; choices are: 0, 2 and 5; default=0
-a ... amplify ... zero crossing percent amplification; integer>=0;
.................. default=100 (no amplification)
-s ... size ...... 2x morphologic disk size; values are integer>=2;
.................. default=4 (values will be divided by 2 later)
-g ............... convert image to grayscale before getting edges

PURPOSE: To apply a zero crossing edge detector to image.

DESCRIPTION: ZEROCROSSING applies a zero crossing edge detector to image in order to thin the edges. The user may select either sobel or morphologic for the basic type of edge detection as well as the type of laplacian used for detecting the zero crossings.


-e edge ... EDGE type. The choices are sobel (s) or morphologic (m). The default=sobel.

-l laplace ... LAPLACIAN type. The choices are: 0, 2 and 5. The default=0.

-a amplify ... AMPLIFICATION is the zero crossing percent amplification. Values are integer>=0. The default=100 (no amplification).

-s size ... SIZE is twice the morphologic disk size. Values are integer>=2. The default=4 (values will be divided by 2 later).

-g ... converts the input image to GRAYSCALE before extracting edges.

REQUIREMENTS: IM is required for the use of -morphology edges. IM is required for the use of sobel edges.

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 1 -- Sobel Gray





-e sobel -l 0 -g

-e sobel -l 2 -g

-e sobel -l 5 -g



-e sobel -l 5 -a 400 -g


Example 2 -- Morphologic Gray



Mophologic (size=4)


-e morphologic -s 4 -l 0 -g

-e morphologic -s 4 -l 2 -g

-e morphologic -s 4 -l 4 -g

-e sobel -l 5 -s 8 -a 100 -g


-e sobel -l 5 -a 400 -g


Example 3 -- Color



Mophologic (size=4)


-e sobel -l 5 -a 400

-e morphologic -l 5 -a 400


What the script does is as follows:

  • Applies either a sobel or mophologic edge detector to the input image
  • Applies a laplacian edge detector to the input image
  • Applies a gradient magnitude to the laplacian image and thresholds to find the zero crossings.
  • Multiplies the zero crossing (mask) image with the edge detected image.

This is equivalent to the following IM commands

  • convert -quiet -regard-warnings "$infile" +repage "$tmpA1"
  • if [ "$grayscale" = "yes" ]; then
    gproc="-colorspace gray"
  • size=`convert xc: -format "%[fx:$size/2]" info:`
  • if [ "$edge" = "sobel" ]; then
    eproc="-define convolve:scale='!' -define morphology:compose=Lighten -morphology Convolve Sobel:>"
    elif [ "$edge" = "morphologic" ]; then
    eproc="-morphology edge disk:$size -auto-level "
  • convert $tmpA1 $gproc \
    \( -clone 0 $eproc \) \
    \( -clone 0 -bias 50% -morphology Convolve Laplacian:$laplace \) \
    \( -clone 2 -define convolve:scale="$amplify%!" -morphology convolve sobel:0 -evaluate pow 2 \) \
    \( -clone 2 -define convolve:scale="$amplify%!" -morphology convolve sobel:90 -evaluate pow 2 \) \
    -delete 0,2 \
    \( -clone 1 -clone 2 -compose plus -composite -evaluate pow 0.5 -negate -threshold 0 -negate \) \
    -delete 1,2 -compose multiply -composite \