Corners are 'Interest Points' where intensity changes significantly in all directions. Detecting them is the key to motion tracking, 3D mapping, and image stitching.
1The Aperture Problem
Welcome to the world of feature extraction. We are hunting for anchors. In computer vision, corners provide a unique coordinate lock that allows algorithms to track movement, stitch panoramas, and build 3D maps.
Why corners and not edges? Think of a straight edge on a wall. If you look through a small window and slide along that edge, the view doesn't changeโyou can't tell you moved! This is the 'Aperture Problem'. But if you find the *corner* of the wall, moving in ANY direction immediately changes the view. A corner gives you a unique 2D lock.
# The Aperture Problem:
# Edge: Changes in 1 direction (Ambiguous)
# Flat: Changes in 0 directions (Useless)
# Corner: Changes in ALL directions (Perfect Lock!)2The Harris Mathematics
To find these corners mathematically, we use the Harris Corner Detector. Chris Harris proposed a brilliant method: slide a window across the image and calculate the gradient (intensity change). By creating a 'Second Moment Matrix', the algorithm calculates eigenvalues that summarize the intensity changes in both the X and Y axes simultaneously.
The Harris algorithm computes a response score, 'R', for every pixel. If 'R' is close to zero, the region is flat. If 'R' is strongly negative, it's an edge. But if 'R' is a large positive number, we've found our corner! We run cv2.cornerHarris, which returns this map of R-scores across the entire image.
import cv2
import numpy as np
# Harris requires Grayscale and Float32 formatting
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = np.float32(gray)
# Execute Harris Detector
# Params: image, blockSize, kSize, k-factor
dst = cv2.cornerHarris(gray, 2, 3, 0.04)3Thresholding Elite Anchors
To actually isolate the strongest corners, we perform thresholding. We dilate the result slightly for visibility, and then say: 'Only keep pixels where the R-score is greater than 1% of the absolute maximum R-score found in the image.'
By filtering out the weak scores, we discard background noise and keep only the elite anchor points. We then paint these elite pixels red to visualize our anchors.
# Dilate for easier human viewing
dst = cv2.dilate(dst, None)
# Threshold: Keep only the top 1% of peaks
img[dst > 0.01 * dst.max()] = [0, 0, 255]4Shi-Tomasi Optimization
While Harris is brilliant, it has flaws. The most notable is the 'k' parameter, which requires manual tuning and can be unpredictable. To fix this, researchers introduced the Shi-Tomasi Corner Detector. It simplifies the math by just taking the minimum of the two eigenvalues. If that minimum is above a threshold, it's a corner.
OpenCV implements Shi-Tomasi via the function cv2.goodFeaturesToTrack(). This function is vastly superior for engineering workflows because it gives you absolute control. You explicitly tell it exactly how many corners you want (e.g., 25), the minimum quality level, and crucially, the minimum Euclidean distance allowed between detected corners.
# Extracting the best 25 corners via Shi-Tomasi
# Params: img, maxCorners, qualityLevel, minDistance
corners = cv2.goodFeaturesToTrack(gray, 25, 0.01, 10)5Spatial Distribution
Why is the minDistance parameter so important? Because corners tend to clump together in highly textured areas (like tree leaves), starving the rest of the image of tracking points.
By enforcing a minimum distance, Shi-Tomasi forces the algorithm to spread the anchors evenly across the entire image. This distributed set of landmarks results in vastly more stable tracking for applications like optical flow and 3D reconstruction.
# Enforcing a minimum distance of 10 pixels
# prevents corner clustering.
import numpy as np
corners = np.int0(corners)
for i in corners:
x, y = i.ravel()
cv2.circle(img, (x,y), 3, 255, -1)