Harris Corner Detection Example with OpenCV

     The Harris corner detector algorithm is an image processing technique used to detect corners or special points in an image. It analyzes the intensity variations in small local regions of the image and assigns a corner response score to each pixel. Higher scores indicate the likelihood of a corner at that pixel.

    The Harris corner detector is widely used in computer vision for various applications, including feature matching, image alignment, object tracking, panoramic image stitching, camera calibration, and SLAM. Its ability to find distinctive keypoints makes it essential for tasks involving matching and tracking points in images and videos.

    In this tutorial, we'll explore the Harris corner detector algorithm and how to implement it with OpenCV's function. The tutorial covers:

  1. Harris corner algorithm 
  2. Detecting Harris corners with OpenCV

     Let's get started.

Harris corner algorithm     

    The Harris corner detector algorithm is a powerful image processing technique designed to identify corners or special points in an image. By analyzing intensity variations in small local regions, the algorithm assigns a corner response score to each pixel, indicating the likelihood of a corner at that location. This approach enables accurate corner detection in images, making it a valuable tool in various computer vision applications.

    The Harris corner detector algorithm involves the following steps:

  1. The input image is converted to grayscale, and Gaussian blurring can be optionally applied to reduce noise.
  2. The horizontal and vertical derivatives of the image are computed using Sobel operators, representing the intensity changes in the x and y directions at each pixel.
  3. The structure tensor is calculated for each pixel. The structure tensor is a matrix that describes the local structure of the image around each pixel. It is computed from the gradients and is used to measure the variations in intensity within a local neighborhood.
  4. The corner response function is used to determine whether a pixel is a corner or not. It is based on the eigenvalues of the structure tensor. High eigenvalues indicate corners, while low eigenvalues indicate flat or edge regions.
  5. Non-maximum suppression is applied to the corner response map to identify local maxima, indicating the corners.
  6. The corner response map is thresholded to retain only significant corners with a response score above a certain threshold, eliminating weak corners. This helps to remove weak corners and retain only the significant ones.


Detecting Harris corners with OpenCV
   In this part of tutorial, we use OpenCV's cv2.cornerHarris() function to detect Harris corners in an image. First we load required libraries and image file to use. Then we convert image to grayscale, and apply blurring to decrease noise level. 
    We detect Harris corners with cv2.cornerHarris() function. Here, the blockSize and ksize parameters control the size of the neighborhood considered for corner detection and the Sobel operator size, respectively. The k parameter adjusts the sensitivity of the corner detection.  
     Next, we can filter the corners to get strongest ones and mark them win red color. You can change the threshold value based on your targets. Finally, we display the output image.

import cv2
import numpy as np
import matplotlib.pyplot as plt

file = "/Users/user/Desktop/tmp/eagle.png"

# Load the image
image = cv2.imread(file)

# Convert the image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Apply Gaussian blur to remove noise
gray = cv2.GaussianBlur(gray, (5, 5), 0)

# Detect Harris corners
harris_corners = cv2.cornerHarris(gray, blockSize=2, ksize=3, k=0.04)

# Threshold the corners to get the strongest ones
threshold = 0.01 * harris_corners.max()
corner_image = np.copy(image)
# Mark the corners in red
corner_image[harris_corners > threshold] = [0, 0, 255
# Display the image with Harris corners marked in red
plt.figure(figsize = (16, 12))
plt.imshow(cv2.cvtColor(corner_image, cv2.COLOR_BGR2RGB))

    In summary, the Harris corner detector helps us to find special points in an image where there are big changes in brightness, like at the corners of objects. These special points are useful for many computer vision tasks, like recognizing objects and matching images together. 
    In this tutorial, we've briefly explored Harris corner detection algorithm and learned how to apply it with OpenCV API in Python. 
Video tutorial


No comments:

Post a Comment