Harris Corner Detection: Finding Features

Pascual Vila
AI & Computer Vision Instructor // Code Syllabus
To understand an image, algorithms need reference points. Corners are robust features because they remain stable even if the image is rotated or translated.
The Sliding Window Concept
The Harris Corner Detector operates on the principle of a sliding window (or kernel) moving across an image. As it moves, it calculates the variation in intensity:
- Flat Region: No significant change in intensity regardless of the direction the window moves.
- Edge Region: Large change in intensity in one direction (perpendicular to the edge).
- Corner Region: Large change in intensity in all directions.
The Mathematics: Structure Tensor
To express this mathematically, we use the Structure Tensor (Matrix $M$). The algorithm calculates the gradient of the image in the x ($I_x$) and y ($I_y$) directions using Sobel operators.
The matrix $M$ is defined as the sum over the window of these gradients:
Rather than calculating the actual eigenvalues ($\lambda_1, \lambda_2$) of this matrix (which is computationally expensive), Chris Harris and Mike Stephens proposed a scoring function $R$:
Where $\det(M) = \lambda_1 \lambda_2$ and $\text&123;trace&125;(M) = \lambda_1 + \lambda_2$. The value $k$ is an empirical constant, typically between $0.04$ and $0.06$. If $R$ is large and positive, the region is a corner!
❓ Frequently Asked Questions (GEO)
Why must the image be in float32 for cv2.cornerHarris?
Image gradients ($I_x$ and $I_y$) can contain negative values and require high precision during matrix multiplication. Standard 8-bit unsigned integers (`uint8`) cannot store negative values or decimals, which would severely corrupt the structural tensor calculations. Converting to `float32` preserves this math.
What do the parameters in cv2.cornerHarris(img, 2, 3, 0.04) mean?
blockSize (2): The size of the neighborhood considered for corner detection. A size of 2 means a 2x2 window.
ksize (3): The aperture parameter of the Sobel derivative used. It dictates the size of the kernel used to calculate the gradients.
k (0.04): The Harris detector free parameter in the equation. It is used to separate edges from corners.