Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to detect and draw FAST feature points in OpenCV Python?
FAST (Features from Accelerated Segment Test) is a high-speed corner detection algorithm designed for real-time applications. OpenCV provides a simple interface to detect corner features using the FAST algorithm through cv2.FastFeatureDetector_create().
How FAST Algorithm Works
FAST detects corners by examining a circle of 16 pixels around each candidate point. If a continuous arc of pixels (usually 12 or more) are all brighter or darker than the center pixel by a threshold value, it's classified as a corner feature.
Steps to Detect FAST Features
To detect and draw feature points using the FAST detector, follow these steps ?
Import the required libraries OpenCV. Make sure you have already installed it.
Read the input image using
cv2.imread()and convert it to grayscale usingcv2.cvtColor().Create a FAST detector object with
cv2.FastFeatureDetector_create().Detect keypoints using
fast.detect(gray, None)method.Draw the detected keypoints using
cv2.drawKeypoints()function.Display the result image with keypoints.
Example 1: FAST with Non-Max Suppression Enabled
In this example, we detect corner features with the default non-max suppression enabled ?
import cv2
import numpy as np
# Create a sample image with corners
img = np.zeros((400, 400, 3), dtype=np.uint8)
img[50:150, 50:150] = [255, 255, 255] # White square
img[200:300, 200:300] = [128, 128, 128] # Gray square
img[250:350, 50:150] = [200, 200, 200] # Light gray square
# Convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Create FAST detector with default values
fast = cv2.FastFeatureDetector_create()
# Detect keypoints
keypoints = fast.detect(gray, None)
# Draw keypoints on the image
img_with_keypoints = cv2.drawKeypoints(img, keypoints, None, color=(0, 255, 0))
# Print detector parameters
print("Threshold:", fast.getThreshold())
print("Non-max Suppression:", fast.getNonmaxSuppression())
print("Neighborhood type:", fast.getType())
print("Total keypoints with non-max suppression:", len(keypoints))
Threshold: 10 Non-max Suppression: True Neighborhood type: 2 Total keypoints with non-max suppression: 16
Example 2: FAST with Non-Max Suppression Disabled
Here we disable non-max suppression to detect more keypoints ?
import cv2
import numpy as np
# Create the same sample image
img = np.zeros((400, 400, 3), dtype=np.uint8)
img[50:150, 50:150] = [255, 255, 255] # White square
img[200:300, 200:300] = [128, 128, 128] # Gray square
img[250:350, 50:150] = [200, 200, 200] # Light gray square
# Convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Create FAST detector and disable non-max suppression
fast = cv2.FastFeatureDetector_create()
fast.setNonmaxSuppression(False)
# Detect keypoints
keypoints = fast.detect(gray, None)
# Draw keypoints
img_with_keypoints = cv2.drawKeypoints(img, keypoints, None, color=(0, 0, 255))
# Print parameters
print("Threshold:", fast.getThreshold())
print("Non-max Suppression:", fast.getNonmaxSuppression())
print("Neighborhood type:", fast.getType())
print("Total keypoints without non-max suppression:", len(keypoints))
Threshold: 10 Non-max Suppression: False Neighborhood type: 2 Total keypoints without non-max suppression: 64
Understanding Non-Max Suppression
Non-max suppression reduces the number of detected keypoints by removing weaker responses near stronger ones. When disabled, FAST detects significantly more keypoints, which may include redundant or less reliable corner points.
Key Parameters
| Parameter | Description | Default Value |
|---|---|---|
| Threshold | Intensity difference threshold | 10 |
| Non-max Suppression | Remove adjacent weak responses | True |
| Neighborhood Type | Circle pattern (TYPE_9_16 or TYPE_7_12) | TYPE_9_16 (2) |
Conclusion
FAST is an efficient corner detection algorithm ideal for real-time applications. Use non-max suppression for cleaner results, or disable it when you need to detect all possible corner candidates.
