Skip to content

Harris corner detector

Fact Value
Also called Harris-Stephens detector, Plessey corner detector
Status Implemented and tested
Family Texture, corners, blobs and features
Engine Browser
Type Nonlinear, spatial, structure tensor and Harris measure
Output Image
Operator shown as algorithm (exact)
Support radius 1 + floor(4 sigma + 0.5), plus the minimum distance for the corners output
Code web/src/core/filters/features/corners.ts, harrisFilter
Operator code web/src/core/filters/features/corners.ts, cornerResponse
Channels Brightness only, RGB

Overview

The Harris corner detector finds the pixels where the brightness changes strongly in every direction. Such points can be found again in another view, so they are used for tracking, matching and alignment. The filter shows the Harris response at every pixel, or the corner points themselves.

Three panels: a checkerboard, the Harris response with bright spots where four squares meet, and the corner points as nine white dots at those places. Three panels: a checkerboard, the Harris response with bright spots where four squares meet, and the corner points as nine white dots at those places.
Harris on a checkerboard. The response peaks where four squares meet, and the corner points mark those peaks.

How it works

  1. Compute the change in x and in y at every pixel.
  2. Collect the squares and the product of the two changes in a Gaussian window. This is the structure tensor.
  3. Score each pixel by det A - k (tr A)^2.
  4. For corner points, keep the local maxima above the threshold.

Mathematical foundation

\[ \begin{gathered} A = G_{\sigma} * \begin{pmatrix} I_x^2 & I_x I_y \\ I_x I_y & I_y^2 \end{pmatrix},\qquad R = \det A - k\,(\operatorname{tr} A)^2 \end{gathered} \]

The formula shows the default parameter values.

where

  • \(A\): The structure tensor
  • \(I_x, I_y\): The Sobel derivatives
  • \(G_{\sigma}\): The Gaussian window
  • \(k\): The Harris parameter

I_x and I_y are the Sobel derivatives of the image on a 0 to 1 scale. G_sigma * blurs each entry of the matrix with a Gaussian, so A sums the gradients around each pixel. A has two large eigenvalues at a corner, one at an edge and none in a flat area. Harris and Stephens used det A - k (tr A)^2, which is large when both eigenvalues are large and negative along edges. The corners output marks pixels whose response is at least the threshold and at least every response within the minimum distance. Plateaus keep every pixel. This is the rule of the filter, and it differs from skimage.feature.corner_peaks, which also drops corners near the border.

Moving a small window across a flat area changes nothing. Along an edge, only moves across the edge change the window. At a corner, every move does. The structure tensor A sums the outer products of the gradient in the window, and its two eigenvalues measure the change in the strongest and weakest direction. Harris and Stephens avoided the eigenvalues with det A - k (tr A)^2. It is positive when both eigenvalues are large, negative when only one is, and near 0 when both are small.

Kernel or operator

There is no single kernel. The Inspector lists the stages with the current sigma and k. The Sobel and Gaussian kernels are on their own pages.

Algorithm

  1. Extend the image by the support with the border mode and scale it to 0 to 1.
  2. Compute the Sobel derivatives and the three products.
  3. Blur each product with the Gaussian to get the structure tensor.
  4. Compute R = det A - k (tr A)^2.
  5. For corner points, keep the local maxima above the threshold, and cut the extension off.

Parameters

Parameter Default Values Meaning
Sigma sigma 1 0.5 to 5 px The size of the Gaussian window over which the gradients are collected. It ends at 4 sigma.
k k 0.05 0.01 to 0.25 The weight of the squared trace in the response, as in scikit-image.
Output output Corner response Corner response, Corner points The response at every pixel, or the corner points as white pixels.
Threshold threshold 0.01 0 to 10 The smallest response that counts as a corner. The response is computed on a 0 to 1 scale.
Minimum distance distance 3 1 to 20 px A corner must have the largest response within this many pixels in x and in y.
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

cornerResponse in web/src/core/filters/features/corners.ts scales the plane to 0 to 1 and computes the Sobel derivatives. It blurs their products with a Gaussian cut at 4 sigma, as skimage.feature.structure_tensor does. The filter extends the plane once by the full support with the border mode, runs these stages and cuts the extension off. Then cornerPeaks keeps the pixels whose response is at least the threshold and the largest within the minimum distance. 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 channel scope, alpha and the output mapping. By default it works on luma.

Worked example

Pixel x y Response
Flat area 3 3 0
Edge 10 17 -0.6295
Corner 10 10 2.4246

A 21 x 21 image, 50 everywhere except a bright quadrant of 200 from x = 10 and y = 10, with sigma 1 and k = 0.05. The image is read on a 0 to 1 scale. The Harris response is 0 in the flat area, negative on the edge, where only one direction changes, and positive at the corner.

Complexity

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

N is the number of pixels per channel and r the radius of the Gaussian, which is separable.

Artifacts and limitations

Limitations

  • Not scale invariant. One sigma fits one corner size.
  • The value of k changes the balance between corners and edges.

Artifacts

  • Clusters of responses along strongly curved edges.

Tests

The reference runs scipy.ndimage.sobel and scipy.ndimage.gaussian_filter on the extended plane and scipy.ndimage.maximum_filter for the peaks. Before the cases are written, the script checks the response against skimage.feature.corner_harris on a larger random image away from the border, where both border rules give the same values. It also checks that a bright square gives exactly four corner points, one next to each of its corners. The cases cover the response and the corner points for all five border modes.

References