OpenCV 2.4.1 - computing SURF descriptors in Python -
i'm trying update code use cv2.surf()
opposed cv2.featuredetector_create("surf")
, cv2.descriptorextractor_create("surf")
. i'm having trouble getting descriptors after detecting keypoints. what's correct way call surf.detect
?
i tried following opencv documentation, i'm little confused. says in documentation.
python: cv2.surf.detect(img, mask) → keypoints¶ python: cv2.surf.detect(img, mask[, descriptors[, useprovidedkeypoints]]) → keypoints, descriptors
how pass keypoints in when making second call surf.detect
?
i not sure whether understand questions correctly. if looking sample of matching surf keypoints, simple , basic 1 below, similar template matching:
import cv2 import numpy np # load images img =cv2.imread('messi4.jpg') # convert them grayscale imgg =cv2.cvtcolor(img,cv2.color_bgr2gray) # surf extraction surf = cv2.surf() kp, descritors = surf.detect(imgg,none,useprovidedkeypoints = false) # setting samples , responses knn samples = np.array(descritors) responses = np.arange(len(kp),dtype = np.float32) # knn training knn = cv2.knearest() knn.train(samples,responses) # loading template image , searching similar keypoints template = cv2.imread('template.jpg') templateg= cv2.cvtcolor(template,cv2.color_bgr2gray) keys,desc = surf.detect(templateg,none,useprovidedkeypoints = false) h,des in enumerate(desc): des = np.array(des,np.float32).reshape((1,128)) retval, results, neigh_resp, dists = knn.find_nearest(des,1) res,dist = int(results[0][0]),dists[0][0] if dist<0.1: # draw matched keypoints in red color color = (0,0,255) else: # draw unmatched in blue color print dist color = (255,0,0) #draw matched key points on original image x,y = kp[res].pt center = (int(x),int(y)) cv2.circle(img,center,2,color,-1) #draw matched key points on template image x,y = keys[h].pt center = (int(x),int(y)) cv2.circle(template,center,2,color,-1) cv2.imshow('img',img) cv2.imshow('tm',template) cv2.waitkey(0) cv2.destroyallwindows()
below results got (copy pasted template image on original image using paint):
as can see, there small mistakes. startup, hope ok.
Comments
Post a Comment