Corner Detection

Learn to detect corners in images with template matching, Harris Corner Detection, and Good Features to Track.

Corners are salient features in an image that we might want to find to locate or track an object from frame to frame in a video sequence. Corners are peculiar points having a high rate of change in two perpendicular directions.

We’ll study the image shown by the code widget below. We must convert the image to grayscale since the corner detection functions accept grayscale images as input.

Press + to interact
import cv2
import numpy as np
import copy
original_img = cv2.imread('./images/paper/book_draw1.jpg')
grayscale_img = cv2.cvtColor(original_img, cv2.COLOR_BGR2GRAY)
cv2.imwrite('./output/0_original.png', original_img)
cv2.imwrite('./output/1_grayscale.png', grayscale_img)

Our task for the rest of this lesson will be to locate the four book corners.

Press + to interact
The book corners we want to locate
The book corners we want to locate

We’ll explore three techniques to detect corners:

  • Template matching

  • Harris Corner Detection

  • Good Features to Track

Template matching

The simplest way to detect corners is to use template matching with a synthetic template. The four corners will each have their template, with a white square in the corner of a black square.

Press + to interact
Example of a blurred template to detect the northeast corner
Example of a blurred template to detect the northeast corner

Template matching of the grayscale image with templates of the four corners will highlight the light book corners on the dark background.

Let’s define the match_corner()­ function that we’ll ...