analitics

Pages

Monday, April 15, 2019

Using the ORB feature from OpenCV python module.

Today I will show you a simple script using the ORB (oriented BRIEF), see C++ documentation / OpenCV.
The algorithm uses FAST in pyramids to detect stable keypoints, selects the strongest features using FAST or Harris response, finds their orientation using first-order moments and computes the descriptors using BRIEF (where the coordinates of random point pairs (or k-tuples) are rotated according to the measured orientation).
One good feature of ORB is the is rotation invariant and resistant to noise.
The ORB descriptor use the Center of the mass of the patch of the Moment (sum of x,y), Centroid (the result of the matrix of all moment) and Orientation ( the atan2 of moment one and two).
One good article about ORB can be found here.
Let's see the script code of this python example:
import cv2
import numpy as np 

image_1 = cv2.imread("1.png", cv2.IMREAD_GRAYSCALE)
image_2 = cv2.imread("2.png", cv2.IMREAD_GRAYSCALE)

orb = cv2.ORB_create()
kp1, des1 = orb.detectAndCompute(image_1,None)
kp2, des2 = orb.detectAndCompute(image_2,None)

bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
matches = bf.match(des1, des2)
matches = sorted(matches, key = lambda x:x.distance)

matching_result = cv2.drawMatches(image_1, kp1, image_2, kp2, matches[:150], None, flags=2)

cv2.imshow("Image 1", image_1)
cv2.imshow("Image 2", image_2)
cv2.imshow("Matching result", matching_result)
cv2.waitKey(0)
cv2.destroyAllWindows()
The script use two file images 1.png and 2.png.
The result is an image composed of the two on which the areas of similitude are traced as detected by the mathematical algorithm.