-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeatures.py
More file actions
576 lines (469 loc) · 20.6 KB
/
features.py
File metadata and controls
576 lines (469 loc) · 20.6 KB
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
import math
import cv2
import numpy as np
import scipy
from scipy import ndimage, spatial
import transformations
def inbounds(shape, indices):
assert len(shape) == len(indices)
for i, ind in enumerate(indices):
if ind < 0 or ind >= shape[i]:
return False
return True
## Keypoint detectors ##########################################################
class KeypointDetector(object):
def detectKeypoints(self, image):
'''
Input:
image -- uint8 BGR image with values between [0, 255]
Output:
list of detected keypoints, fill the cv2.KeyPoint objects with the
coordinates of the detected keypoints, the angle of the gradient
(in degrees), the detector response (Harris score for Harris detector)
and set the size to 10.
'''
raise NotImplementedError()
class DummyKeypointDetector(KeypointDetector):
'''
Compute silly example features. This doesn't do anything meaningful, but
may be useful to use as an example.
'''
def detectKeypoints(self, image):
'''
Input:
image -- uint8 BGR image with values between [0, 255]
Output:
list of detected keypoints, fill the cv2.KeyPoint objects with the
coordinates of the detected keypoints, the angle of the gradient
(in degrees), the detector response (Harris score for Harris detector)
and set the size to 10.
'''
image = image.astype(np.float32)
image /= 255.
features = []
height, width = image.shape[:2]
for y in range(height):
for x in range(width):
r = image[y, x, 0]
g = image[y, x, 1]
b = image[y, x, 2]
if int(255 * (r + g + b) + 0.5) % 100 == 1:
# If the pixel satisfies this meaningless criterion,
# make it a feature.
f = cv2.KeyPoint()
f.pt = (x, y)
# Dummy size
f.size = 10
f.angle = 0
f.response = 10
features.append(f)
return features
class HarrisKeypointDetector(KeypointDetector):
# Compute harris values of an image.
def computeHarrisValues(self, srcImage):
'''
Input:
srcImage -- Grayscale input image in a numpy array with
values in [0, 1]. The dimensions are (rows, cols).
Output:
harrisImage -- numpy array containing the Harris score at
each pixel.
orientationImage -- numpy array containing the orientation of the
gradient at each pixel in degrees.
'''
height, width = srcImage.shape[:2]
harrisImage = np.zeros(srcImage.shape[:2])
orientationImage = np.zeros(srcImage.shape[:2])
# TODO 1: Compute the harris corner strength for 'srcImage' at
# each pixel and store in 'harrisImage'. See the project page
# for direction on how to do this. Also compute an orientation
# for each pixel and store it in 'orientationImage.'
# TODO-BLOCK-BEGIN
sobelImageIX = scipy.ndimage.sobel(srcImage, 1, mode='constant', cval=0.0)
sobelImageIY = scipy.ndimage.sobel(srcImage, 0, mode='constant', cval=0.0)
A = scipy.ndimage.filters.gaussian_filter(sobelImageIX*sobelImageIX,sigma=0.5)
B = scipy.ndimage.filters.gaussian_filter(sobelImageIY*sobelImageIX,sigma=0.5)
C = scipy.ndimage.filters.gaussian_filter(sobelImageIY*sobelImageIY,sigma=0.5)
H = [[0,0],[0,0]]
for i in range(0, height):
for j in range(0, width):
# Computer Harris Image
H[0][0] = np.sum(A[i,j])
H[0][1] = np.sum(B[i,j])
H[1][0] = np.sum(B[i,j])
H[1][1] = np.sum(C[i,j])
cH = (np.linalg.det(H)) - (0.1*(np.trace(H) * np.trace(H)))
harrisImage[i][j] = cH
# Computer Orientation Image
if sobelImageIX[i,j] == 0 and sobelImageIY[i,j] > 0:
Ori = 90
elif sobelImageIX[i,j] == 0 and sobelImageIY[i,j] < 0:
Ori = -90
elif sobelImageIY[i,j] == 0 and sobelImageIX[i,j] > 0:
Ori = 0
elif sobelImageIX[i,j] < 0 and sobelImageIY[i,j] == 0:
Ori = 180
else:
Ori = np.degrees(np.arctan2(sobelImageIY[i,j],sobelImageIX[i,j]))
orientationImage[i][j] = Ori
# TODO-BLOCK-END
print(harrisImage[40,89])
return harrisImage, orientationImage
def computeLocalMaxima(self, harrisImage):
'''
Input:
harrisImage -- numpy array containing the Harris score at
each pixel.
Output:
destImage -- numpy array containing True/False at
each pixel, depending on whether
the pixel value is the local maxima in
its 7x7 neighborhood.
'''
height, width = harrisImage.shape[:2]
destImage = np.zeros_like(harrisImage, np.bool)
# TODO 2: Compute the local maxima image
# TODO-BLOCK-BEGIN
maximaImage = scipy.ndimage.filters.maximum_filter(harrisImage, size=7, mode='constant')
for i in range (0, height):
for j in range(0, width):
if harrisImage[i][j] == maximaImage[i][j]:
destImage[i][j] = True
else:
destImage[i][j] = False
# TODO-BLOCK-END
return destImage
def detectKeypoints(self, image):
'''
Input:
image -- BGR image with values between [0, 255]
Output:
list of detected keypoints, fill the cv2.KeyPoint objects with the
coordinates of the detected keypoints, the angle of the gradient
(in degrees), the detector response (Harris score for Harris detector)
and set the size to 10.
'''
image = image.astype(np.float32)
image /= 255.
height, width = image.shape[:2]
features = []
# Create grayscale image used for Harris detection
grayImage = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# computeHarrisValues() computes the harris score at each pixel
# position, storing the result in harrisImage.
# You will need to implement this function.
harrisImage, orientationImage = self.computeHarrisValues(grayImage)
# Compute local maxima in the Harris image. You will need to
# implement this function. Create image to store local maximum harris
# values as True, other pixels False
harrisMaxImage = self.computeLocalMaxima(harrisImage)
# Loop through feature points in harrisMaxImage and fill in information
# needed for descriptor computation for each point.
# You need to fill x, y, and angle.
for y in range(height):
for x in range(width):
if not harrisMaxImage[y, x]:
continue
f = cv2.KeyPoint()
# TODO 3: Fill in feature f with location and orientation
# data here. Set f.size to 10, f.pt to the (x,y) coordinate,
# f.angle to the orientation in degrees and f.response to
# the Harris score
# TODO-BLOCK-BEGIN
f.size = 10
f.pt = (x, y)
f.angle = orientationImage[y][x]
f.response = harrisImage[y][x]
# TODO-BLOCK-END
features.append(f)
return features
class ORBKeypointDetector(KeypointDetector):
def detectKeypoints(self, image):
'''
Input:
image -- uint8 BGR image with values between [0, 255]
Output:
list of detected keypoints, fill the cv2.KeyPoint objects with the
coordinates of the detected keypoints, the angle of the gradient
(in degrees) and set the size to 10.
'''
detector = cv2.ORB()
return detector.detect(image)
## Feature descriptors #########################################################
class FeatureDescriptor(object):
# Implement in child classes
def describeFeatures(self, image, keypoints):
'''
Input:
image -- BGR image with values between [0, 255]
keypoints -- the detected features, we have to compute the feature
descriptors at the specified coordinates
Output:
Descriptor numpy array, dimensions:
keypoint number x feature descriptor dimension
'''
raise NotImplementedError
class SimpleFeatureDescriptor(FeatureDescriptor):
# TODO: Implement parts of this function
def describeFeatures(self, image, keypoints):
'''
Input:
image -- BGR image with values between [0, 255]
keypoints -- the detected features, we have to compute the feature
descriptors at the specified coordinates
Output:
desc -- K x 25 numpy array, where K is the number of keypoints
'''
image = image.astype(np.float32)
image /= 255.
grayImage = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
desc = np.zeros((len(keypoints), 5 * 5))
print(desc)
for i, f in enumerate(keypoints):
x, y = f.pt
# TODO 4: The simple descriptor is a 5x5 window of intensities
# sampled centered on the feature point. Store the descriptor
# as a row-major vector. Treat pixels outside the image as zero.
# TODO-BLOCK-BEGIN
count = 0
for k in range (-2,3):
for l in range(-2,3):
# print(grayImage)
if y+k < 0 or y+k > len(grayImage) or x+l < 0 or x+l > len(grayImage[0]):
desc[i][count] = 0
else:
# print(image[y+k][x+l])
desc[i][count] = grayImage[y+k][x+l]
count += 1
# TODO-BLOCK-END
return desc
class MOPSFeatureDescriptor(FeatureDescriptor):
# TODO: Implement parts of this function
def describeFeatures(self, image, keypoints):
'''
Input:
image -- BGR image with values between [0, 255]
keypoints -- the detected features, we have to compute the feature
descriptors at the specified coordinates
Output:
desc -- K x W^2 numpy array, where K is the number of keypoints
and W is the window size
'''
image = image.astype(np.float32)
image /= 255.
# This image represents the window around the feature you need to
# compute to store as the feature descriptor (row-major)
windowSize = 8
desc = np.zeros((len(keypoints), windowSize * windowSize))
grayImage = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
grayImage = ndimage.gaussian_filter(grayImage, 0.5)
for i, f in enumerate(keypoints):
# TODO 5: Compute the transform as described by the feature
# location/orientation. You will need to compute the transform
# from each pixel in the 40x40 rotated window surrounding
# the feature to the appropriate pixels in the 8x8 feature
# descriptor image.
transMx = np.zeros((2, 3))
# TODO-BLOCK-BEGIN
x, y = f.pt
newPt = np.array([-x,-y, 0])
# Get the translation matrix
t1 = transformations.get_trans_mx(newPt)
# Get the rotation matrix
angle = np.radians(f.angle)
rotation = transformations.get_rot_mx(0.0,0.0, angle)
# Get scale matrix - 0.2 so that it's at 1/5 scale
scale = transformations.get_scale_mx(0.2,0.2,1)
# Get the second transaltion matrix
t2 = transformations.get_trans_mx(np.array([4, 4, 0]))
# Compute dot matrix
dotResult = np.dot(np.dot(np.dot(t1, rotation), scale), t2)
transMx[0] = [ dotResult[0][0], dotResult[0][1], dotResult[0][3] ]
transMx[1] = [ dotResult[1][0], dotResult[1][1], dotResult[1][3] ]
# TODO-BLOCK-END
# Call the warp affine function to do the mapping
# It expects a 2x3 matrix
destImage = cv2.warpAffine(grayImage, transMx,
(windowSize, windowSize), flags=cv2.INTER_LINEAR)
# TODO 6: Normalize the descriptor to have zero mean and unit
# variance. If the variance is zero then set the descriptor
# vector to zero. Lastly, write the vector to desc.
# TODO-BLOCK-BEGIN
# Normalize
destImage -= destImage.mean()
destImageStdDev = destImage.std()
if destImageStdDev == 0:
destImage = np.zeros(windowSize, windowSize)
else:
destImage /= destImageStdDev
dest[i] = destImage.flatten()
# TODO-BLOCK-END
return desc
class ORBFeatureDescriptor(KeypointDetector):
def describeFeatures(self, image, keypoints):
'''
Input:
image -- BGR image with values between [0, 255]
keypoints -- the detected features, we have to compute the feature
descriptors at the specified coordinates
Output:
Descriptor numpy array, dimensions:
keypoint number x feature descriptor dimension
'''
descriptor = cv2.ORB()
kps, desc = descriptor.compute(image, keypoints)
if desc is None:
desc = np.zeros((0, 128))
return desc
# Compute Custom descriptors (extra credit)
class CustomFeatureDescriptor(FeatureDescriptor):
def describeFeatures(self, image, keypoints):
'''
Input:
image -- BGR image with values between [0, 255]
keypoints -- the detected features, we have to compute the feature
descriptors at the specified coordinates
Output:
Descriptor numpy array, dimensions:
keypoint number x feature descriptor dimension
'''
raise NotImplementedError('NOT IMPLEMENTED')
## Feature matchers ############################################################
class FeatureMatcher(object):
def matchFeatures(self, desc1, desc2):
'''
Input:
desc1 -- the feature descriptors of image 1 stored in a numpy array,
dimensions: rows (number of key points) x
columns (dimension of the feature descriptor)
desc2 -- the feature descriptors of image 2 stored in a numpy array,
dimensions: rows (number of key points) x
columns (dimension of the feature descriptor)
Output:
features matches: a list of cv2.DMatch objects
How to set attributes:
queryIdx: The index of the feature in the first image
trainIdx: The index of the feature in the second image
distance: The distance between the two features
'''
raise NotImplementedError
# Evaluate a match using a ground truth homography. This computes the
# average SSD distance between the matched feature points and
# the actual transformed positions.
@staticmethod
def evaluateMatch(features1, features2, matches, h):
d = 0
n = 0
for m in matches:
id1 = m.queryIdx
id2 = m.trainIdx
ptOld = np.array(features2[id2].pt)
ptNew = FeatureMatcher.applyHomography(features1[id1].pt, h)
# Euclidean distance
d += np.linalg.norm(ptNew - ptOld)
n += 1
return d / n if n != 0 else 0
# Transform point by homography.
@staticmethod
def applyHomography(pt, h):
x, y = pt
d = h[6]*x + h[7]*y + h[8]
return np.array([(h[0]*x + h[1]*y + h[2]) / d,
(h[3]*x + h[4]*y + h[5]) / d])
class SSDFeatureMatcher(FeatureMatcher):
def matchFeatures(self, desc1, desc2):
'''
Input:
desc1 -- the feature descriptors of image 1 stored in a numpy array,
dimensions: rows (number of key points) x
columns (dimension of the feature descriptor)
desc2 -- the feature descriptors of image 2 stored in a numpy array,
dimensions: rows (number of key points) x
columns (dimension of the feature descriptor)
Output:
features matches: a list of cv2.DMatch objects
How to set attributes:
queryIdx: The index of the feature in the first image
trainIdx: The index of the feature in the second image
distance: The distance between the two features
'''
matches = []
# feature count = n
assert desc1.ndim == 2
# feature count = m
assert desc2.ndim == 2
# the two features should have the type
assert desc1.shape[1] == desc2.shape[1]
if desc1.shape[0] == 0 or desc2.shape[0] == 0:
return []
# TODO 7: Perform simple feature matching. This uses the SSD
# distance between two feature vectors, and matches a feature in
# the first image with the closest feature in the second image.
# Note: multiple features from the first image may match the same
# feature in the second image.
# TODO-BLOCK-BEGIN
for i in range(0, desc1):
cur_closest = cv2.DMatch()
cur_closest.queryIdx = i
cur_closest.trainIdx = 0
cur_closest.distance = scipy.spatial.distance.euclidean(desc[i], desc2[0])
for j in range(0, desc2):
eDist = scipy.spatial.distance.euclidean(desc[i], desc2[j])
if eDist < cur_closest.distance:
cur_closest.trainIdx = j
cur_closest.distance = eDist
matches.append(cur_closest)
# TODO-BLOCK-END
return matches
class RatioFeatureMatcher(FeatureMatcher):
def matchFeatures(self, desc1, desc2):
'''
Input:
desc1 -- the feature descriptors of image 1 stored in a numpy array,
dimensions: rows (number of key points) x
columns (dimension of the feature descriptor)
desc2 -- the feature descriptors of image 2 stored in a numpy array,
dimensions: rows (number of key points) x
columns (dimension of the feature descriptor)
Output:
features matches: a list of cv2.DMatch objects
How to set attributes:
queryIdx: The index of the feature in the first image
trainIdx: The index of the feature in the second image
distance: The ratio test score
'''
matches = []
# feature count = n
assert desc1.ndim == 2
# feature count = m
assert desc2.ndim == 2
# the two features should have the type
assert desc1.shape[1] == desc2.shape[1]
if desc1.shape[0] == 0 or desc2.shape[0] == 0:
return []
# TODO 8: Perform ratio feature matching.
# This uses the ratio of the SSD distance of the two best matches
# and matches a feature in the first image with the closest feature in the
# second image.
# Note: multiple features from the first image may match the same
# feature in the second image.
# You don't need to threshold matches in this function
# TODO-BLOCK-BEGIN
distances = spatial.distance.cdist(desc1, desc2, metric='euclidean')
sortedDistances = np.argsort(distances)
closestD1 = sortedDistances[:,0]
closestD2 = sortedDistances[:,1]
for i in range(0, closestD1.shape[0]):
match = cv2.DMatch()
match.queryIdx = i
match.trainIdx = closestD1[i]
match.distance = distances[i, closestD1[i]]/distances[i,closestD2[i]]
matches.append(match)
# TODO-BLOCK-END
return matches
class ORBFeatureMatcher(FeatureMatcher):
def __init__(self):
self.bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
super(ORBFeatureMatcher, self).__init__()
def matchFeatures(self, desc1, desc2):
return self.bf.match(desc1.astype(np.uint8), desc2.astype(np.uint8))