Harris corner detector

The Harris detector turns the second moment matrix of every pixel into a single cornerness score, then keeps the strongest local peaks. Follow a pixel through the steps, and see where it lands in the plane of the two eigenvalues.

Image II corners ellipse of M
1 · IxI_x derivative in x, 0 = gray
1 · IyI_y derivative in y, 0 = gray
2–3 · g(Ix2)g(I_x^2) squared, then Gaussian window
2–3 · g(Iy2)g(I_y^2) squared, then Gaussian window
2–3 · g(IxIy)g(I_x I_y) product, then window, 0 = gray
4 · Cornerness CC 0 = gray, square-root scale
5–6 · C≥tC \ge t and local maxima above t corners

From the second moment matrix to a score

The autocorrelation surface showed the idea: a window is distinctive if shifting it in any direction changes its content. The Harris detector builds on three approximations:

Instead of computing the eigenvalues at every pixel, Harris uses that the determinant is their product and the trace their sum:

C=λ1λ2−α(λ1+λ2)2=det⁡(M)−αtrace⁡(M)2C = \lambda_1 \lambda_2 - \alpha (\lambda_1 + \lambda_2)^2 = \det(M) - \alpha \operatorname{trace}(M)^2

with a small constant α\alpha, commonly 0.04 to 0.06. CC is large and positive at corners, negative on edges, where one eigenvalue dominates, and close to 0 in flat regions.

The algorithm

  1. Input: the image II. MM is computed at every pixel.
  2. Compute the image derivatives IxI_x and IyI_y, optionally after blurring the image (σD\sigma_D).
  3. Compute the entries of MM as products of the derivatives: Ix2I_x^2, Iy2I_y^2, IxIyI_x I_y.
  4. Filter each product with a Gaussian gg of width σ\sigma: this is the window function ww.
  5. Compute the cornerness with element-wise products ⊙\odot:
C=g(Ix2)⊙g(Iy2)−g(Ix⊙Iy)2−α[g(Ix2)+g(Iy2)]2C = g(I_x^2) \odot g(I_y^2) - g(I_x \odot I_y)^2 - \alpha \left[ g(I_x^2) + g(I_y^2) \right]^2
  1. Threshold CC to keep only high cornerness. Here tt is a fraction of the largest CC in the image.
  2. Non-maximum suppression: keep a pixel only if its CC is the largest in its neighborhood of 2r+12r + 1 pixels square.

Everything up to step 4 is linear filtering and pixel-wise arithmetic, which makes the detector fast.

The plane of eigenvalues

The plot next to the controls shows every pixel as a point (λ1,λ2)(\lambda_1, \lambda_2) with λ1≥λ2\lambda_1 \ge \lambda_2. Flat pixels crowd near the origin, edge pixels lie along the λ1\lambda_1 axis, and corners are the few points that also have a large λ2\lambda_2. The shaded regions are where C≥tC \ge t and C≤−tC \le -t. Their borders are lines of constant CC, so the classification depends on the eigenvalues only.

α\alpha sets how equal the eigenvalues must be. For t=0t = 0, a pixel is a corner if λ2/λ1\lambda_2 / \lambda_1 is above a ratio that grows with α\alpha: about 0.056 for α=0.05\alpha = 0.05. At α=0.25\alpha = 0.25, C=−(λ1−λ2)2/4≤0C = -(\lambda_1 - \lambda_2)^2 / 4 \le 0, and nothing is a corner anymore.

The ellipses

The red ellipse at the selected pixel is a line of constant [u   v] M [u   v]⊤[u \;\, v] \, M \, [u \;\, v]^\top. Its axes point along the eigenvectors and are proportional to λ−1/2\lambda^{-1/2}: small and round at corners, long and thin along edges, and huge (clipped here) in flat regions. Turn on the grid to see the ellipses all over the image.

Which corners survive a change of brightness, a rotation or a change of scale is the topic of Harris invariance.

Try this