In the realm of image processing and computer vision, corner detection plays a crucial role. It helps identify important points in images that have significant intensity changes in multiple directions. One popular and efficient method for corner detection is the Harris corner detection algorithm, developed by Chris Harris and Mike Stephens in 1988. This algorithm is widely recognized for its effectiveness and reliability.
In this Answer, we will explore the inner workings of the Harris Corner Detection algorithm and explain its mathematical foundations and implement it using the OpenCV library.
Understanding the Harris Corner Detection algorithm requires familiarity with the fundamental concepts of intensity and gradient in an image:
Intensity: Intensity represents the brightness or color information at a specific point in the image. It is mathematically denoted as
Gradient: The gradient represents the direction of maximum intensity change at a given point. It is a two-dimensional vector whose components are partial derivatives of the intensity along the
At its core, the Harris algorithm determines the variation in intensity for a displacement
Here,
In the Harris algorithm, corners are detected by maximizing the function
Where the matrix
The derivatives
The Harris algorithm introduces a corner score, or a specific mathematical criterion, to decide whether a region contains a corner. This corner score
The determinant of the matrix
The relative magnitudes of the eigenvalues
Flat region: If
Edge: If
Corner: If
This is illustrated below:
cv2.cornerHarris()
The cv2.cornerHarris()
function is the Harris Corner Detector provided by the OpenCV library.
corners = cv2.cornerHarris(src, blockSize, ksize, k)
It has the following parameters:
src
: The source image. The image must be grayscale
and its type should be float32
.
blockSize
: The size of the neighborhood considered for corner detection.
ksize
: The aperture parameter of the Sobel derivative used. This is the size of the Sobel kernel used to get derivates of the image.
k
: The Harris detector free parameter in the equation k
is usually in the range of 0.04 - 0.06.
The function returns the corner map (an image with corner intensities). Corners in the returned image are marked with large positive values.
Here is a step-by-step algorithm for the Harris Corner Detection to implement it using the OpenCV library.
Load the input image.
Convert the image to grayscale as corner detection relies on intensity values.
Normalize pixel values to a range of 0 to 255.
Apply the cv2.cornerHarris()
function to detect corners.
Dilate corner markers for better visualization.
Superimpose detected corners on the original image.
Here's how we can implement the Harris Corner Detection algorithm in Python using OpenCV:
# Import necessary libraries import cv2 import numpy as np # Load image input_image = cv2.imread('input.jpg') # Convert to grayscale gray_image = cv2.cvtColor(input_image, cv2.COLOR_BGR2GRAY) # Normalize to 8-bit gray_image = np.float32(gray_image) # Apply cv2.cornerHarris() function harris_corners = cv2.cornerHarris(gray_image, 2, 3, 0.04) # Dilate corners for better marking harris_corners = cv2.dilate(harris_corners, None) # Define a threshold for extracting large corners threshold = 0.01 * harris_corners.max() # Iterate through all the corners and draw them for i in range(harris_corners.shape[0]): for j in range(harris_corners.shape[1]): if harris_corners[i, j] > threshold: # Draw a red circle at each corner cv2.circle(input_image, (j, i), 5, (0, 0, 255), -1) # Display the image with corners cv2.imshow('Harris Corners', input_image) cv2.waitKey(0) cv2.destroyAllWindows()
Here’s a line-by-line explanation of the given Python script:
Lines 1–2: Import the necessary libraries. cv2
is the OpenCV library used for image processing, and numpy
is used for numerical operations.
Line 6: Read the image using the cv2.imread()
function. Replace ‘input.jpg’ with the path to the image you want to process.
Line 9: Convert the color image to grayscale using the cv2.cvtColor()
function. The Harris corner detection method works with grayscale images.
Line 12: Convert the grayscale image to a float32
data type. This is a requirement for the cv2.cornerHarris()
function used later.
Line 15: Apply the cv2.cornerHarris()
function to the grayscale image to detect corners. The arguments include the image, the size of the neighborhood considered for corner detection (2 in this case), the Sobel aperture parameter (3 here), and the Harris detector free parameter (0.04 here).
Line 18: Apply the cv2.dilate()
function to the corners detected by the Harris detector. This step is used to enhance the corner points.
Line 21: Define a threshold
for the corners. Only corners with a Harris response greater than the threshold are considered. The threshold is defined as 1% of the maximum response.
Lines 24–28: Iterate through all the pixels in the harris_corners
matrix. If the response at a pixel location is greater than the threshold, draw a circle at that location in the original image. The cv2.circle()
function takes the image, the center of the circle, the radius of the circle, the color of the circle (red in this case), and the thickness (which is -1 to fill the circle) as arguments.
Lines 31–33: Display the image with the detected corners using cv2.imshow()
. Wait for a key press using cv2.waitKey(0)
. Then, close all windows with cv2.destroyAllWindows()
.
The Harris corner detection algorithm is an invaluable tool in computer vision, allowing efficient detection and marking of corners within images. Through a combination of Python and OpenCV, we can implement this powerful algorithm to perform tasks ranging from image recognition to 3D reconstruction, opening doors to a broad spectrum of applications.
Harris corner detection
Determines whether a region is flat, an edge or a corner based on the magnitude of the score ‘R’
Eigenvalues of matrix M
Python OpenCV function to apply the Harris corner detection
The function E(u,v)
Mathematical function to find the difference in intensity for a displacement of (u,v) in all directions
cv2.cornerHarris()
Corner detection algorithm that uses variations of intensity to identify corners within an image
Flat region, Edge, and Corner
and used to calculate the corner score ‘R’ in the Harris algorithm
Free Resources