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.
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:
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.
Let us break down the parameters we just passed into the function:
gray_float: This is our prepared,32-bit floatgrayscaleimage.blockSize: This determines the size of the neighborhood the algorithm looks at. A value of2means it looks at a2x2pixel grid to detect corners.ksize: This is the aperture parameter used to mathematically find the edges. A value of5is a standard starting point.k: This is a mathematical parameter used to calculate the final "corner score." A standard value is0.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.
