Fred's ImageMagick Scripts


    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:


Uses the Hough Transform technique to compute and display straight lines from a binary edge image.

Download Script

last modified: June 22, 2016

USAGE: houghlines [-d distinc] [-a anginc] [-m maskthresh] [-D dilation] [-b bgcolor] [-c color] [-t thickness] infile linefile [accumulatorfile]
USAGE: houghlines [-h or -help]

-d ... dstinc ....... distance bin increment; float>0; default=1
-a ... anginc ....... angle bin increment; float>0; default=1
-m ... maskthresh ... masking threshold; most critical parameter to find
..................... peaks in the accumulator image (i.e., longest edges);
..................... 0<integer<100; default=50
-D ... dilation ..... mask dilation amount; integer>1; default=4
-b ... bgcolor ...... background color for Hough lines
-c ... color ........ color of Hough lines
-t ... thickness .... thickness of Hough lines; integer>0
-C .................. apply contrast stretch to the accumulator image

PURPOSE: Uses the Hough Transform technique to compute and display straight lines from a binary edge image.

DESCRIPTION: Uses the Hough Transform technique to compute and display straight lines from an input binary edge image. The input must be a binary edge image that is created by any edge extraction technique. The Canny edge image is generally the best. The output will be the straight lines of user specified color drawn on a user specified background color. The line end points and other textual information will be displayed to the terminal. A optional second output image may be the Hough accumulator image. It will need to be contrast stretched (-contrast-stretch 0x0.25% to see the data). The accumulator horizontal axis corresponds to the line distance relative to the upper left corner of the image and the vertical axis corresponds to the angle of of the lines (0 to 180 deg). The horizontal dimension of the accumulator image is 2*width+1 of the input image, since distances can be negative. The convergent points of the "butterfly patterns" are the relevant peaks that are extracted to compute the straight lines. The line endpoints are clipped to the dimensions of the input image. Note that this script was a prototype for the IM development of -hough-lines and has the origin at the top left corner rather than at the center as is done in the IM function. The mask threshold argument is the critical parameter to locate the centers of the "butterfly patterns" and thus extract the Hough lines. The script allows the user to specify both the distance increment and angle increment contrary to the IM function -hough-lines, which is restricted to values of 1 for the increments.


-d dstinc ... DSTINC is the distance bin increment. Values are float>0. The default=1.

-a anginc ... ANGINC is the angle bin increment. Values are float>0. The default=1.

-m maskthresh ... MASKTHRESH is the masking threshold for creating a mask from the accumulator image. This is the most critical parameter to find peaks in the accumulator image (i.e., longest edges). Values are 0<integer<100. The default=50. Larger values will produce fewer lines and smaller values will produce more lines.

-D dilation ... DILATION is the dilation amount applied to the mask image. Values are integer>1. The default=4.

-b bgcolor ... BGCOLOR is the background color for the Hough lines. Any valid IM color is allowed. The default is black.

-c color ... COLOR is the line color for the Hough lines. Any valid IM color is allowed. The default is white.

-t thickness ... THICKNESS is the thickness of the Hough lines. Values are integer>0. The default=2

-C ... apply CONTRAST STRETCH to the accumulator image

Limitations: Requires IM or higher due to the use of -define identify:locate=maximum and -define identify:limit


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

Original Image


convert rectangle.png -canny 0x1+10%+30% rectangle_canny.png

houghlines -m 45 -C rectangle_canny.png rectangle_lines.png rectangle_accum.png

convert rectangle.png \( rectangle_lines.png -background red -alpha shape \) \
-compose over -composite rectangle_lines_overlay.png

Example 2

Original Image


convert blocks.gif -canny 0x1+10%+25% blocks_canny.png

houghlines -m 45 -C blocks_canny.png blocks_lines.png blocks_accum.png

convert blocks.gif \( blocks_lines.png -background red -alpha shape \) \
-compose over -composite blocks_lines_overlay.png

Example 3

Original Image


convert desktop5.png -canny 0x1+10%+30% desktop5_canny.png

houghlines -m 25 -C desktop5_canny.png desktop5_lines.png desktop5_accum.png

convert desktop5.png \( desktop5_lines.png -background red -alpha shape \) \
-compose over -composite desktop5_lines_overlay.png

Example 4

Original Image


convert fence.png -canny 0x1+10%+40% fence_canny.png

houghlines -m 50 fence_canny.png fence_lines.png

convert fence.png \( fence_lines.png -background red -alpha shape \) \
-compose over -composite fence_lines_overlay.png

What the script does is as follows:

  • reads every white pixel's x,y location in the edge image and
    increments the accumulator image for the computed distance and angle
    for that x,y coordinate
  • stretches the accumulator image so the max value is white, then
    dilates it and thresholds it
  • finds the maximum value in the original accumulator,
    computes the slope and intercept and end points clipped
    to the image boundaries
  • floodfills the mask with black at the max location and applies the
    mask to the accumulator to remove that region from further consideration
  • repeats the process to find the next maximum and floodfills the mask
    until the mask image is completely black