Skip to content

Line detectors

Fact Value
Also called Horizontal line detector, Vertical line detector, Diagonal line detectors
Status Implemented and tested
Family Texture, corners, blobs and features
Engine Browser
Type Nonlinear, spatial, template matching
Output Image
Operator shown as fixed-kernel (exact)
Support radius 1
Code web/src/core/filters/edges/compass.ts, lineDetectorFilter
Operator code web/src/core/filters/edges/compass.ts, LINE_MASKS
Channels Brightness only, RGB

Overview

The line detectors are four 3x3 masks from Gonzalez and Woods that answer most strongly to a thin bright line: horizontal, vertical, and the two diagonals. Use one mask to find lines in one direction, or the strongest of the four to find lines in any of them.

Three panels: thin bright lines in four directions on a dark background, the horizontal mask that keeps only the horizontal line, and the strongest of the four masks that keeps all lines. Three panels: thin bright lines in four directions on a dark background, the horizontal mask that keeps only the horizontal line, and the strongest of the four masks that keeps all lines.
The horizontal mask finds only the horizontal line. The strongest of the four finds all of them.

How it works

  1. Weight the pixels along a direction by 2 and the others by -1.
  2. Add up the weighted window. Flat areas give 0.
  3. Keep the response of one mask, or the largest of the four.

Mathematical foundation

\[ \begin{gathered} L_{\text{h}} = \begin{bmatrix} -1 & -1 & -1 \\ 2 & 2 & 2 \\ -1 & -1 & -1 \end{bmatrix}, \qquad R = \max_{d} \bigl( L_d \star I \bigr) \end{gathered} \]

The formula shows the default parameter values.

where

  • \(L_d\): The line mask for direction d
  • \(R\): The response

Each mask has a row of 2s along one direction and -1 elsewhere, so its weights add up to 0. A bright line one pixel wide along that direction gives a large positive response, flat areas give 0. The other masks are the same pattern turned to vertical and the two diagonals. With the max output, the strongest of the four responses is kept.

Each mask is a matched filter for a one-pixel line: the pattern it responds to most is the line itself. Its weights add up to 0, so it ignores the local brightness. A bright line gives a positive response and a dark line a negative one. The clip mapping keeps only bright lines. Edges give a weaker response than lines, but not zero.

Kernel or operator

The Inspector shows the kernels that run, read from the same code. The Impulse response demo in the Learn panel shows how one bright pixel spreads through them.

Algorithm

  1. Pad the image with the border mode.
  2. Correlate with the chosen line mask, or with all four.
  3. Keep the response, or the largest of the four.
  4. Map the result to 8 bits. Clipping keeps only bright lines.

Parameters

Parameter Default Values Meaning
Output output Strongest of the four Strongest of the four, Horizontal, Vertical, Diagonal from top left, Diagonal from bottom left One direction, or the largest response of all four masks.
Output mapping outputMapping Clip to 0 to 255 Absolute value, Offset by 128, Stretch to full range, Clip to 0 to 255 How signed results, such as gradients, become 8-bit pixel values.
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.
Channels channelScope Brightness only RGB, Brightness only Which part of the color image the filter changes.

Implementation

lineDetectorFilter in web/src/core/filters/edges/compass.ts correlates with one mask, or with all four and keeps the maximum. This entry covers the four masks of Gonzalez and Woods. Other line mask families are not included. The filter runs in the browser engine, in Web Workers, through the shared filter framework in web/src/core/filters/run.ts, which handles the border mode, the channel scope, alpha and the output mapping. By default it works on luma and gives a gray result.

Worked example

\[ W = \begin{bmatrix} 50 & 50 & 50 \\ 210 & 210 & 210 \\ 50 & 50 & 50 \end{bmatrix} \]

A bright horizontal line one pixel wide. The horizontal mask gives 2 x 3 x 210 - 6 x 50 = 960. The vertical mask gives 0, because each of its rows adds up to 0 and every row of this window is constant. A flat window gives 0 for every mask.

Complexity

  • Time: \(O(N)\)
  • Memory: \(O(N)\)

N is the number of pixels per channel. One to four 3x3 correlations per pixel.

Artifacts and limitations

Limitations

  • Finds bright lines. Dark lines give negative responses, which the clip mapping hides.
  • Only four directions and one-pixel width. Other line mask families, such as Laplacian-based or oriented line filters, are not included in this entry.

Artifacts

  • Responses on edges as well as lines, weaker but not zero.

Tests

The reference correlates with the four masks. The test covers the maximum and one diagonal mask for all five border modes.

References

  • Gonzalez, R. C. and Woods, R. E. Digital Image Processing, 4th edition. Pearson, 2018. Section 10.2, line detection.