Computer Vision Corner Detection

A corner is a point where two edges meet. Corners make excellent landmarks because they look unique and stay recognizable when the camera moves. Vision software finds corners first and uses them for tracking, alignment, and 3D measurement.

Why Corners Beat Flat Areas and Edges

Imagine looking at a photo through a tiny square window and sliding the window around.

Flat area        Edge               Corner
+-------+       +---+---+         +---+---+
|       |       |   |   |         |   |   |
|       |       |   |   |         |---+   |
+-------+       |   |   |         |       |
                +---+---+         +-------+
Slide any way:  Slide along edge:  Slide any way:
nothing changes nothing changes    something changes
  • Sliding over a flat area shows no change in any direction
  • Sliding along an edge shows no change in one direction
  • Sliding at a corner shows change in every direction

Corner detectors measure this change and mark places where it peaks.

Harris Corner Detector

Chris Harris and Mike Stephens published their detector in 1988. The method computes image gradients inside a small window and builds a score that rewards change in two directions. A high score means corner, a score near zero means flat area, and a strongly negative score means edge.

Score R
 large positive  -> corner
 near zero       -> flat area
 large negative  -> edge
import cv2
import numpy as np

img = cv2.imread("building.jpg")
gray = np.float32(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY))

harris = cv2.cornerHarris(gray, blockSize=2, ksize=3, k=0.04)
harris = cv2.dilate(harris, None)
img[harris > 0.01 * harris.max()] = [0, 0, 255]

Parameter Guide

  • blockSize sets the window size; larger windows find broader corners
  • ksize sets the gradient filter size
  • k usually sits between 0.04 and 0.06
  • The final threshold decides how many corners survive

Shi-Tomasi: Good Features to Track

Jianbo Shi and Carlo Tomasi improved the scoring rule in 1994. The method keeps the strongest corners and enforces a minimum distance between them, so points spread across the image without clustering.

corners = cv2.goodFeaturesToTrack(gray, maxCorners=100,
                                  qualityLevel=0.01, minDistance=10)
for c in corners.astype(int):
    x, y = c.ravel()
    cv2.circle(img, (x, y), 4, (0, 255, 0), -1)

Optical flow trackers use these points as starting positions.

FAST: Speed First

FAST, short for Features from Accelerated Segment Test, checks a ring of 16 pixels around a candidate. A pixel counts as a corner when a long run of ring pixels is all brighter or all darker than the center.

      . . .
    .       .
   .    P    .       P is a corner if 9+ ring pixels in a row
    .       .        are much brighter or darker than P
      . . .
fast = cv2.FastFeatureDetector_create(threshold=25)
keypoints = fast.detect(gray, None)
out = cv2.drawKeypoints(img, keypoints, None, color=(255, 0, 0))

FAST runs much faster than Harris, which makes it a favorite for live video and phones.

Refining Corner Positions

Detectors report whole-pixel positions. Sub-pixel refinement nudges each corner to a precise decimal position, which improves camera calibration and measurement.

criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
refined = cv2.cornerSubPix(gray, np.float32(corners), (5, 5), (-1, -1), criteria)

Comparing the Detectors

Detector       Speed     Strength                  Typical use
-------------  --------  ------------------------  -----------------------
Harris         medium    classic, reliable         teaching, simple tasks
Shi-Tomasi     medium    evenly spread points      optical flow tracking
FAST           fastest   real-time performance     mobile and video apps

Limits of Corner Detection

  • Plain corners do not change with rotation but shift when the scale changes
  • Noisy images produce false corners, so blur lightly before detection
  • Smooth, textureless surfaces offer no corners at all

Scale-aware detectors such as SIFT and ORB build on corner ideas and handle size changes.

Practice Exercise

Photograph a chessboard or a tiled floor. Run Harris and Shi-Tomasi on the same image and count the corners each method returns. Lower the threshold in small steps and watch false points appear on noise. Compare the speed of FAST against Harris by timing both on a large photo.

Corners in Real Projects

A robot vacuum spots corners of furniture to estimate its position. A video stabilizer tracks corners from frame to frame and cancels camera shake. A photo-stitching tool anchors overlapping pictures on shared corners. Each use needs points that appear again and again in the same place, and corners deliver that stability.

Choosing Settings for Your Scene

  • Increase the quality level when you want only the strongest corners
  • Raise the minimum distance when points bunch up around a busy texture
  • Apply a light blur when sensor noise creates scattered false points
  • Resize huge images first to save time without losing important corners