-
Notifications
You must be signed in to change notification settings - Fork 0
/
cv_assignment_2_task_1.py
399 lines (294 loc) · 12.5 KB
/
cv_assignment_2_task_1.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# -*- coding: utf-8 -*-
"""CV Assignment 2 Task 1.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1xecx_vdbAR8-a3MPtWk0q-yC6okJNTXs
"""
print("Bismillah")
"""# IMPORTS"""
import numpy as np
import cv2
from matplotlib import pyplot as plt
from pylab import rcParams
# !pip install opencv-python==3.3.0.10 opencv-contrib-python==3.3.0.10
"""# FUNCTIONS ~ Local Feature Detectors and Descriptors
BRIEF (Binary Robust Independent Elementary Features)
"""
# BRIEF (Binary Robust Independent Elementary Features)
def Star_det_BRIEF_Des(img):
rcParams['figure.figsize'] = 5, 5
# Initiate Star detector
star = cv2.xfeatures2d.StarDetector_create()
# Initiate BRIEF extractor
brief = cv2.xfeatures2d.BriefDescriptorExtractor_create()
# find the keypoints with STAR
kp = star.detect(img,None)
# compute the descriptors with BRIEF
kp1, des = brief.compute(img, kp)
#now draw
BRIEF_img = cv2.drawKeypoints(img, kp1, None, color=(255,0,0))
plt.imshow(BRIEF_img)
plt.title("ORB on image")
plt.show()
return kp1, des
"""Oriented FAST and Rotated BRIEF (ORB)"""
# Oriented FAST and Rotated BRIEF (ORB)
def ORB_det_Des(img):
# Initiate ORB Detector and Descriptor
orb = cv2.ORB_create()
rcParams['figure.figsize'] = 5, 5
# find the keypoints and descriptors with ORB
kp, des = orb.detectAndCompute(img, None)
# draw only keypoints location,not size and orientation
orb_img = cv2.drawKeypoints(img, kp, None, color=(255,0,0))
plt.imshow(orb_img)
plt.title("ORB on image")
plt.show()
return kp, des
"""Features from Accelerated Segment Test (FAST)"""
# Features from Accelerated Segment Test (FAST) with Non-Max Suppression
def FAST_det_BRIEF_Des_NMS(img):
rcParams['figure.figsize'] = 5, 5
# Initiate FAST object with default values
fast = cv2.FastFeatureDetector_create()
# Initiate BRIEF extractor
brief = cv2.xfeatures2d.BriefDescriptorExtractor_create()
# find and draw the keypoints
kp = fast.detect(img,None)
fast_img = cv2.drawKeypoints(img, kp, None, color=(255,0,0)) # With Non-Max Suppression
# Print all default params
print( "Threshold: {}".format(fast.getThreshold()) )
print( "nonmaxSuppression:{}".format(fast.getNonmaxSuppression()) )
print( "neighborhood: {}".format(fast.getType()) )
print( "Total Keypoints with nonmaxSuppression: {}".format(len(kp)) )
# compute the descriptors with BRIEF
kp1, des = brief.compute(img, kp)
plt.imshow(fast_img)
plt.title("FAST on image")
plt.show()
return kp, des
"""Scale Invariant Feature Transform (SIFT)"""
# Scale Invariant Feature Transform (SIFT)
def SIFT_apply(img):
rcParams['figure.figsize'] = 5, 5
# Initiate SIFT detector
sift = cv2.xfeatures2d.SIFT_create()
kp , desc = sift.detectAndCompute(img,None)
# draw only keypoints location,not size and orientation
result_sift=cv2.drawKeypoints(img,kp,None,flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
plt.imshow(result_sift)
plt.title("SIFT on image")
plt.show()
return kp, des
"""Speeded-Up Robust Features (SURF)"""
# Speeded-Up Robust Features (SURF)
def SURF_apply(img):
rcParams['figure.figsize'] = 5, 5
# Initiate SIFT detector
surf = cv2.xfeatures2d.SURF_create()
kp, des = surf.detectAndCompute(img,None)
# draw only keypoints location,not size and orientation
result_surf=cv2.drawKeypoints(img, kp, None, flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
# img2 = cv2.drawKeypoints(img,kp,None,(255,0,0),4)
plt.imshow(result_surf)
plt.title("SURF on image")
plt.show()
return kp, des
"""# FUNCTIONS ~ Feature Matchers
Brute Force Matcher
"""
#Brute Force Matcher Function
def BruteForceMatcher(img1, kp1, desc1, img2, kp2, desc2):
rcParams['figure.figsize'] = 20, 20
# create BFMatcher object
bf = cv2.BFMatcher(cv2.NORM_L2, crossCheck=True)
matches = bf.match(desc1, desc2)
matches = sorted(matches, key = lambda x:x.distance)
print(len(matches))
result = cv2.drawMatches(img1, kp1, img2, kp2, matches[:10], None, flags=2)
plt.imshow(result)
plt.title("Brute Force Matcher top 10 features matched")
plt.show()
"""FLANN Based Matcher"""
#FLANN Based Matcher Function
def FLANN_based_Matcher(img1, kp1, desc1, img2, kp2, desc2):
FLANN_INDEX_KDTREE = 0
index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
search_params = dict(checks=50) # or pass empty dictionary
flann = cv2.FlannBasedMatcher(index_params,search_params)
des_1 = np.float32(desc1)
des_2 = np.float32(desc2)
# matches = flann.knnMatch(des_1,des_2,k=2)
matches = flann.knnMatch(np.asarray(des_1,np.float32),np.asarray(des_2,np.float32),k=2)
# Need to draw only good matches, so create a mask
matchesMask = [[0,0] for i in range(len(matches))]
# ratio test as per Lowe's paper
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),
singlePointColor = (255,0,0),
matchesMask = matchesMask,
flags = 0)
# top 10
result_img = cv2.drawMatchesKnn(img1, kp1, img2, kp2, matches[:5],None,flags=2)
plt.imshow(result_img)
plt.title("FLANN Based Matcher")
"""# Applying For Book Set
Reading matching book set items
"""
# Reading matching book set items
book = cv2.imread('/content/drive/MyDrive/CV/Assignment 2/book.jpg',0)
book_person = cv2.imread('/content/drive/MyDrive/CV/Assignment 2/book_person_holding.jpg',0)
"""Applying ORB"""
b_kp , b_ds = ORB_det_Des(book)
bp_kp , bp_ds = ORB_det_Des(book_person)
# Matching Features
BruteForceMatcher(book, b_kp , b_ds, book_person, bp_kp , bp_ds)
FLANN_based_Matcher(book, b_kp , b_ds, book_person, bp_kp , bp_ds)
# print(len(b_kp), len(bp_kp))
"""Applying BRIEF with Star"""
b_kp , b_ds = Star_det_BRIEF_Des(book)
bp_kp , bp_ds = Star_det_BRIEF_Des(book_person)
# Matching Features
BruteForceMatcher(book, b_kp , b_ds, book_person, bp_kp , bp_ds)
FLANN_based_Matcher(book, b_kp , b_ds, book_person, bp_kp , bp_ds)
print(len(b_kp), len(bp_kp))
"""Applying FAST"""
b_kp , b_ds = FAST_det_BRIEF_Des_NMS(book)
bp_kp , bp_ds = FAST_det_BRIEF_Des_NMS(book_person)
# Matching Features
BruteForceMatcher(book, b_kp , b_ds, book_person, bp_kp , bp_ds)
FLANN_based_Matcher(book, b_kp , b_ds, book_person, bp_kp , bp_ds)
"""Applying SIFT"""
b_kp , b_ds = SIFT_apply(book)
bp_kp , bp_ds = SIFT_apply(book_person)
# Matching Features
BruteForceMatcher(book, b_kp , b_ds, book_person, bp_kp , bp_ds)
FLANN_based_Matcher(book, b_kp , b_ds, book_person, bp_kp , bp_ds)
print(len(b_kp), len(bp_kp))
"""Applying SURF"""
b_kp , b_ds = SURF_apply(book)
bp_kp , bp_ds = SURF_apply(book_person)
# Matching Features
BruteForceMatcher(book, b_kp , b_ds, book_person, bp_kp , bp_ds)
FLANN_based_Matcher(book, b_kp , b_ds, book_person, bp_kp , bp_ds)
"""# Applying For Roma Set
Reading matching Roma set items
"""
# Reading matching book set items
roma_1 = cv2.imread('/content/drive/MyDrive/CV/Assignment 2/roma_1.jpg',0)
roma_2 = cv2.imread('/content/drive/MyDrive/CV/Assignment 2/roma_2.jpg',0)
"""Applying ORB"""
r1_kp , r1_ds = ORB_det_Des(roma_1)
r2_kp , r2_ds = ORB_det_Des(roma_2)
# Matching Features
BruteForceMatcher(roma_1, r1_kp , r1_ds, roma_2, r2_kp , r2_ds)
FLANN_based_Matcher(roma_1, r1_kp , r1_ds, roma_2, r2_kp , r2_ds)
print(len(r1_kp), len(r2_kp))
"""Applying BRIEF with Star"""
r1_kp , r1_ds = Star_det_BRIEF_Des(roma_1)
r2_kp , r2_ds = Star_det_BRIEF_Des(roma_2)
# Matching Features
BruteForceMatcher(roma_1, r1_kp , r1_ds, roma_2, r2_kp , r2_ds)
FLANN_based_Matcher(roma_1, r1_kp , r1_ds, roma_2, r2_kp , r2_ds)
print(len(r1_kp), len(r2_kp))
"""Applying FAST"""
r1_kp , r1_ds = FAST_det_BRIEF_Des_NMS(roma_1)
r2_kp , r2_ds = FAST_det_BRIEF_Des_NMS(roma_2)
# Matching Features
BruteForceMatcher(roma_1, r1_kp , r1_ds, roma_2, r2_kp , r2_ds)
FLANN_based_Matcher(roma_1, r1_kp , r1_ds, roma_2, r2_kp , r2_ds)
print(len(r1_kp), len(r2_kp))
"""Applying SIFT"""
r1_kp , r1_ds = SIFT_apply(roma_1)
r2_kp , r2_ds = SIFT_apply(roma_2)
# Matching Features
BruteForceMatcher(roma_1, r1_kp , r1_ds, roma_2, r2_kp , r2_ds)
FLANN_based_Matcher(roma_1, r1_kp , r1_ds, roma_2, r2_kp , r2_ds)
print(len(r1_kp), len(r2_kp))
"""Applying SURF"""
r1_kp , r1_ds = SURF_apply(roma_1)
r2_kp , r2_ds = SURF_apply(roma_2)
# Matching Features
BruteForceMatcher(roma_1, r1_kp , r1_ds, roma_2, r2_kp , r2_ds)
FLANN_based_Matcher(roma_1, r1_kp , r1_ds, roma_2, r2_kp , r2_ds)
print(len(r1_kp), len(r2_kp))
"""# Applying For Building Set
Reading matching Building set items
"""
# Reading matching book set items
building_1 = cv2.imread('/content/drive/MyDrive/CV/Assignment 2/building_1.jpg',0)
building_2 = cv2.imread('/content/drive/MyDrive/CV/Assignment 2/building_2.jpg',0)
building_3 = cv2.imread('/content/drive/MyDrive/CV/Assignment 2/building_3.jpg',0)
"""Applying ORB"""
b1_kp , b1_ds = ORB_det_Des(building_1)
b2_kp , b2_ds = ORB_det_Des(building_2)
b3_kp , b3_ds = ORB_det_Des(building_3)
# Matching Features
BruteForceMatcher(building_1, b1_kp, b1_ds, building_2, b2_kp , b2_ds)
FLANN_based_Matcher(building_1, b1_kp, b1_ds, building_2, b2_kp , b2_ds)
print(len(b1_kp), len(b2_kp), len(b3_kp))
# Matching Features
BruteForceMatcher(building_1, b1_kp, b1_ds, building_3, b3_kp , b3_ds)
FLANN_based_Matcher(building_1, b1_kp, b1_ds, building_3, b3_kp , b3_ds)
# Matching Features
BruteForceMatcher(building_2, b2_kp , b2_ds, building_3, b3_kp , b3_ds)
FLANN_based_Matcher(building_2, b2_kp , b2_ds, building_3, b3_kp , b3_ds)
"""Applying BRIEF with Star"""
b1_kp , b1_ds = Star_det_BRIEF_Des(building_1)
b2_kp , b2_ds = Star_det_BRIEF_Des(building_2)
b3_kp , b3_ds = Star_det_BRIEF_Des(building_3)
# Matching Features
BruteForceMatcher(building_1, b1_kp, b1_ds, building_2, b2_kp , b2_ds)
FLANN_based_Matcher(building_1, b1_kp, b1_ds, building_2, b2_kp , b2_ds)
print(len(b1_kp), len(b2_kp), len(b3_kp))
# Matching Features
BruteForceMatcher(building_1, b1_kp, b1_ds, building_3, b3_kp , b3_ds)
FLANN_based_Matcher(building_1, b1_kp, b1_ds, building_3, b3_kp , b3_ds)
# Matching Features
BruteForceMatcher(building_2, b2_kp , b2_ds, building_3, b3_kp , b3_ds)
FLANN_based_Matcher(building_2, b2_kp , b2_ds, building_3, b3_kp , b3_ds)
"""Applying FAST"""
b1_kp , b1_ds = FAST_det_BRIEF_Des_NMS(building_1)
b2_kp , b2_ds = FAST_det_BRIEF_Des_NMS(building_2)
b3_kp , b3_ds = FAST_det_BRIEF_Des_NMS(building_3)
# Matching Features
BruteForceMatcher(building_1, b1_kp, b1_ds, building_2, b2_kp , b2_ds)
FLANN_based_Matcher(building_1, b1_kp, b1_ds, building_2, b2_kp , b2_ds)
print(len(b1_kp), len(b2_kp), len(b3_kp))
# Matching Features
BruteForceMatcher(building_1, b1_kp, b1_ds, building_3, b3_kp , b3_ds)
FLANN_based_Matcher(building_1, b1_kp, b1_ds, building_3, b3_kp , b3_ds)
# Matching Features
BruteForceMatcher(building_2, b2_kp , b2_ds, building_3, b3_kp , b3_ds)
FLANN_based_Matcher(building_2, b2_kp , b2_ds, building_3, b3_kp , b3_ds)
"""Applying SIFT"""
b1_kp , b1_ds = SIFT_apply(building_1)
b2_kp , b2_ds = SIFT_apply(building_2)
b3_kp , b3_ds = SIFT_apply(building_3)
# Matching Features
BruteForceMatcher(building_1, b1_kp, b1_ds, building_2, b2_kp , b2_ds)
FLANN_based_Matcher(building_1, b1_kp, b1_ds, building_2, b2_kp , b2_ds)
print(len(b1_kp), len(b2_kp), len(b3_kp))
# Matching Features
BruteForceMatcher(building_1, b1_kp, b1_ds, building_3, b3_kp , b3_ds)
FLANN_based_Matcher(building_1, b1_kp, b1_ds, building_3, b3_kp , b3_ds)
# Matching Features
BruteForceMatcher(building_2, b2_kp , b2_ds, building_3, b3_kp , b3_ds)
FLANN_based_Matcher(building_2, b2_kp , b2_ds, building_3, b3_kp , b3_ds)
"""Applying SURF"""
b1_kp , b1_ds = SURF_apply(building_1)
b2_kp , b2_ds = SURF_apply(building_2)
b3_kp , b3_ds = SURF_apply(building_3)
# Matching Features
BruteForceMatcher(building_1, b1_kp, b1_ds, building_2, b2_kp , b2_ds)
FLANN_based_Matcher(building_1, b1_kp, b1_ds, building_2, b2_kp , b2_ds)
print(len(b1_kp), len(b2_kp), len(b3_kp))
# Matching Features
BruteForceMatcher(building_1, b1_kp, b1_ds, building_3, b3_kp , b3_ds)
FLANN_based_Matcher(building_1, b1_kp, b1_ds, building_3, b3_kp , b3_ds)
# Matching Features
BruteForceMatcher(building_2, b2_kp , b2_ds, building_3, b3_kp , b3_ds)
FLANN_based_Matcher(building_2, b2_kp , b2_ds, building_3, b3_kp , b3_ds)
"""**Google Colab link: https://colab.research.google.com/drive/1xecx_vdbAR8-a3MPtWk0q-yC6okJNTXs?usp=sharing**"""