-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSIFT_KNN_BBS.py
60 lines (40 loc) · 1.39 KB
/
SIFT_KNN_BBS.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import cv2
import cv2.xfeatures2d as cv
import numpy
from matplotlib import pyplot as plt
def SIFT_KNN_BBS(img1, img2):
t1 = cv2.imread(img1,0)
t2 = cv2.imread(img2,0)
sift=cv.SIFT_create()
kp1, des1 = sift.detectAndCompute(t1, None)
kp2, des2 = sift.detectAndCompute(t2, None)
f=cv2.drawKeypoints(t1,kp1,None,[0,0,255],flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
nf=cv2.drawKeypoints(t2,kp2,None,[255,0,0],flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
bf = cv2.BFMatcher()
matches = bf.knnMatch(des1,des2, k=2)
good1 = []
for m,n in matches:
if m.distance < 0.75*n.distance:
good1.append([m])
matches = bf.knnMatch(des2,des1, k=2)
good2 = []
for m,n in matches:
if m.distance < 0.75*n.distance:
good2.append([m])
good=[]
for i in good1:
img1_id1=i[0].queryIdx
img2_id1=i[0].trainIdx
(x1,y1)=kp1[img1_id1].pt
(x2,y2)=kp2[img2_id1].pt
for j in good2:
img1_id2=j[0].queryIdx
img2_id2=j[0].trainIdx
(a1,b1)=kp2[img1_id2].pt
(a2,b2)=kp1[img2_id2].pt
if (a1 == x2 and b1 == y2) and (a2 == x1 and b2 == y1):
good.append(i)
result=cv2.drawMatchesKnn(t1,kp1,t2,kp2,good,None,[0,0,255],flags=2)
plt.imshow(result, interpolation = 'bicubic')
plt.axis('off')
plt.show()