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 changes to enhance the performance. To implement ORB feature detector and descriptors, you could follow the steps given below

  • 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 image 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. Use 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_KEYPOINTS as 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

In this Python program, we detect and compute keypoints and descriptors in the input image using ORB feature detector. We also draw the keypoints on the image and display it.

# import required libraries import cv2 # read input image img = cv2.imread('house.jpg') # convert the image to grayscale gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Initiate ORB object with default values orb = cv2.ORB_create(nfeatures=2000) # detect and compute the keypoints on image (grayscale) kp = orb.detect(gray, None) kp, des = orb.compute(gray, kp) # draw keypoints in image img1 = cv2.drawKeypoints(gray, kp, None, (0,0,255), flags=0) # display the image with keypoints drawn on it cv2.imshow("ORB Keypoints", img1) cv2.waitKey(0) cv2.destroyAllWindows()

Output

When we execute the above program, it will produce the following output window −


The keypoints are shown in red color.

Example

In this Python program too, we detect and compute keypoints and descriptors in the input image using ORB feature detector. We also draw the keypoints on the image and display it.

We use the flag cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS to draw keypoints.

# import required libraries import cv2 # read input image img = cv2.imread('house.jpg') # 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, des = orb.detectAndCompute(gray, None) # draw keypoints in image img1 = cv2.drawKeypoints(img, kp, None,(0,0,255), flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS) # display the image with keypoints drawn on it cv2.imshow("ORB Keypoints", img1) cv2.waitKey(0) cv2.destroyAllWindows()

Output

When we execute the above program, it will produce the following output window −


In the above output image, the keypoints are drawn with their size and orientation. We find and draw 50 feature keypoints.

Updated on: 05-Dec-2022

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements