Skip to content

Hough-based edge linking

Fact Value
Also called Probabilistic Hough transform, Progressive probabilistic Hough transform, Line segment detection
Status Implemented and tested
Family Edge detection and boundary analysis (also Texture, corners, blobs and features)
Engine Science engine
Type Nonlinear, transform, random voting and segment tracing
Output Image
Operator shown as algorithm (exact)
Support radius global
Code engine/src/pixelnexus_engine/filters/hough.py, hough_linking
Operator code engine/src/pixelnexus_engine/filters/hough.py, hough_linking_representation
Packages scikit-image

Overview

Canny gives edge pixels, not lines. Edge linking joins the pixels that lie on a straight line into segments, each with a start and an end. This filter uses the progressive probabilistic Hough transform of Matas, Galambos and Kittler. Edge pixels vote one at a time, in random order. As soon as a line has enough votes, its segment is traced along the edge pixels and removed. The result shows the segments over the edges, or lists them in a table. It runs in the PixelNexus engine, with scikit-image.

Four panels. The input has a bright triangle, a gray rectangle and a dashed diagonal line. With the defaults, white segments cover the three sides of the triangle, the long sides of the rectangle and both edges of the dashed line, while the short sides of the rectangle stay gray. With the shortest segment 20, the short sides of the rectangle turn white too. With no gap allowed, the dashed line stays gray, and one side of the triangle is only partly white. Four panels. The input has a bright triangle, a gray rectangle and a dashed diagonal line. With the defaults, white segments cover the three sides of the triangle, the long sides of the rectangle and both edges of the dashed line, while the short sides of the rectangle stay gray. With the shortest segment 20, the short sides of the rectangle turn white too. With no gap allowed, the dashed line stays gray, and one side of the triangle is only partly white.
A test image with a triangle, a rectangle and a dashed line. Then the segments in white over the Canny edges in gray with the defaults, with the shortest segment 20, and with the shortest segment 20 and the largest gap 0.

How it works

  1. Find edges with Canny on the luma.
  2. Take edge pixels in random order and let each vote for all lines through it.
  3. When a line reaches the threshold, trace its segment along the edge pixels.
  4. Keep segments that span enough pixels, and remove their pixels and votes.
  5. Draw the segments over the edges, or list them.

Mathematical foundation

\[ \rho = x \cos\theta + y \sin\theta, \qquad H(\theta, \rho) \ge 10 \]

The formula shows the default parameter values.

where

  • \(\theta, \rho\): The normal angle and distance of a line
  • \(H\): The accumulator of votes

The progressive probabilistic Hough transform of Matas and coworkers votes with one edge pixel at a time, in random order, in the same accumulator of lines as the standard transform. When a line reaches the vote threshold, the filter follows that line from the pixel that voted last, in both directions, and jumps over gaps up to the largest gap. A segment is kept when it spans the shortest length in x or in y. Its pixels are then removed from the edges and their votes from the accumulator. Pixels on a found segment never get their turn to vote, which saves time. The order is random, so this filter fixes the seed and gives the same result each time.

The standard Hough transform lets every edge pixel vote and then looks for peaks. That finds infinite lines, and it is slow when there are many edge pixels. The progressive version stops voting as soon as one line is clear. Then it walks along that line from the pixel that voted last, in both directions, as long as it meets edge pixels or gaps up to the largest gap. That walk gives the start and end of the segment. The pixels of a kept segment leave the edges and take their votes back. Later lines then do not get votes from pixels that already belong to a segment.

Kernel or operator

There is no kernel. The Inspector asks the engine for the stages with the current settings: the Canny edges, the random voting, and the segment limits.

Parameters

Parameter Default Values Meaning
Output output Segments over the edges Segments over the edges, Table of segments The segments in white over the edges in gray, or a table of the segments.
Edge smoothing sigma 1 0.5 to 5 px The sigma of the Gaussian blur before Canny finds edges. scikit-image uses 1.
Low threshold lowThreshold 0.1 0 to 2 Weak edges must reach this Sobel magnitude, on the luma from 0 to 1. scikit-image uses 0.1.
High threshold highThreshold 0.2 0 to 2 Strong edges must reach this Sobel magnitude. scikit-image uses 0.2. A value below the low threshold counts as the low threshold.
Border mode border Reflect 101 (default) Reflect 101 (default), Reflect, Replicate, Constant (zero), Wrap How pixels outside the image are filled when the window reaches past the edge.
Angles angles 180 30 to 720 The number of angles over half a turn. scikit-image uses 180, one per degree.
Vote threshold threshold 10 1 to 500 A line must reach this many votes before its segment is traced. scikit-image uses 10.
Shortest segment minLength 50 2 to 1000 px A segment is kept when it spans at least this many pixels in x or in y. scikit-image uses 50.
Largest gap maxGap 10 0 to 100 px A segment may jump over a gap of at most this many pixels. scikit-image uses 10.

Implementation

hough_linking in engine/src/pixelnexus_engine/filters/hough.py finds edges with skimage.feature.canny on the luma from 0 to 1. Then skimage.transform.probabilistic_hough_line finds the segments with the given angles, vote threshold, shortest length and largest gap. Its seed is fixed at 0, so the same image gives the same segments. Here scikit-image keeps a segment when its extent in x or in y reaches the shortest length, so a diagonal segment must be a little longer than that. Segments that are too short keep their pixels and their votes. The table gives the end points, the length and the direction of each segment, longest first. The direction counts counterclockwise from the x axis, from 0 to 180 degrees. The image draws the segments in white over the edges in gray (96), so gray edges are the ones no segment took.

Worked example

Shortest segment (px) Largest gap (px) Segments Total length (px)
50 0 4 255
50 3 7 491
50 10 11 759
20 0 7 386
20 3 13 697
20 10 15 885

The test image of the figure has 624 edge pixels with the default Canny settings. The total length adds up the lengths of all segments. The dashes of the dashed line are 10 pixels long with gaps of about 6.

With no gap allowed, the dashed line stays unlinked, since each dash spans less than 20 pixels in x and in y. Allowing a gap of 3 nearly doubles the total length. A gap of 10 also bridges the gaps of the dashed line. Lowering the shortest segment from 50 to 20 adds the short sides of the rectangle, which are 40 pixels long.

Complexity

  • Time: \(O(E A)\)
  • Memory: \(O(A D + N)\)

E is the number of edge pixels, A the number of angles, D the length of the image diagonal and N the number of pixels. Pixels on found segments do not vote.

Artifacts and limitations

Limitations

  • Only straight segments. Curves become chains of short segments or are dropped.
  • Another seed would give slightly different segments.

Artifacts

  • Segments that stop short of a corner, or run past it along the next edge.

Tests

The segments must be those of scikit-image's probabilistic Hough transform with seed 0, the same on every run. A horizontal and a vertical run of edge pixels must give segments at 0 and 90 degrees with their lengths. The image must show only black, gray edges and white segments, and the table must list the segments. The engine API test runs the filter.

References

  • Matas, J., Galambos, C., and Kittler, J. Robust detection of lines using the progressive probabilistic Hough transform. Computer Vision and Image Understanding 78(1), 119 to 137, 2000. DOI 10.1006/cviu.1999.0831.
  • Canny, J. A computational approach to edge detection. IEEE Transactions on Pattern Analysis and Machine Intelligence PAMI-8(6), 679 to 698, 1986. DOI 10.1109/TPAMI.1986.4767851.
  • scikit-image documentation, skimage.transform.probabilistic_hough_line.