Feature Detection and Description¶
Feature detection and description are important parts of many computer vision algorithms. Features can be various structures in the image such as edges, corners, and blobs of certain shapes and sizes. Descriptors are the mathematical representations of these features. They play a crucial role in many areas, such as image stitching, object recognition, and tracking.
import cv2
import matplotlib.pyplot as plt
import numpy as np
def display_image_in_notebook(img):
# Convert the image from BGR to RGB
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# Display the image using matplotlib
plt.imshow(img_rgb)
#if you want to remove the graduation on the axis
#plt.axis('off')
plt.show()
# Load our test image
img = cv2.imread('./data/frame_1.png', 1)
# Display the image in Jupyter notebook
display_image_in_notebook(img)
Harris Corner Detection¶
Corners are regions in the image with large variation in intensity in all the directions. The Harris Corner Detection algorithm is one of the simplest methods which can be implemented to find corners.
Here's how you can use OpenCV to apply Harris Corner Detection:
# Convert our original image to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Harris corner detection
dst = cv2.cornerHarris(gray, 2, 3, 0.04)
# Result is dilated for marking the corners
dst = cv2.dilate(dst, None)
# Threshold for an optimal value
img[dst > 0.01 * dst.max()] = [0, 0, 255]
# Display the image
display_image_in_notebook(img)
SIFT (Scale-Invariant Feature Transform)¶
The SIFT method is used for extracting distinctive invariant features from images that can be used to perform reliable matching between different views of an object or scene. Unfortunately, SIFT is patented and may not be included in all OpenCV installations.
# Convert it to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Create a SIFT object
sift = cv2.SIFT_create()
# Detect SIFT features, with no masks
keypoints, descriptors = sift.detectAndCompute(gray, None)
# Draw the keypoints
img = cv2.drawKeypoints(gray, keypoints, img)
# Display the image with keypoints
display_image_in_notebook(img)
FAST (Features from Accelerated Segment Test) Algorithm for Corner Detection¶
The FAST feature detector is a faster corner detection method which could be useful in real-time applications.
# Convert it to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Create a FAST object
fast = cv2.FastFeatureDetector_create()
# Detect FAST features
keypoints = fast.detect(gray, None)
# Draw the keypoints
img = cv2.drawKeypoints(gray, keypoints, img)
# Display the image with keypoints
display_image_in_notebook(img)
These feature detection and description techniques are widely used in various computer vision tasks, such as image matching, object detection, and more. Keep in mind that choosing the right method depends on the requirements of the specific task at hand.