New: Hough line detector

Announcements pertaining to ImageMagick, or ImageMagick related software. This list is moderated. No discussions here, instead post to the users group instead.
Post Reply
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

New: Hough line detector

Post by fmw42 »

As of version 6.8.9.1, Imagemagick will contain the Hough line detector. This is used in conjunction with any binary edge extracted image via -edge, -morphology edge or -morphology convolve Sobel or probably best, the new Canny edge detector, -canny. (see http://www.imagemagick.org/discourse-se ... =4&t=25405)

The Hough process accumulates counts for every white pixel for every possible orientation (for angles from 0 to 179 in 1 deg increments) and distance from the center of the image to the corner (in 1 px increments) and stores the counts in an accumulator matrix of angle vs distance. The size of the accumulator will be 180x(diagonal/2). It then searches this space for peaks in counts and converts the locations of the peaks to slope and intercept in the normal x,y input image space. The slope/intercepts are used to find the endpoints clipped to the bounds of the image. The lines are then drawn. The counts are a measure of the length of the lines

Given a binary edge image, the syntax is as follows

Code: Select all

convert binary_edge_image -background somecolor1 -fill somecolor2 -hough-lines WxH+threshold hough_lines_image
This will produce color lines on a color background. If -background and -fill are left off, then the default is black lines on a black background, which is a totally black image. The WxH specifies the filter size for locating the peaks in Hough space. The threshold is used to exclude lines whose counts are less than the argument.

One may extract a text file of just the line endpoints and counts by using the .mvg suffix on the output file.

Code: Select all

convert binary_edge_image -hough-lines WxH+count hough_lines_image.mvg
If one wants to see the hough accumulator image, then use -define hough-lines:accumulator=true and there will be two output images. The first will be the lines image and second the accumulator image. To see anything in the accumulator image, it needs to be stretched using -contrast-stretch.

Here are some examples.

Code: Select all

convert rectangle.png \
\( +clone -canny 0x1+10%+30% -write rectangle_canny.png \
-background none -fill red -stroke red -strokewidth 2 \
-hough-lines 9x9+40 -write rectangle_lines.png \) -composite rectangle_hough.png
Image Image
Image Image

Code: Select all

convert rectangle.png -canny 0x1+10%+30% -hough-lines 9x9+40 rectangle_lines.mvg

Code: Select all

cat rectangle_lines.mvg

# Hough line transform: 9x9+40
viewbox 0 0 276 276
line 158.785,0 -0.564065,276  # 48
line 0,29.4581 276,188.807  # 99
line 0,87.1932 276,246.542  # 97
line 274.255,0 114.906,276  # 48
or to just send the above data directly to the terminal without saving as a file:

Code: Select all

convert rectangle.png -canny 0x1+10%+30% -hough-lines 9x9+40 MVG:-

Code: Select all

convert rectangle.png -canny 0x1+10%+30% -fill white -stroke white \
-define hough-lines:accumulator=true -hough-lines 9x9+40 \
-delete 0 -contrast-stretch 0.1% rectangle_accumulator.png
Image


Code: Select all

convert blocks.gif -median 3 \
\( +clone -canny 0x1+10%+25% -write blocks_canny.png \
-background none -fill red -stroke red -strokewidth 2 \
-hough-lines 9x9+30 -write blocks_lines.png \) -composite blocks_hough.png
Image Image
Image Image


Code: Select all

convert fence.png \
\( +clone -canny 0x1+10%+40% -write fence_canny.png \
-background none -fill red -stroke red -strokewidth 2 \
-hough-lines 9x9+150 -write fence_lines.png \) -composite fence_hough.png
Image Image
Image Image
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: New: Hough line detector

Post by anthony »

I have added the option to IM Examples
Huogh Line Detection
http://www.imagemagick.org/Usage/transform/#hough

Can you have a look and see if there is any corrections or additions that should be made.


Hmmm... a nice addition would vbe to expand the comment to not only include the threshold strength, but the angle and distance of the line (position of peak in accumulator). A comment near the top of the image could also be added as a 'reminder' of what the numbers mean.

EG: add this to the generated MVG text.

Code: Select all

#  X1,  Y1   X2,  Y2     # acculator  angle  offset
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply