Harris corners
A corner is a place where the image changes in every direction. The structure tensor's eigenvalues say exactly that.
- Build the structure tensor from image gradients
- Classify flat, edge and corner from its eigenvalues and the Harris response
- Run a local-maximum search without stepping outside the image
To match two photos we need points that can be found again: places that look different from all their neighbours. Slide a small window by \((u, v)\) and measure how much its contents change:
\[E(u, v) = \sum_{x, y} w(x, y)\,\big[I(x + u, y + v) - I(x, y)\big]^2 \approx \begin{pmatrix} u & v \end{pmatrix} M \begin{pmatrix} u \\ v \end{pmatrix}, \qquad M = \sum w \begin{pmatrix} I_x^2 & I_x I_y \\ I_x I_y & I_y^2 \end{pmatrix}\]The 2 × 2 structure tensor \(M\) summarises the gradients in the window. Its eigenvalues \(\lambda_1 \ge \lambda_2\) are the change along the steepest and the flattest direction:
- flat: both small — the window can move anywhere unnoticed;
- edge: one large, one small — sliding along the edge changes nothing;
- corner: both large — every move changes the window.
Harris avoids computing eigenvalues with a single score,
\[R = \det M - k\,(\operatorname{tr} M)^2 = \lambda_1 \lambda_2 - k(\lambda_1 + \lambda_2)^2,\]which is positive at corners, negative at edges and near zero on flat patches. The repository uses \(k = 0.06\). Its harrisResponseImage is a faithful implementation:
dIdx = cv2.Sobel(img, ddepth=cv2.CV_64F, dx=1, dy=0, ksize=3)
dIdy = cv2.Sobel(img, ddepth=cv2.CV_64F, dx=0, dy=1, ksize=3)
A = cv2.GaussianBlur(dIdx * dIdx, (3, 3), 1, 1) # weighted sum of Ix^2
B = cv2.GaussianBlur(dIdy * dIdy, (3, 3), 1, 1) # Iy^2
C = cv2.GaussianBlur(dIdx * dIdy, (3, 3), 1, 1) # Ix*Iy
response = (A * B - C * C) - 0.06 * (A + B) ** 2
(The debug windows labelled Ixx, Iyy and Ixy show dIdx, dIdy and dIdx again — a display slip only.)
Keypoints and edges — and the border
harrisKeypoints keeps pixels with \(R > 0.1\) that are the largest in their 3 × 3 neighbourhood; harrisEdges marks pixels with \(R < -0.01\) that are a minimum along \(x\) or \(y\). Both loop over every pixel and then read response[y + dy, x + dx] or response[y, x + 1]. Run on the Tsukuba image of lesson 9, both crash with an IndexError (index 288 is out of bounds for axis 0 with size 288) as soon as a border pixel passes the threshold — and Sobel’s reflected borders make that likely. (At the top and left edges, index -1 silently wraps around to the opposite side instead.) Starting the loops at 1 and stopping one short fixes both:
for y in range(1, height - 1):
for x in range(1, width - 1):
With that change the detector finds 214 corners at the default threshold (671 at 0.01, 40 at 0.5) and marks 4,997 edge pixels:
Note that the threshold 0.1 is absolute, while \(R\) scales with the fourth power of image contrast — the image is normalised to [0, 1] first, which is what makes a fixed threshold workable at all.
Try it yourself. In the animation, read off \(\lambda_1, \lambda_2\) at the corner and on an edge, and check \(R\) by hand. Then replace the threshold in harrisKeypoints by a fraction of the maximum, 0.01 * response.max(), and skip the min–max normalisation in main, and compare the corner counts on a dark and a bright version of the same image (gray * 0.5 and gray). Which threshold is contrast-independent?