Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use surf and sift detector in OpenCV for Python

I was trying a code for feature matching which uses the function SURF(). Upon execution it gives an error saying "AttributeError: 'module' object has no attribute 'SURF'".

How can I download this module for Python (Windows) and fix this error?

like image 575
pallavi Avatar asked Nov 12 '15 11:11

pallavi


People also ask

Is SIFT available in OpenCV?

Note: SIFT detector has been removed in the latest versions of OpenCV ( ≥ 3.4. 2.16).


1 Answers

You can try ORB (Oriented FAST and Rotated BRIEF) as an alternate to SURF in open cv. It almost works as good as SURF and SIFT and it's free unlike SIFT and SURF which are patented and can't be used commercially.
You can read about it more in opencv-python documentation here

Here's the sample code for your ease

import cv2
from matplotlib import pyplot as plt

img1 = cv2.imread('text.png',cv2.COLOR_BGR2GRAY) # queryImage
img2 = cv2.imread('original.png',cv2.COLOR_BGR2GRAY) # trainImage
# Initiate SIFT detector
orb = cv2.ORB_create()

# find the keypoints and descriptors with SIFT
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()
like image 90
Ahmed Avatar answered Sep 27 '22 20:09

Ahmed