-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathFeatureMacthingAlgorithms.py
194 lines (133 loc) · 5.48 KB
/
FeatureMacthingAlgorithms.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# -*- coding: utf-8 -*-
"""
Created on Sun Jun 16 15:10:19 2019
@author: MAGESHWARAN
"""
import cv2
import matplotlib.pyplot as plt
import os
import numpy as np
base_dir = os.getcwd()
data_folder = os.path.join(base_dir, "Dataset")
images_folder = os.path.join(data_folder, "Images")
crops_folder = os.path.join(data_folder, "Crops")
sample_testset = os.path.join(data_folder, "sample_testset")
sample_images = os.path.join(sample_testset, "images")
sample_crops = os.path.join(sample_testset, "crops")
def bruteForceSIFT(img1, img2):
sift = cv2.xfeatures2d.SIFT_create()
kp1, des1 = sift.detectAndCompute(img1, None)
kp2, des2 = sift.detectAndCompute(img2, None)
bf = cv2.BFMatcher()
matches = bf.knnMatch(des1, des2, k=2)
good = []
print(len(matches))
for match1, match2 in matches:
if match1.distance < 0.75 * match2.distance:
good.append([match1])
sift_matches = cv2.drawMatchesKnn(img1, kp1, img2, kp2, good, None, flags=2)
sift_matches = cv2.cvtColor(sift_matches, cv2.COLOR_BGR2RGB)
print(len(good))
plt.imshow(sift_matches)
plt.show()
return (kp1, kp2)
def bruteForceORB(img1, img2):
orb = cv2.ORB_create()
kp1, des1 = orb.detectAndCompute(img1,None)
kp2, des2 = orb.detectAndCompute(img2,None)
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
matches = bf.match(des1, des2)
matches = sorted(matches, key = lambda x:x.distance)
orb_matches = cv2.drawMatches(img1, kp1, img2, kp2,matches[:10], None,
flags=cv2.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS)
orb_matches = cv2.cvtColor(orb_matches, cv2.COLOR_BGR2RGB)
plt.imshow(orb_matches)
plt.show()
return matches[:10]
def FLANN(img1, img2):
FLANN_INDEX_KDTREE = 1
index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
search_params = dict(checks=10)
sift = cv2.xfeatures2d.SIFT_create()
kp1, des1 = sift.detectAndCompute(img1, None)
kp2, des2 = sift.detectAndCompute(img2, None)
flann = cv2.FlannBasedMatcher(index_params,search_params)
matches = flann.knnMatch(des1,des2,k=2)
matchesMask = [[0,0] for i in range(len(matches))]
for i,(m,n) in enumerate(matches):
if m.distance < 0.7*n.distance:
matchesMask[i]=[1,0]
draw_params = dict(matchColor = (0,255,0),
matchesMask = matchesMask,
flags = cv2.DrawMatchesFlags_DEFAULT)
flann_matches = cv2.drawMatchesKnn(img1,kp1,img2,kp2,matches,None,**draw_params)
orb_matches = cv2.cvtColor(flann_matches, cv2.COLOR_BGR2RGB)
plt.imshow(orb_matches)
plt.show()
return matches[:10]
def templateMatching(img1, img2, method):
w, h, c = img1.shape
img = img2.copy()
res = cv2.matchTemplate(img, img1, eval(method))
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
top_left = min_loc
bottom_right = (top_left[0] + w, top_left[1] + h)
cv2.rectangle(img,top_left, bottom_right, 255, 2)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.imshow(img)
plt.show()
return top_left, bottom_right
def ModifiedFLANN(img1, img2):
mini_match_count = 10
FLANN_INDEX_KDTREE = 1
index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
search_params = dict(checks=10)
sift = cv2.xfeatures2d.SIFT_create()
kp1, des1 = sift.detectAndCompute(img1, None)
kp2, des2 = sift.detectAndCompute(img2, None)
flann = cv2.FlannBasedMatcher(index_params, search_params)
matches = flann.knnMatch(des1, des2, k=2)
good_matches = []
orgBorder = None
for match1, match2 in matches:
if match1.distance < (0.7 * match2.distance):
good_matches.append((match1))
if len(good_matches) > mini_match_count:
cropImg = []
orgImg = []
for m in good_matches:
cropImg.append(kp1[m.queryIdx].pt)
orgImg.append(kp2[m.trainIdx].pt)
cropImg, orgImg = np.float32((cropImg, orgImg))
H, status = cv2.findHomography(cropImg, orgImg, cv2.RANSAC, 3.0)
h, w, c = img1.shape
cropBorder = np.float32([[[0,0], [0,h-1], [w-1,h-1], [w-1,0]]])
orgBorder = cv2.perspectiveTransform(cropBorder, H)
cv2.polylines(img2, [np.int32(orgBorder)], True, (0, 255, 0), 5)
else:
print("No Good Matches Found")
matched = cv2.cvtColor(img2, cv2.COLOR_BGR2RGB)
plt.imshow(matched)
plt.show()
return orgBorder
crop_img = cv2.imread(os.path.join(sample_crops,
"3ef28b3a-cdd5-548b-8134-7c69393c89f2.jpg"))
img = cv2.imread(os.path.join(sample_images,
"ea9b55cc-e010-5905-b696-9f8bacc50fe6.jpg"))
plt.imshow(cv2.cvtColor(crop_img, cv2.COLOR_BGR2RGB))
plt.show()
# =============================================================================
#
# matches_sift = bruteForceSIFT(crop_img, img)
#
# matches_orb = bruteForceORB(crop_img, img)
#
# matches_flann = FLANN(crop_img, img)
#
# methods = ['cv2.TM_CCOEFF', 'cv2.TM_CCOEFF_NORMED', 'cv2.TM_CCORR',
# 'cv2.TM_CCORR_NORMED', 'cv2.TM_SQDIFF', 'cv2.TM_SQDIFF_NORMED']
#
# for method in methods:
# x, y = templateMatching(crop_img, img, method)
# =============================================================================
crop_border = ModifiedFLANN(crop_img, img)