Hough transform for lines¶
| Fact | Value |
|---|---|
| Also called | Hough lines, Line Hough transform, Standard Hough transform |
| Status | Implemented and tested |
| Family | Texture, corners, blobs and features |
| Engine | Science engine |
| Type | Nonlinear, transform, voting in line space |
| Output | Image |
| Operator shown as | algorithm (exact) |
| Support radius | global |
| Code | engine/src/pixelnexus_engine/filters/hough.py, hough_lines |
| Operator code | engine/src/pixelnexus_engine/filters/hough.py, hough_lines_representation |
| Packages | scikit-image |
Overview¶
The Hough transform finds straight lines by voting. First Canny finds the edge pixels. Then every edge pixel votes for all the lines that could pass through it. A line that many edge pixels share gets many votes, even when it has gaps or is partly hidden. The filter draws the found lines across the image, shows the accumulator of votes, or lists the lines in a table for the Features panel. It runs in the PixelNexus engine, with scikit-image.
How it works¶
- Find edges with Canny on the luma.
- Let every edge pixel vote for all lines through it.
- Find the peaks of the accumulator.
- Draw the lines over the edges, show the accumulator, or list the lines.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(\theta\): The angle of the normal of a line
- \(\rho\): The distance of a line from the origin
- \(H\): The accumulator of votes
A line is described by the angle theta of its normal and its distance rho from the origin, as Duda and Hart proposed. An edge pixel at (x, y) lies on all lines with x cos theta + y sin theta = rho, which is a sine curve in the plane of theta and rho. Every edge pixel adds one vote along its curve in the accumulator H. Where many curves cross, many edge pixels lie on one line, so peaks of H are lines. Here scikit-image measures theta with y pointing down and the origin at the center of the top-left pixel. The peaks must reach the vote threshold times the largest vote and lie apart by the distance and angle limits.
A line y = m x + c cannot describe vertical lines, since m would be infinite. Duda and Hart used the normal form instead. Drop a perpendicular from the origin to the line. Its angle theta and its length rho describe the line, and both stay finite for every line.
Fix one edge pixel (x, y). The lines through it have rho = x cos theta + y sin theta, one for each theta. In the plane of theta and rho, this is a sine curve. Two edge pixels give two curves, which cross at the one line through both pixels. Many edge pixels on one line give many curves through one point. The accumulator counts the curves through each cell, and its peaks are the lines.
Each peak only needs enough pixels on the line, not a solid line, so dashed and broken lines are found too. The price is that the result is an infinite line. It does not tell where the edge pixels start and stop. Hough-based edge linking finds segments instead.
Kernel or operator¶
There is no kernel. The Inspector asks the engine for the stages with the current settings: the Canny edges, the voting, and the peak search.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Output output |
Lines over the edges | Lines over the edges, Accumulator, Table of lines | The found lines in white over the edges in gray, the accumulator of votes, or a table of the lines. |
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 |
0.5 | 0.05 to 1 | A line needs at least this share of the votes of the strongest line. scikit-image uses 0.5. |
Most lines lines |
10 | 1 to 100 | At most this many lines, the strongest first. |
Distance apart minDistance |
9 | 1 to 100 px | Two lines must be at least this far apart in distance from the origin, or far enough apart in angle. scikit-image uses 9. |
Angle apart minAngle |
10 | 1 to 90 | Two lines must be at least this many angle steps apart, or far enough apart in distance. With 180 angles, a step is 1 degree. scikit-image uses 10. |
Implementation¶
hough_lines in engine/src/pixelnexus_engine/filters/hough.py finds edges with skimage.feature.canny on the luma from 0 to 1, with the border mode as the mode of its smoothing. Then skimage.transform.hough_line builds the accumulator. It uses the given number of angles theta from -90 degrees up to, but not including, 90 degrees, and distances rho in steps of 1 pixel. The origin is the center of the top-left pixel. Then hough_line_peaks keeps peaks of at least the vote threshold times the largest vote, apart by the distance and angle limits, at most the given number.
Here scikit-image measures theta with y pointing down, so it turns clockwise on screen. The table gives the direction of each line instead, counterclockwise from the x axis from 0 to 180 degrees. It also gives the distance of the line from the origin, its votes, and the two points where it leaves the image. The lines output draws each line in white across the image, over the edges in gray (96).
The accumulator view is turned to our angles. From left to right, the normal of the line turns counterclockwise from -90 to 90 degrees. From bottom to top, the signed distance grows from minus to plus the length of the diagonal. All edge pixels have x and y of at least 0, so most votes fall in the upper half. The view shows log(1 + votes), which is our choice, so weak curves stay visible. It is resampled to the size of the image and stretched.
Worked example¶
| Direction (deg) | Distance (px) | Votes |
|---|---|---|
| 126 | 12 | 75 |
| 0 | 91 | 68 |
| 0 | 139 | 60 |
| 1 | 102 | 55 |
| 74 | 34 | 52 |
| 136 | 56 | 50 |
| 91 | 147 | 44 |
| 89 | 92 | 40 |
The table output with the defaults on the test image of the figure. The direction counts counterclockwise from the x axis. The distance is that of the line from the center of the top-left pixel.
The three sides of the triangle are found at 74, 126 and 0 degrees. The dashed line, at 135 degrees on screen, is found at 136, though its dashes cover only about 60 percent of it. The rectangle gives four lines near 0 and 90 degrees, three of them a degree off. Its sides are short, so lines a degree apart collect almost the same votes.
Complexity¶
- Time: \(O(E A)\)
- Memory: \(O(A D)\)
E is the number of edge pixels, A the number of angles and D the length of the image diagonal.
Artifacts and limitations¶
Limitations
- Lines are infinite. The filter does not tell where a segment starts or ends. Edge linking does.
- Many short edges in a busy image add up to false lines.
- Thick edges vote for several nearby lines.
Artifacts
- Lines that are slightly off in angle, when the edges are short.
Tests¶
The edges must be scikit-image's Canny with the mapped border mode. A horizontal and a vertical line of edge pixels must give the directions 0 and 90 degrees, their distances, their votes and their end points. Falling and rising diagonals must give 135 and 45 degrees. The table must hold the peaks of scikit-image, strongest first. The accumulator view must put a horizontal line at the right edge, below the middle. The lines output must draw white lines over gray edges, and a flat image must give no lines and a black accumulator. The engine API test runs the filter.
References¶
- Duda, R. O., and Hart, P. E. Use of the Hough transformation to detect lines and curves in pictures. Communications of the ACM 15(1), 11 to 15, 1972. DOI 10.1145/361237.361242.
- 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.hough_line and hough_line_peaks.