1
- #-*-coding:utf8-*-#
1
+ #-*-coding:utf8-*-#
2
2
__author__ = 'play4fun'
3
3
"""
4
4
create time:15-11-9 下午1:28
5
+ 对 SIFT 描述符进行蛮力匹配和比值测试
5
6
"""
6
7
7
-
8
-
9
8
import numpy as np
10
9
import cv2
11
10
from matplotlib import pyplot as plt
12
- img1 = cv2 .imread ('../data/box.png' ,0 )
11
+
12
+ img1 = cv2 .imread ('../data/box.png' , 0 )
13
13
# queryImage
14
- img2 = cv2 .imread ('../data/box_in_scene.png' ,0 ) # trainImage
14
+ img2 = cv2 .imread ('../data/box_in_scene.png' ,0 ) # trainImage
15
15
# Initiate SIFT detector
16
16
# sift = cv2.SIFT()
17
17
sift = cv2 .xfeatures2d .SIFT_create ()
18
- #TODO
18
+
19
19
20
20
# 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
+
23
24
# BFMatcher with default params
24
25
bf = cv2 .BFMatcher ()
25
- matches = bf .knnMatch (des1 ,des2 , k = 2 )
26
+ matches = bf .knnMatch (des1 , des2 , k = 2 )
27
+
26
28
# Apply ratio test
27
29
# 比值测试,首先获取与 A距离最近的点 B (最近)和 C (次近),
28
30
# 只有当 B/C 小于阀值时(0.75)才被认为是匹配,
29
- #因为假设匹配是一一对应的,真正的匹配的理想距离为0
31
+ #因为假设匹配是一一对应的,真正的匹配的理想距离为0
30
32
good = []
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 :
33
35
good .append ([m ])
34
36
35
37
# 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