See Corner Structure with Harris

Introduction to Corner Detection

In the first unit of this course, we outlined the image stitching pipeline and learned that to stitch images together, we must first find unique "landmarks" across our images.

Imagine trying to stitch together two pictures of a perfectly blank, blue sky. It would be nearly impossible because there are no unique reference points to tell you how the images align. However, if the images contain a highly textured brick building, the sharp edges and corners make alignment much easier.

In this second unit out of our five-unit journey, we will explore the Harris Corner Detector. The Harris algorithm is a "detector-only" method. It does not describe or match features across images; instead, it is a fantastic diagnostic tool. It shows us exactly where an algorithm finds corner-like structures. By building this tool, you will develop a strong intuition for why certain images are great for stitching, while others are prone to failure.

Preparing the Image Data

Before we can look for corners, we need to load and prepare our image. As a quick reminder from our previous discussions, we will use our custom cvkit helper library to read our image file and prepare it.

import cv2
import numpy as np
from cvkit import preprocess_for_features, read_color

image = read_color("sample_image.jpg")
gray = preprocess_for_features(image)

In this snippet, read_color safely loads our image. We then pass it to preprocess_for_features, which converts the image to grayscale. Corner detection relies on measuring drastic changes in light intensity (light to dark), so color information is not necessary and would only slow down our math.

Next, OpenCV's mathematical functions for finding corners require our grayscale image data to be in a specific format called a 32-bit float. We can convert our grayscale image using NumPy:

gray_float = np.float32(gray)

Now, our image is perfectly prepared for the Harris Corner Detector.

Applying the Harris Corner Detector

To find corners, we will use the cv2.cornerHarris() function provided by OpenCV. This function scans the image to find areas where the pixel intensity changes significantly in all directions — the classic definition of a corner.

response = cv2.cornerHarris(gray_float, blockSize=2, ksize=5, k=0.07)

Let us break down the parameters we just passed into the function:

  • gray_float: This is our prepared, 32-bit float grayscale image.
  • blockSize: This determines the size of the neighborhood the algorithm looks at. A value of 2 means it looks at a 2x2 pixel grid to detect corners.
  • ksize: This is the aperture parameter used to mathematically find the edges. A value of 5 is a standard starting point.
  • k: This is a mathematical parameter used to calculate the final "corner score." A standard value is 0.07.

The response we get back is not an image we can display. Instead, it is a map of scores. Every pixel gets a score indicating how "corner-like" it is.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal