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:
- distinctiveness is approximated by the local autocorrelation ,
- the autocorrelation is approximated by the second moment matrix, ,
- cornerness is a function of the eigenvalues of : a corner has two large eigenvalues.
Instead of computing the eigenvalues at every pixel, Harris uses that the determinant is their product and the trace their sum:
with a small constant , commonly 0.04 to 0.06. is large and positive at corners, negative on edges, where one eigenvalue dominates, and close to 0 in flat regions.
The algorithm
- Input: the image . is computed at every pixel.
- Compute the image derivatives and , optionally after blurring the image ().
- Compute the entries of as products of the derivatives: , , .
- Filter each product with a Gaussian of width : this is the window function .
- Compute the cornerness with element-wise products :
- Threshold to keep only high cornerness. Here is a fraction of the largest in the image.
- Non-maximum suppression: keep a pixel only if its is the largest in its neighborhood of 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 with . Flat pixels crowd near the origin, edge pixels lie along the axis, and corners are the few points that also have a large . The shaded regions are where and . Their borders are lines of constant , so the classification depends on the eigenvalues only.
sets how equal the eigenvalues must be. For , a pixel is a corner if is above a ratio that grows with : about 0.056 for . At , , and nothing is a corner anymore.
The ellipses
The red ellipse at the selected pixel is a line of constant . Its axes point along the eigenvectors and are proportional to : 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
- Select a pixel on a straight side of a rectangle and one on a corner, and compare their points in the eigenvalue plane and their ellipses.
- Look at the cornerness : corners are bright spots, while edges are dark, negative lines.
- Increase towards 0.25: the corner region in the plane narrows towards the diagonal, and corners whose eigenvalues differ a lot drop out.
- Add noise with and set the threshold to 0: hundreds of local maxima appear in the flat regions. A threshold of 0.01 brings back the corners of the clean image.
- Increase the window to 5: the window now covers several squares of the checkerboard, and only its inner corners remain.
- Set the blur to 0 with noise: the derivatives get much noisier, but the Gaussian window still averages most of it away.
- Turn on the ellipses on a grid: along the sides of the shapes they are long and thin and follow the edge, on the checkerboard they are small and round.