#-*-coding:utf-8-*-import cv2 import NumPy as NP from find_obj import Filter_matches,explore_match from Matplotlib im Port Pyplot as Plt def getsift (): ' Get and view sift feature ' img_path1 = '. /.. /data/home.jpg ' #读取图像 img = cv2.imread (img_path1) #转换为灰度图 gray= cv2.cvtcolor (img,cv2. Color_bgr2gray) #创建sift的类 sift = Cv2. SIFT () #在图像中找到关键点 can also be computed in one step #kp, des = sift.detectandcompute KP = sift.detect (gray,none) print type (KP), type (kp[0 ] #Keypoint数据类型分析 http://www.cnblogs.com/cj695/p/4041399.html print kp[0].pt #计算每个点的sift des = Sift.compu Te (GRAY,KP) print type (KP), type (DES) #des [0] is the list,des[1 of the key point] for the matrix print type (Des[0]) of the eigenvector, type (des[1)) Prin
T des[0],des[1] #可以看出共有885个sift特征, each feature is 128 D print des[1].shape #在灰度图中画出这些点 img=cv2.drawkeypoints (GRAY,KP)
#cv2. Imwrite (' sift_keypoints.jpg ', img) plt.imshow (IMG), Plt.show () def matchsift (): ' Match sift features ' IMG1 = Cv2.imread ('.. /.. /data/Box.png ', 0) # Queryimage img2 = Cv2.imread ('. /.. /data/box_in_scene.png ', 0) # Trainimage sift = Cv2. SIFT () kp1, des1 = Sift.detectandcompute (IMG1, none) kp2, Des2 = Sift.detectandcompute (Img2, None) # Brute force matching algorithm, two parameters, Distance metric (L2 (default), L1), whether cross matching (false) BF = Cv2. Bfmatcher () #返回k个最佳匹配 matches = Bf.knnmatch (Des1, Des2, k=2) # CV2.DRAWMATCHESKNN expects list of lists as Mat
Ches. #opencv2.4.13 does not have the DRAWMATCHESKNN function, you need to put the common.py and find_obj files under Opencv2.4.13\sources\samples\python2 into the current directory and import P1, p2, Kp_pairs = Filter_matches (Kp1, KP2, Matches) explore_match (' Find_obj ', IMG1, Img2, kp_pairs) # CV2 shows image CV 2.waitKey () cv2.destroyallwindows () def matchSift3 (): ' Match sift feature ' IMG1 = Cv2.imread ('. /.. /data/box.png ', 0) # Queryimage img2 = Cv2.imread ('. /.. /data/box_in_scene.png ', 0) # Trainimage sift = Cv2.
SIFT () kp1, des1 = Sift.detectandcompute (IMG1, None) kp2, des2 = Sift.detectandcompute (Img2, none) # Brute force matching algorithm, with two parameters, distance metric (L2 (default), L1), whether cross matching (false) BF = Cv2. Bfmatcher () #返回k个最佳匹配 matches = Bf.knnmatch (Des1, Des2, k=2) # CV2.DRAWMATCHESKNN expects list of lists as Mat
Ches. #opencv3.0 has the DRAWMATCHESKNN function # Apply ratio test # ratio tests, first obtain the nearest point B (nearest) and C (near) to a distance, only when b/c # is less than the threshold (0.75) is considered to be a match, because the fake Set the match to be one by one corresponding, the ideal distance for the true match is 0 good = [] for M, N in matches:if m.distance < 0.75 * N.distance:go Od.append ([m]) IMG3 = CV2.DRAWMATCHESKNN (IMG1, Kp1, Img2, KP2, Good[:10], None, flags=2) Cv2.drawm plt.imshow ( IMG3), Plt.show () Matchsift ()