How to compare surf features in python opencv2.4

I have two images that I want to compare using python and opencv.

I figured out how to extract the surf features from a single image from this book: "Programming Vision Vision with Python."

I am extracting functions as follows:

import cv2 from numpy import * # read image im = cv2.imread('empire.jpg') # downsample im_lowres = cv2.pyrDown(im) # convert to grayscale gray = cv2.cvtColor(im_lowres,cv2.COLOR_RGB2GRAY) # detect feature points s = cv2.SURF() mask = uint8(ones(gray.shape)) keypoints = s.detect(gray,mask) # show image and points vis = cv2.cvtColor(gray,cv2.COLOR_GRAY2BGR) for k in keypoints[::10]: cv2.circle(vis,(int(k.pt[0]),int(k.pt[1])),2,(0,255,0),-1) cv2.circle(vis,(int(k.pt[0]),int(k.pt[1])),int(k.size),(0,255,0),2) cv2.imshow('local descriptors',vis) cv2.waitKey() 

Now, how can I compare key points with other sets of key points that come from a reference image using?

+4
source share
2 answers

There is a FLANN implementation for Python OpenCV, I used it myself, and it works very well. Initially, it was not easy to understand, but this question helped me a lot, see Esteban Angers answer.

You can also see my answer to this question , where I quickly explained the code. I repeat the explanation here.

 r_threshold = 0.6 FLANN_INDEX_KDTREE = 1 # bug: flann enums are missing 

Build a parameter dictionary:

 flann_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 4) flann = cv2.flann_Index(desc2, flann_params) 

Search for nearby neighbors:

 idx2, dist = flann.knnSearch(desc1, 2, params = {}) # bug: need to provide empty dict mask = dist[:,0] / dist[:,1] < r_threshold idx1 = np.arange(len(desc1)) pairs = np.int32( zip(idx1, idx2[:,0]) ) 

Returns descriptors that match:

 return pairs[mask] 
+6
source

Corresponding SURF descriptors are usually executed using k-nearest neighbors (with k = 2). Of course, for C ++, OpenCV has a built-in class for this - a quick approximate nearest neighbor socket descriptor ( FLANN_matcher ), although I can't seem to find any docs for Python versions. Maybe dig it up, see if you can will you find him?

If you need to do this from scratch, this post has a good code example using cv2.KNearest , which is definitely in the Python version.

+1
source

Source: https://habr.com/ru/post/1436602/


All Articles