Skip to content

Kirsch compass

Fact Value
Also called Kirsch
Status Implemented and tested
Family Edge detection and boundary analysis
Engine Browser
Type Nonlinear, spatial, compass convolution
Output Image
Operator shown as fixed-kernel (exact)
Support radius 1
Code web/src/core/filters/edges/compass.ts, kirschFilter
Operator code web/src/core/filters/edges/compass.ts, KIRSCH_NORTH
Channels Brightness only, RGB

Overview

The Kirsch operator turns one 3x3 mask in eight steps of 45 degrees and keeps the strongest response at each pixel. The strongest mask also gives the edge direction, to the nearest 45 degrees, with no arctangent.

Three panels: disks, the largest Kirsch response with bright rings at the disk edges, and the direction map in which each disk ring shows eight bands of different gray. Three panels: disks, the largest Kirsch response with bright rings at the disk edges, and the direction map in which each disk ring shows eight bands of different gray.
The largest response marks the edges. The direction map has eight values, one per mask.

How it works

  1. Correlate the image with each of the eight masks.
  2. Keep the largest response as the edge strength.
  3. The direction of that mask is the edge direction.

Mathematical foundation

\[ \begin{gathered} K_{\text{N}} = \begin{bmatrix} 5 & 5 & 5 \\ -3 & 0 & -3 \\ -3 & -3 & -3 \end{bmatrix}, \qquad G = \max_{k = 0, \dots, 7} \bigl( K_k \star I \bigr) \end{gathered} \]

The formula shows the default parameter values.

where

  • \(K_k\): The mask turned by k steps of 45 degrees
  • \(G\): The largest response

The north mask weights the top row with 5 and the rest of the ring with -3. Moving its outer ring by one cell turns it by 45 degrees. The eight masks cover all directions, and the largest response is the edge strength. The mask that gives it is the direction.

The north mask weights the top row with 5 and the other ring cells with -3, so its weights add up to 0 and flat areas give 0. Moving the outer ring by one cell turns the mask by 45 degrees. The maximum over the eight masks is a nonlinear combination, unlike the magnitude of Sobel. The direction is measured like the Sobel direction, from dark to bright with y pointing down.

Kernel or operator

The Inspector shows all eight masks, read from the code that turns them. The worked example below lists the eight responses for one window.

Algorithm

  1. Pad the image with the border mode.
  2. Correlate with each of the eight masks.
  3. Keep the largest response, or the direction of the mask that gives it.
  4. Map the result to 8 bits with the output mapping.

Parameters

Parameter Default Values Meaning
Output output Largest response Largest response, Direction of the largest response The strongest of the eight mask responses, or the direction of that mask.
Output mapping outputMapping Stretch to full range 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

compassFilter in web/src/core/filters/edges/compass.ts builds the eight masks with turnMask and keeps the largest of the eight correlations. 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} 20 & 20 & 200 \\ 20 & 20 & 200 \\ 20 & 20 & 200 \end{bmatrix} \]

The eight Kirsch responses for this window:

N NW W SW S SE E NE
-180 -1620 -1620 -1620 -180 1260 2700 1260

The largest, 2700, comes from the east mask, whose 5s lie on the bright right column. The filter gives 2700 and the direction 0 degrees, pointing right, from dark to bright.

Complexity

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

N is the number of pixels per channel. Eight 3x3 correlations per pixel.

Artifacts and limitations

Limitations

  • The direction has only eight values.
  • Large weights make it sensitive to noise.

Artifacts

  • Thick edges and a coarse direction map.

Tests

The reference turns the mask the same way and correlates with scipy.ndimage.correlate. Before it is used, the script checks the west and north-west masks against their published form and that the eight masks add up to 0. The test covers both outputs for all five border modes.

References

  • Kirsch, R. A. Computer determination of the constituent structure of biological images. Computers and Biomedical Research 4(3), 315 to 328, 1971. DOI 10.1016/0010-4809(71)90034-6.
  • Gonzalez, R. C. and Woods, R. E. Digital Image Processing, 4th edition. Pearson, 2018. Section 10.2.