Monday, November 16, 2015

OpenCV: ORB - Oriented FAST and Rotated BRIEF - Python Example

#ORB detection
#OpenCV 3.0.0
#Raspberry Pi 2, Jessie
#An efficient alternative to SIFT or SURF

import numpy as np
import cv2
from matplotlib import pyplot as plt

img1 = cv2.imread('img1.jpg',0)        # this is the object you want to find
img2 = cv2.imread('img2.jpg',0)        # this is the "bigger picture"

# Initiate ORB detector
orb = cv2.ORB_create()

# Find the keypoints and descriptors with ORB
kp1, des1 = orb.detectAndCompute(img1,None)
kp2, des2 = orb.detectAndCompute(img2,None)

# create BFMatcher object
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)

# Match descriptors.
matches = bf.match(des1,des2)

# Sort them in the order of their distance.
matches = sorted(matches, key = lambda x:x.distance)

# Draw first 10 matches.
img3 = cv2.drawMatches(img1,kp1,img2,kp2,matches[:10],None, flags=2)

plt.imshow(img3),plt.show()

4 comments:

  1. This code no longer works in the latest OpenCV.

    ReplyDelete
    Replies
    1. Unlike most examples on the internet, I specifically stated in the comments section at the top exactly what hardware and software versions were used to make this project successful. It is inevitable that this example will become dated and obsolete, but at least someone can figure that out right from the start.

      Delete
  2. Excellent and simple.

    ReplyDelete

Be nice!