1- #-*-coding:utf8-*-#
1+ # -*-coding:utf8-*-#
22__author__ = 'play4fun'
33"""
44create time:15-11-9 下午1:28
5+ 对 SIFT 描述符进行蛮力匹配和比值测试
56"""
67
7-
8-
98import numpy as np
109import cv2
1110from matplotlib import pyplot as plt
12- img1 = cv2 .imread ('../data/box.png' ,0 )
11+
12+ img1 = cv2 .imread ('../data/box.png' , 0 )
1313# queryImage
14- img2 = cv2 .imread ('../data/box_in_scene.png' ,0 ) # trainImage
14+ img2 = cv2 .imread ('../data/box_in_scene.png' , 0 ) # trainImage
1515# Initiate SIFT detector
1616# sift = cv2.SIFT()
1717sift = cv2 .xfeatures2d .SIFT_create ()
18- #TODO
18+
1919
2020# find the keypoints and descriptors with SIFT
21- kp1 , des1 = sift .detectAndCompute (img1 ,None )
22- kp2 , des2 = sift .detectAndCompute (img2 ,None )
21+ kp1 , des1 = sift .detectAndCompute (img1 , None )
22+ kp2 , des2 = sift .detectAndCompute (img2 , None )
23+
2324# BFMatcher with default params
2425bf = cv2 .BFMatcher ()
25- matches = bf .knnMatch (des1 ,des2 , k = 2 )
26+ matches = bf .knnMatch (des1 , des2 , k = 2 )
27+
2628# Apply ratio test
2729# 比值测试,首先获取与 A距离最近的点 B (最近)和 C (次近),
2830# 只有当 B/C 小于阀值时(0.75)才被认为是匹配,
29- #因为假设匹配是一一对应的,真正的匹配的理想距离为0
31+ # 因为假设匹配是一一对应的,真正的匹配的理想距离为0
3032good = []
31- for m ,n in matches :
32- if m .distance < 0.75 * n .distance :
33+ for m , n in matches :
34+ if m .distance < 0.75 * n .distance :
3335 good .append ([m ])
3436
3537# cv2.drawMatchesKnn expects list of lists as matches.
36- img3 = np .ndarray ([2 ,2 ])
37- img3 = cv2 .drawMatchesKnn (img1 ,kp1 ,img2 ,kp2 ,good [:10 ],img3 ,flags = 2 )
38- plt .imshow (img3 ),plt .show ()
38+ # img3 = np.ndarray([2, 2])
39+ # img3 = cv2.drawMatchesKnn(img1, kp1, img2, kp2, good[:10], img3, flags=2)
40+
41+ # cv2.drawMatchesKnn expects list of lists as matches.
42+ img3 = cv2 .drawMatchesKnn (img1 ,kp1 ,img2 ,kp2 ,good ,flags = 2 )
43+
44+ plt .imshow (img3 ), plt .show ()
0 commit comments