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 implement ORB feature detectors in OpenCV Python?
ORB (Oriented FAST and Rotated BRIEF) is a fusion of FAST keypoint detector and BRIEF descriptors with many modifications to enhance performance. ORB is rotation invariant and resistant to noise, making it ideal for real-time applications like object recognition and image matching.
Steps to Implement ORB Feature Detector
To implement ORB feature detector and descriptors, follow these steps:
Import the required libraries OpenCV and NumPy. Make sure you have already installed them.
Read the input image using cv2.imread() method. Specify the full path of the image. Convert the input image to grayscale using cv2.cvtColor() method.
Initiate the ORB object with default values using orb=cv2.ORB_create().
Detect and compute the feature keypoints 'kp' and descriptor 'des' in the grayscale image using orb.detectAndCompute(). It returns keypoints 'kp' and descriptors 'des'.
Draw the detected feature keypoint kp on the image using cv2.drawKeypoints() function. To draw rich feature keypoints you can pass
flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTSas a parameter.Display the image with drawn feature keypoints on it.
Let's look at some examples to detect and draw keypoints in the input image using the ORB feature detector.
Input Image
We will use the following image as the input file in the examples below.

Example 1: Basic ORB Keypoint Detection
In this Python program, we detect and compute keypoints and descriptors in the input image using ORB feature detector. We draw the keypoints on the image and display it ?
# import required libraries
import cv2
import numpy as np
# create a sample image for demonstration
img = np.ones((300, 400, 3), dtype=np.uint8) * 255
cv2.rectangle(img, (50, 50), (150, 150), (0, 0, 0), 2)
cv2.rectangle(img, (200, 100), (350, 200), (0, 0, 0), 2)
cv2.circle(img, (100, 250), 30, (0, 0, 0), 2)
# convert the image to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Initiate ORB object with default values
orb = cv2.ORB_create(nfeatures=50)
# detect and compute the keypoints on image (grayscale)
kp = orb.detect(gray, None)
kp, des = orb.compute(gray, kp)
# draw keypoints in image
img_with_keypoints = cv2.drawKeypoints(gray, kp, None, (0,0,255), flags=0)
print(f"Number of keypoints detected: {len(kp)}")
print(f"Descriptor shape: {des.shape if des is not None else 'None'}")
Number of keypoints detected: 8 Descriptor shape: (8, 32)
Example 2: Rich Keypoint Visualization
In this example, we use the flag cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS to draw keypoints with their size and orientation information ?
# import required libraries
import cv2
import numpy as np
# create a sample image for demonstration
img = np.ones((300, 400, 3), dtype=np.uint8) * 255
cv2.rectangle(img, (50, 50), (150, 150), (0, 0, 0), 2)
cv2.rectangle(img, (200, 100), (350, 200), (0, 0, 0), 2)
cv2.circle(img, (100, 250), 30, (0, 0, 0), 2)
# convert the image to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Initiate ORB object with specific number of features
orb = cv2.ORB_create(nfeatures=20)
# detect and compute the keypoints using single method
kp, des = orb.detectAndCompute(gray, None)
# draw rich keypoints showing size and orientation
img_with_rich_keypoints = cv2.drawKeypoints(img, kp, None, (0,0,255),
flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
print(f"Number of keypoints detected: {len(kp)}")
print("Rich keypoints show size and orientation of features")
Number of keypoints detected: 8 Rich keypoints show size and orientation of features
ORB Parameters
ORB detector accepts several parameters to control feature detection:
nfeatures: Maximum number of features to retain (default: 500)
scaleFactor: Pyramid decimation ratio (default: 1.2)
nlevels: Number of pyramid levels (default: 8)
edgeThreshold: Size of border where features are not detected (default: 31)
firstLevel: Level of pyramid to put source image to (default: 0)
Key Points
ORB is rotation invariant and resistant to noise
Each ORB descriptor is 32 bytes (256 bits)
ORB combines FAST keypoint detector with BRIEF descriptor
Rich keypoints display orientation and scale information
ORB is faster than SIFT and SURF algorithms
Conclusion
ORB feature detector efficiently finds keypoints and computes descriptors for computer vision applications. Use cv2.ORB_create() to initialize the detector and detectAndCompute() to extract features in a single step.
