Shi-Tomasi Corner Detection Example with OpenCV

          Shi-Tomasi corner detection, also known as the Good Features to Track algorithm, is a method used in computer vision to identify key points or corners in an image. These corners are characterized by distinctive features that can be tracked from one image frame to another, making them valuable for various tasks like object tracking, image stitching, and motion analysis.

        In this blog post, we'll delve into Shi-Tomasi corner detection and its applications with OpenCV. The tutorial covers:

  1. The concept of Shi-Tomasi corner detection
  2. Shi-Tomasi corner detection with OpenCV 
  3. Conclusion

     Let's get started.


The concept of Shi-Tomasi corner detection

    Shi-Tomasi corner detection identifies important image points by analyzing intensity variations. Key features, like corners, are often characterized by significant intensity changes. It evaluates whether a pixel qualifies as a corner by examining the Eigenvalues of the Structure Tensor, a 2x2 matrix summarizing intensity gradients. Shi-Tomasi uniquely uses the minimum Eigenvalue to classify corners, unlike the Harris corner detector. If this minimum Eigenvalue exceeds a preset threshold, the pixel is considered a corner. Corners are further ranked based on a "cornerness score" with weaker ones discarded. A minimum distance parameter prevents close corner selection, ensuring a balanced distribution of keypoints in the image.


Shi-Tomasi corner detection with OpenCV

   In OpenCV, we can use the cv2.goodFeaturesToTrack() function to perform Shi-Tomasi corner detection. Here's a brief explanation of how it works:

# Detect corners using Shi-Tomasi
corners = cv2.goodFeaturesToTrack(input_image, max_corners, 
quality_level, min_distance)

    1. Input image: A grayscale image to detect corners.
    2. Parameters:

  • maxCorners: The maximum number of corners to detect. It will return the strongest corners up to this number.
  • qualityLevel: A value between 0 and 1 that represents the minimal accepted quality of corners. Higher values result in fewer corners.
  • minDistance: The minimum Euclidean distance between detected corners. Corners closer than this distance will be rejected as duplicates.
  • blockSize: Size of the neighborhood considered for corner detection. Larger values may detect larger corners.
  • useHarrisDetector: A Boolean flag indicating whether to use the Harris corner detection method instead of Shi-Tomasi. Harris is another corner detection technique.

    3. Output: The function returns a list of coordinates (x, y) representing the detected corners. 

     Now, let's explore the example below that demonstrates the implementation of Shi-Tomasi corners using OpenCV. In this example, we'll load input image file, convert it to grayscale and keep one copy of image to display the result. We define Shi-Tomasi corner detection parameters. Then, detect corners by using cv2.goodFeaturesToTrack() function. We convert corners to integers and draw circles at detected corner locations. Finally, we display detected corners on image.
import cv2
import numpy as np
import matplotlib.pyplot as plt

# Load the image in grayscale
image = cv2.imread('chess.jpg')
gray = cv2.imread('chess.jpg', cv2.IMREAD_GRAYSCALE)

# Define Shi-Tomasi corner detection parameters
max_corners = 100
quality_level = 0.01
min_distance = 10

# Detect corners using Shi-Tomasi
corners = cv2.goodFeaturesToTrack(gray, max_corners, 
quality_level, min_distance)

# Convert corners to integers
corners = np.int0(corners)

# Draw circles at detected corner locations
for corner in corners:
x, y = corner.ravel(), (x, y), 3, 255, -1)

plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.title("Shi-Tomasi Corners")

    In this tutorial, we've briefly explored the concepts of Shi-Tomasi corner detection and learned how to implement it with OpenCV. 
     Shi-Tomasi corner detection identifies significant image features (corners) by analyzing the Eigenvalues of the Structure Tensor. It does so with a criterion that considers only the minimum Eigenvalue and introduces parameters for quality control and minimum distance to fine-tune the corner detection process. This theory forms the basis for robust and reliable corner detection, making it a vital tool in various computer vision applications.

No comments:

Post a Comment