-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsift.py
632 lines (533 loc) · 32.7 KB
/
sift.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
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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
# -*- coding: utf-8 -*-
# @Time : 2019/10/10 0010 15:48
# @Author : Erichym
# @Email : [email protected]
# @File : sift.py.py
# @Software: PyCharm
# Thanks rmislam. Modified based on https://github.com/rmislam/PythonSIFT/blob/master/siftdetector.py
import numpy as np
from scipy import signal
from scipy import misc
from scipy import ndimage
from scipy.stats import multivariate_normal
from numpy.linalg import norm
import numpy.linalg
import cv2
# INPUTS: imagename (filename of image, string)
# threshold (constrast threshold, int or float)
# OUTPUT: keypoints (an array of four column, where the first is the x location, the second is the y location, the third is the scale, and the fourth is the orientation)
# descriptors (an array of 128 columns, which correspond to the SIFT descriptor)
def detect_keypoints(imagename, threshold):
# SIFT Detector
# --------------
original = ndimage.imread(imagename, flatten=True)
# SIFT Parameters
s = 3
k = 2 ** (1.0 / s)
# threshold variable is the contrast threshold. Set to at least 1
# Standard deviations for Gaussian smoothing
kvec1 = np.array([1.3, 1.6, 1.6 * k, 1.6 * (k ** 2), 1.6 * (k ** 3), 1.6 * (k ** 4)])
kvec2 = np.array([1.6 * (k ** 2), 1.6 * (k ** 3), 1.6 * (k ** 4), 1.6 * (k ** 5), 1.6 * (k ** 6), 1.6 * (k ** 7)])
kvec3 = np.array([1.6 * (k ** 5), 1.6 * (k ** 6), 1.6 * (k ** 7), 1.6 * (k ** 8), 1.6 * (k ** 9), 1.6 * (k ** 10)])
kvec4 = np.array(
[1.6 * (k ** 8), 1.6 * (k ** 9), 1.6 * (k ** 10), 1.6 * (k ** 11), 1.6 * (k ** 12), 1.6 * (k ** 13)])
kvectotal = np.array(
[1.6, 1.6 * k, 1.6 * (k ** 2), 1.6 * (k ** 3), 1.6 * (k ** 4), 1.6 * (k ** 5), 1.6 * (k ** 6), 1.6 * (k ** 7),
1.6 * (k ** 8), 1.6 * (k ** 9), 1.6 * (k ** 10), 1.6 * (k ** 11)])
# Downsampling images
doubled = misc.imresize(original, 200, 'bilinear').astype(int) # original size*2
normal = misc.imresize(doubled, 50, 'bilinear').astype(int) # original size*2*0.5
halved = misc.imresize(normal, 50, 'bilinear').astype(int) # original size*2*0.5*0.5
quartered = misc.imresize(halved, 50, 'bilinear').astype(int) # original size*2*0.5*0.5*0.5
# Initialize Gaussian pyramids
pyrlvl1 = np.zeros((doubled.shape[0], doubled.shape[1], 6))
pyrlvl2 = np.zeros((normal.shape[0], normal.shape[1], 6))
pyrlvl3 = np.zeros((halved.shape[0], halved.shape[1], 6))
pyrlvl4 = np.zeros((quartered.shape[0], quartered.shape[1], 6))
print("Constructing pyramids...")
# Construct Gaussian pyramids
for i in range(0, 6):
pyrlvl1[:, :, i] = ndimage.filters.gaussian_filter(doubled, kvec1[i])
pyrlvl2[:, :, i] = misc.imresize(ndimage.filters.gaussian_filter(doubled, kvec2[i]), 50, 'bilinear')
pyrlvl3[:, :, i] = misc.imresize(ndimage.filters.gaussian_filter(doubled, kvec3[i]), 25, 'bilinear')
pyrlvl4[:, :, i] = misc.imresize(ndimage.filters.gaussian_filter(doubled, kvec4[i]), 1.0 / 8.0, 'bilinear')
# Initialize Difference-of-Gaussians (DoG) pyramids
diffpyrlvl1 = np.zeros((doubled.shape[0], doubled.shape[1], 5))
diffpyrlvl2 = np.zeros((normal.shape[0], normal.shape[1], 5))
diffpyrlvl3 = np.zeros((halved.shape[0], halved.shape[1], 5))
diffpyrlvl4 = np.zeros((quartered.shape[0], quartered.shape[1], 5))
# Construct DoG pyramids
for i in range(0, 5):
diffpyrlvl1[:, :, i] = pyrlvl1[:, :, i + 1] - pyrlvl1[:, :, i]
diffpyrlvl2[:, :, i] = pyrlvl2[:, :, i + 1] - pyrlvl2[:, :, i]
diffpyrlvl3[:, :, i] = pyrlvl3[:, :, i + 1] - pyrlvl3[:, :, i]
diffpyrlvl4[:, :, i] = pyrlvl4[:, :, i + 1] - pyrlvl4[:, :, i]
# Initialize pyramids to store extrema locations
extrpyrlvl1 = np.zeros((doubled.shape[0], doubled.shape[1], 3))
extrpyrlvl2 = np.zeros((normal.shape[0], normal.shape[1], 3))
extrpyrlvl3 = np.zeros((halved.shape[0], halved.shape[1], 3))
extrpyrlvl4 = np.zeros((quartered.shape[0], quartered.shape[1], 3))
print("Starting extrema detection...")
print("First octave")
# In each of the following for loops, elements of each pyramids that are larger or smaller than its 26 immediate neighbors in space and scale are labeled as extrema.
# As explained in section 4 of Lowe's paper, these initial extrema are pruned by checking that their contrast and curvature are above certain thresholds.
# The thresholds used here are those suggested by Lowe.
for i in range(1, 4):
for j in range(80, doubled.shape[0] - 80):
for k in range(80, doubled.shape[1] - 80):
if np.absolute(diffpyrlvl1[j, k, i]) < threshold:
continue
maxbool = (diffpyrlvl1[j, k, i] > 0)
minbool = (diffpyrlvl1[j, k, i] < 0)
for di in range(-1, 2):
for dj in range(-1, 2):
for dk in range(-1, 2):
if di == 0 and dj == 0 and dk == 0:
continue
maxbool = maxbool and (diffpyrlvl1[j, k, i] > diffpyrlvl1[j + dj, k + dk, i + di])
minbool = minbool and (diffpyrlvl1[j, k, i] < diffpyrlvl1[j + dj, k + dk, i + di])
if not maxbool and not minbool:
break
if not maxbool and not minbool:
break
if not maxbool and not minbool:
break
if maxbool or minbool:
dx = (diffpyrlvl1[j, k + 1, i] - diffpyrlvl1[j, k - 1, i]) * 0.5 / 255
dy = (diffpyrlvl1[j + 1, k, i] - diffpyrlvl1[j - 1, k, i]) * 0.5 / 255
ds = (diffpyrlvl1[j, k, i + 1] - diffpyrlvl1[j, k, i - 1]) * 0.5 / 255
dxx = (diffpyrlvl1[j, k + 1, i] + diffpyrlvl1[j, k - 1, i] - 2 * diffpyrlvl1[j, k, i]) * 1.0 / 255
dyy = (diffpyrlvl1[j + 1, k, i] + diffpyrlvl1[j - 1, k, i] - 2 * diffpyrlvl1[j, k, i]) * 1.0 / 255
dss = (diffpyrlvl1[j, k, i + 1] + diffpyrlvl1[j, k, i - 1] - 2 * diffpyrlvl1[j, k, i]) * 1.0 / 255
dxy = (diffpyrlvl1[j + 1, k + 1, i] - diffpyrlvl1[j + 1, k - 1, i] - diffpyrlvl1[j - 1, k + 1, i] +
diffpyrlvl1[j - 1, k - 1, i]) * 0.25 / 255
dxs = (diffpyrlvl1[j, k + 1, i + 1] - diffpyrlvl1[j, k - 1, i + 1] - diffpyrlvl1[j, k + 1, i - 1] +
diffpyrlvl1[j, k - 1, i - 1]) * 0.25 / 255
dys = (diffpyrlvl1[j + 1, k, i + 1] - diffpyrlvl1[j - 1, k, i + 1] - diffpyrlvl1[j + 1, k, i - 1] +
diffpyrlvl1[j - 1, k, i - 1]) * 0.25 / 255
dD = np.matrix([[dx], [dy], [ds]])
H = np.matrix([[dxx, dxy, dxs], [dxy, dyy, dys], [dxs, dys, dss]])
x_hat = numpy.linalg.lstsq(H, dD)[0]
D_x_hat = diffpyrlvl1[j, k, i] + 0.5 * np.dot(dD.transpose(), x_hat)
r = 10.0
if ((((dxx + dyy) ** 2) * r) < (dxx * dyy - (dxy ** 2)) * (((r + 1) ** 2))) and (
np.absolute(x_hat[0]) < 0.5) and (np.absolute(x_hat[1]) < 0.5) and (
np.absolute(x_hat[2]) < 0.5) and (np.absolute(D_x_hat) > 0.03):
extrpyrlvl1[j, k, i - 1] = 1
print("Second octave")
for i in range(1, 4):
for j in range(40, normal.shape[0] - 40):
for k in range(40, normal.shape[1] - 40):
if np.absolute(diffpyrlvl2[j, k, i]) < threshold:
continue
maxbool = (diffpyrlvl2[j, k, i] > 0)
minbool = (diffpyrlvl2[j, k, i] < 0)
for di in range(-1, 2):
for dj in range(-1, 2):
for dk in range(-1, 2):
if di == 0 and dj == 0 and dk == 0:
continue
maxbool = maxbool and (diffpyrlvl2[j, k, i] > diffpyrlvl2[j + dj, k + dk, i + di])
minbool = minbool and (diffpyrlvl2[j, k, i] < diffpyrlvl2[j + dj, k + dk, i + di])
if not maxbool and not minbool:
break
if not maxbool and not minbool:
break
if not maxbool and not minbool:
break
if maxbool or minbool:
dx = (diffpyrlvl2[j, k + 1, i] - diffpyrlvl2[j, k - 1, i]) * 0.5 / 255
dy = (diffpyrlvl2[j + 1, k, i] - diffpyrlvl2[j - 1, k, i]) * 0.5 / 255
ds = (diffpyrlvl2[j, k, i + 1] - diffpyrlvl2[j, k, i - 1]) * 0.5 / 255
dxx = (diffpyrlvl2[j, k + 1, i] + diffpyrlvl2[j, k - 1, i] - 2 * diffpyrlvl2[j, k, i]) * 1.0 / 255
dyy = (diffpyrlvl2[j + 1, k, i] + diffpyrlvl2[j - 1, k, i] - 2 * diffpyrlvl2[j, k, i]) * 1.0 / 255
dss = (diffpyrlvl2[j, k, i + 1] + diffpyrlvl2[j, k, i - 1] - 2 * diffpyrlvl2[j, k, i]) * 1.0 / 255
dxy = (diffpyrlvl2[j + 1, k + 1, i] - diffpyrlvl2[j + 1, k - 1, i] - diffpyrlvl2[j - 1, k + 1, i] +
diffpyrlvl2[j - 1, k - 1, i]) * 0.25 / 255
dxs = (diffpyrlvl2[j, k + 1, i + 1] - diffpyrlvl2[j, k - 1, i + 1] - diffpyrlvl2[j, k + 1, i - 1] +
diffpyrlvl2[j, k - 1, i - 1]) * 0.25 / 255
dys = (diffpyrlvl2[j + 1, k, i + 1] - diffpyrlvl2[j - 1, k, i + 1] - diffpyrlvl2[j + 1, k, i - 1] +
diffpyrlvl2[j - 1, k, i - 1]) * 0.25 / 255
dD = np.matrix([[dx], [dy], [ds]])
H = np.matrix([[dxx, dxy, dxs], [dxy, dyy, dys], [dxs, dys, dss]])
x_hat = numpy.linalg.lstsq(H, dD)[0]
D_x_hat = diffpyrlvl2[j, k, i] + 0.5 * np.dot(dD.transpose(), x_hat)
r = 10.0
if (((dxx + dyy) ** 2) * r) < (dxx * dyy - (dxy ** 2)) * (((r + 1) ** 2)) and np.absolute(
x_hat[0]) < 0.5 and np.absolute(x_hat[1]) < 0.5 and np.absolute(
x_hat[2]) < 0.5 and np.absolute(D_x_hat) > 0.03:
extrpyrlvl2[j, k, i - 1] = 1
print("Third octave")
for i in range(1, 4):
for j in range(20, halved.shape[0] - 20):
for k in range(20, halved.shape[1] - 20):
if np.absolute(diffpyrlvl3[j, k, i]) < threshold:
continue
maxbool = (diffpyrlvl3[j, k, i] > 0)
minbool = (diffpyrlvl3[j, k, i] < 0)
for di in range(-1, 2):
for dj in range(-1, 2):
for dk in range(-1, 2):
if di == 0 and dj == 0 and dk == 0:
continue
maxbool = maxbool and (diffpyrlvl3[j, k, i] > diffpyrlvl3[j + dj, k + dk, i + di])
minbool = minbool and (diffpyrlvl3[j, k, i] < diffpyrlvl3[j + dj, k + dk, i + di])
if not maxbool and not minbool:
break
if not maxbool and not minbool:
break
if not maxbool and not minbool:
break
if maxbool or minbool:
dx = (diffpyrlvl3[j, k + 1, i] - diffpyrlvl3[j, k - 1, i]) * 0.5 / 255
dy = (diffpyrlvl3[j + 1, k, i] - diffpyrlvl3[j - 1, k, i]) * 0.5 / 255
ds = (diffpyrlvl3[j, k, i + 1] - diffpyrlvl3[j, k, i - 1]) * 0.5 / 255
dxx = (diffpyrlvl3[j, k + 1, i] + diffpyrlvl3[j, k - 1, i] - 2 * diffpyrlvl3[j, k, i]) * 1.0 / 255
dyy = (diffpyrlvl3[j + 1, k, i] + diffpyrlvl3[j - 1, k, i] - 2 * diffpyrlvl3[j, k, i]) * 1.0 / 255
dss = (diffpyrlvl3[j, k, i + 1] + diffpyrlvl3[j, k, i - 1] - 2 * diffpyrlvl3[j, k, i]) * 1.0 / 255
dxy = (diffpyrlvl3[j + 1, k + 1, i] - diffpyrlvl3[j + 1, k - 1, i] - diffpyrlvl3[j - 1, k + 1, i] +
diffpyrlvl3[j - 1, k - 1, i]) * 0.25 / 255
dxs = (diffpyrlvl3[j, k + 1, i + 1] - diffpyrlvl3[j, k - 1, i + 1] - diffpyrlvl3[j, k + 1, i - 1] +
diffpyrlvl3[j, k - 1, i - 1]) * 0.25 / 255
dys = (diffpyrlvl3[j + 1, k, i + 1] - diffpyrlvl3[j - 1, k, i + 1] - diffpyrlvl3[j + 1, k, i - 1] +
diffpyrlvl3[j - 1, k, i - 1]) * 0.25 / 255
dD = np.matrix([[dx], [dy], [ds]])
H = np.matrix([[dxx, dxy, dxs], [dxy, dyy, dys], [dxs, dys, dss]])
x_hat = numpy.linalg.lstsq(H, dD)[0]
D_x_hat = diffpyrlvl3[j, k, i] + 0.5 * np.dot(dD.transpose(), x_hat)
r = 10.0
if (((dxx + dyy) ** 2) * r) < (dxx * dyy - (dxy ** 2)) * (((r + 1) ** 2)) and np.absolute(
x_hat[0]) < 0.5 and np.absolute(x_hat[1]) < 0.5 and np.absolute(
x_hat[2]) < 0.5 and np.absolute(D_x_hat) > 0.03:
extrpyrlvl3[j, k, i - 1] = 1
print("Fourth octave")
for i in range(1, 4):
for j in range(10, quartered.shape[0] - 10):
for k in range(10, quartered.shape[1] - 10):
if np.absolute(diffpyrlvl4[j, k, i]) < threshold:
continue
maxbool = (diffpyrlvl4[j, k, i] > 0)
minbool = (diffpyrlvl4[j, k, i] < 0)
for di in range(-1, 2):
for dj in range(-1, 2):
for dk in range(-1, 2):
if di == 0 and dj == 0 and dk == 0:
continue
maxbool = maxbool and (diffpyrlvl4[j, k, i] > diffpyrlvl4[j + dj, k + dk, i + di])
minbool = minbool and (diffpyrlvl4[j, k, i] < diffpyrlvl4[j + dj, k + dk, i + di])
if not maxbool and not minbool:
break
if not maxbool and not minbool:
break
if not maxbool and not minbool:
break
if maxbool or minbool:
dx = (diffpyrlvl4[j, k + 1, i] - diffpyrlvl4[j, k - 1, i]) * 0.5 / 255
dy = (diffpyrlvl4[j + 1, k, i] - diffpyrlvl4[j - 1, k, i]) * 0.5 / 255
ds = (diffpyrlvl4[j, k, i + 1] - diffpyrlvl4[j, k, i - 1]) * 0.5 / 255
dxx = (diffpyrlvl4[j, k + 1, i] + diffpyrlvl4[j, k - 1, i] - 2 * diffpyrlvl4[j, k, i]) * 1.0 / 255
dyy = (diffpyrlvl4[j + 1, k, i] + diffpyrlvl4[j - 1, k, i] - 2 * diffpyrlvl4[j, k, i]) * 1.0 / 255
dss = (diffpyrlvl4[j, k, i + 1] + diffpyrlvl4[j, k, i - 1] - 2 * diffpyrlvl4[j, k, i]) * 1.0 / 255
dxy = (diffpyrlvl4[j + 1, k + 1, i] - diffpyrlvl4[j + 1, k - 1, i] - diffpyrlvl4[j - 1, k + 1, i] +
diffpyrlvl4[j - 1, k - 1, i]) * 0.25 / 255
dxs = (diffpyrlvl4[j, k + 1, i + 1] - diffpyrlvl4[j, k - 1, i + 1] - diffpyrlvl4[j, k + 1, i - 1] +
diffpyrlvl4[j, k - 1, i - 1]) * 0.25 / 255
dys = (diffpyrlvl4[j + 1, k, i + 1] - diffpyrlvl4[j - 1, k, i + 1] - diffpyrlvl4[j + 1, k, i - 1] +
diffpyrlvl4[j - 1, k, i - 1]) * 0.25 / 255
dD = np.matrix([[dx], [dy], [ds]])
H = np.matrix([[dxx, dxy, dxs], [dxy, dyy, dys], [dxs, dys, dss]])
x_hat = numpy.linalg.lstsq(H, dD)[0]
D_x_hat = diffpyrlvl4[j, k, i] + 0.5 * np.dot(dD.transpose(), x_hat)
r = 10.0
if (((dxx + dyy) ** 2) * r) < (dxx * dyy - (dxy ** 2)) * (((r + 1) ** 2)) and np.absolute(
x_hat[0]) < 0.5 and np.absolute(x_hat[1]) < 0.5 and np.absolute(
x_hat[2]) < 0.5 and np.absolute(D_x_hat) > 0.03:
extrpyrlvl4[j, k, i - 1] = 1
print("Number of extrema in first octave: %d" % np.sum(extrpyrlvl1))
print("Number of extrema in second octave: %d" % np.sum(extrpyrlvl2))
print("Number of extrema in third octave: %d" % np.sum(extrpyrlvl3))
print("Number of extrema in fourth octave: %d" % np.sum(extrpyrlvl4))
# Gradient magnitude and orientation for each image sample point at each scale
magpyrlvl1 = np.zeros((doubled.shape[0], doubled.shape[1], 3))
magpyrlvl2 = np.zeros((normal.shape[0], normal.shape[1], 3))
magpyrlvl3 = np.zeros((halved.shape[0], halved.shape[1], 3))
magpyrlvl4 = np.zeros((quartered.shape[0], quartered.shape[1], 3))
oripyrlvl1 = np.zeros((doubled.shape[0], doubled.shape[1], 3))
oripyrlvl2 = np.zeros((normal.shape[0], normal.shape[1], 3))
oripyrlvl3 = np.zeros((halved.shape[0], halved.shape[1], 3))
oripyrlvl4 = np.zeros((quartered.shape[0], quartered.shape[1], 3))
for i in range(0, 3):
for j in range(1, doubled.shape[0] - 1):
for k in range(1, doubled.shape[1] - 1):
magpyrlvl1[j, k, i] = (((doubled[j + 1, k] - doubled[j - 1, k]) ** 2) + (
(doubled[j, k + 1] - doubled[j, k - 1]) ** 2)) ** 0.5
oripyrlvl1[j, k, i] = (36 / (2 * np.pi)) * (np.pi + np.arctan2((doubled[j, k + 1] - doubled[j, k - 1]),
(doubled[j + 1, k] - doubled[j - 1, k])))
for i in range(0, 3):
for j in range(1, normal.shape[0] - 1):
for k in range(1, normal.shape[1] - 1):
magpyrlvl2[j, k, i] = (((normal[j + 1, k] - normal[j - 1, k]) ** 2) + (
(normal[j, k + 1] - normal[j, k - 1]) ** 2)) ** 0.5
oripyrlvl2[j, k, i] = (36 / (2 * np.pi)) * (np.pi + np.arctan2((normal[j, k + 1] - normal[j, k - 1]),
(normal[j + 1, k] - normal[j - 1, k])))
for i in range(0, 3):
for j in range(1, halved.shape[0] - 1):
for k in range(1, halved.shape[1] - 1):
magpyrlvl3[j, k, i] = (((halved[j + 1, k] - halved[j - 1, k]) ** 2) + (
(halved[j, k + 1] - halved[j, k - 1]) ** 2)) ** 0.5
oripyrlvl3[j, k, i] = (36 / (2 * np.pi)) * (np.pi + np.arctan2((halved[j, k + 1] - halved[j, k - 1]),
(halved[j + 1, k] - halved[j - 1, k])))
for i in range(0, 3):
for j in range(1, quartered.shape[0] - 1):
for k in range(1, quartered.shape[1] - 1):
magpyrlvl4[j, k, i] = (((quartered[j + 1, k] - quartered[j - 1, k]) ** 2) + (
(quartered[j, k + 1] - quartered[j, k - 1]) ** 2)) ** 0.5
oripyrlvl4[j, k, i] = (36 / (2 * np.pi)) * (
np.pi + np.arctan2((quartered[j, k + 1] - quartered[j, k - 1]),
(quartered[j + 1, k] - quartered[j - 1, k])))
extr_sum = np.sum(extrpyrlvl1) + np.sum(extrpyrlvl2) + np.sum(extrpyrlvl3) + np.sum(extrpyrlvl4) # total extremas
print("extr_sum:{}".format(extr_sum)) # extr_sum:772.0
# keypoints is an n by 4 numpy array that holds the n keypoints (the first column is the image row coordinate,
# the second column is the image column coordinate, the third column is the scale, and the fourth column is the orientation as a bin index)
keypoints = np.zeros((int(extr_sum), 4))
print("Calculating keypoint orientations...")
count = 0
for i in range(0, 3):
for j in range(80, doubled.shape[0] - 80):
for k in range(80, doubled.shape[1] - 80):
if extrpyrlvl1[j, k, i] == 1:
gaussian_window = multivariate_normal(mean=[j, k], cov=((1.5 * kvectotal[i]) ** 2))
two_sd = np.floor(2 * 1.5 * kvectotal[i])
orient_hist = np.zeros([36, 1])
for x in range(int(-1 * two_sd * 2), int(two_sd * 2) + 1):
ylim = int((((two_sd * 2) ** 2) - (np.absolute(x) ** 2)) ** 0.5)
for y in range(-1 * ylim, ylim + 1):
if j + x < 0 or j + x > doubled.shape[0] - 1 or k + y < 0 or k + y > doubled.shape[1] - 1:
continue
weight = magpyrlvl1[j + x, k + y, i] * gaussian_window.pdf([j + x, k + y])
bin_idx = np.clip(np.floor(oripyrlvl1[j + x, k + y, i]), 0, 35)
orient_hist[int(np.floor(bin_idx))] += weight
maxval = np.amax(orient_hist)
maxidx = np.argmax(orient_hist)
keypoints[count, :] = np.array([int(j * 0.5), int(k * 0.5), kvectotal[i], maxidx]) # because the doubled image= origin*2, thus it need to *0.5 to look for corresponding pixel location
count += 1
orient_hist[maxidx] = 0
newmaxval = np.amax(orient_hist)
while newmaxval > 0.8 * maxval:
newmaxidx = np.argmax(orient_hist)
np.append(keypoints, np.array([[int(j * 0.5), int(k * 0.5), kvectotal[i], newmaxidx]]), axis=0)
orient_hist[newmaxidx] = 0
newmaxval = np.amax(orient_hist)
for i in range(0, 3):
for j in range(40, normal.shape[0] - 40):
for k in range(40, normal.shape[1] - 40):
if extrpyrlvl2[j, k, i] == 1:
gaussian_window = multivariate_normal(mean=[j, k], cov=((1.5 * kvectotal[i + 3]) ** 2))
two_sd = np.floor(2 * 1.5 * kvectotal[i + 3])
orient_hist = np.zeros([36, 1])
for x in range(int(-1 * two_sd), int(two_sd + 1)):
ylim = int(((two_sd ** 2) - (np.absolute(x) ** 2)) ** 0.5)
for y in range(-1 * ylim, ylim + 1):
if j + x < 0 or j + x > normal.shape[0] - 1 or k + y < 0 or k + y > normal.shape[1] - 1:
continue
weight = magpyrlvl2[j + x, k + y, i] * gaussian_window.pdf([j + x, k + y])
bin_idx = np.clip(np.floor(oripyrlvl2[j + x, k + y, i]), 0, 35)
orient_hist[int(np.floor(bin_idx))] += weight
maxval = np.amax(orient_hist)
maxidx = np.argmax(orient_hist)
keypoints[count, :] = np.array([j, k, kvectotal[i + 3], maxidx]) # corresponding pixel location
count += 1
orient_hist[maxidx] = 0
newmaxval = np.amax(orient_hist)
while newmaxval > 0.8 * maxval:
newmaxidx = np.argmax(orient_hist)
np.append(keypoints, np.array([[j, k, kvectotal[i + 3], newmaxidx]]), axis=0)
orient_hist[newmaxidx] = 0
newmaxval = np.amax(orient_hist)
for i in range(0, 3):
for j in range(20, halved.shape[0] - 20):
for k in range(20, halved.shape[1] - 20):
if extrpyrlvl3[j, k, i] == 1:
gaussian_window = multivariate_normal(mean=[j, k], cov=((1.5 * kvectotal[i + 6]) ** 2))
two_sd = np.floor(2 * 1.5 * kvectotal[i + 6])
orient_hist = np.zeros([36, 1])
for x in range(int(-1 * two_sd * 0.5), int(two_sd * 0.5) + 1):
ylim = int((((two_sd * 0.5) ** 2) - (np.absolute(x) ** 2)) ** 0.5)
for y in range(-1 * ylim, ylim + 1):
if j + x < 0 or j + x > halved.shape[0] - 1 or k + y < 0 or k + y > halved.shape[1] - 1:
continue
weight = magpyrlvl3[j + x, k + y, i] * gaussian_window.pdf([j + x, k + y])
bin_idx = np.clip(np.floor(oripyrlvl3[j + x, k + y, i]), 0, 35)
orient_hist[int(np.floor(bin_idx))] += weight
maxval = np.amax(orient_hist)
maxidx = np.argmax(orient_hist)
keypoints[count, :] = np.array([j * 2, k * 2, kvectotal[i + 6], maxidx]) # corresponding pixel location
count += 1
orient_hist[maxidx] = 0
newmaxval = np.amax(orient_hist)
while newmaxval > 0.8 * maxval:
newmaxidx = np.argmax(orient_hist)
np.append(keypoints, np.array([[j * 2, k * 2, kvectotal[i + 6], newmaxidx]]), axis=0)
orient_hist[newmaxidx] = 0
newmaxval = np.amax(orient_hist)
for i in range(0, 3):
for j in range(10, quartered.shape[0] - 10):
for k in range(10, quartered.shape[1] - 10):
if extrpyrlvl4[j, k, i] == 1:
gaussian_window = multivariate_normal(mean=[j, k], cov=((1.5 * kvectotal[i + 9]) ** 2))
two_sd = np.floor(2 * 1.5 * kvectotal[i + 9])
orient_hist = np.zeros([36, 1])
for x in range(int(-1 * two_sd * 0.25), int(two_sd * 0.25) + 1):
ylim = int((((two_sd * 0.25) ** 2) - (np.absolute(x) ** 2)) ** 0.5)
for y in range(-1 * ylim, ylim + 1):
if j + x < 0 or j + x > quartered.shape[0] - 1 or k + y < 0 or k + y > quartered.shape[
1] - 1:
continue
weight = magpyrlvl4[j + x, k + y, i] * gaussian_window.pdf([j + x, k + y])
bin_idx = np.clip(np.floor(oripyrlvl4[j + x, k + y, i]), 0, 35)
orient_hist[int(np.floor(bin_idx))] += weight
maxval = np.amax(orient_hist)
maxidx = np.argmax(orient_hist)
keypoints[count, :] = np.array([j * 4, k * 4, kvectotal[i + 9], maxidx]) # corresponding pixel location
count += 1
orient_hist[maxidx] = 0
newmaxval = np.amax(orient_hist)
while newmaxval > 0.8 * maxval:
newmaxidx = np.argmax(orient_hist)
np.append(keypoints, np.array([[j * 4, k * 4, kvectotal[i + 9], newmaxidx]]), axis=0)
orient_hist[newmaxidx] = 0
newmaxval = np.amax(orient_hist)
print("Calculating descriptor...")
magpyr = np.zeros((normal.shape[0], normal.shape[1], 12))
oripyr = np.zeros((normal.shape[0], normal.shape[1], 12))
for i in range(0, 3):
magmax = np.amax(magpyrlvl1[:, :, i])
magpyr[:, :, i] = misc.imresize(magpyrlvl1[:, :, i], (normal.shape[0], normal.shape[1]), "bilinear").astype(
float)
magpyr[:, :, i] = (magmax / np.amax(magpyr[:, :, i])) * magpyr[:, :, i]
oripyr[:, :, i] = misc.imresize(oripyrlvl1[:, :, i], (normal.shape[0], normal.shape[1]), "bilinear").astype(int)
oripyr[:, :, i] = ((36.0 / np.amax(oripyr[:, :, i])) * oripyr[:, :, i]).astype(int)
for i in range(0, 3):
magpyr[:, :, i + 3] = (magpyrlvl2[:, :, i]).astype(float)
oripyr[:, :, i + 3] = (oripyrlvl2[:, :, i]).astype(int)
for i in range(0, 3):
magpyr[:, :, i + 6] = misc.imresize(magpyrlvl3[:, :, i], (normal.shape[0], normal.shape[1]), "bilinear").astype(
int)
oripyr[:, :, i + 6] = misc.imresize(oripyrlvl3[:, :, i], (normal.shape[0], normal.shape[1]), "bilinear").astype(
int)
for i in range(0, 3):
magpyr[:, :, i + 9] = misc.imresize(magpyrlvl4[:, :, i], (normal.shape[0], normal.shape[1]), "bilinear").astype(
int)
oripyr[:, :, i + 9] = misc.imresize(oripyrlvl4[:, :, i], (normal.shape[0], normal.shape[1]), "bilinear").astype(
int)
descriptors = np.zeros([keypoints.shape[0], 128])
for i in range(0, keypoints.shape[0]):
for x in range(-8, 8):
for y in range(-8, 8):
theta = 10 * keypoints[i, 3] * np.pi / 180.0
xrot = np.round((np.cos(theta) * x) - (np.sin(theta) * y))
yrot = np.round((np.sin(theta) * x) + (np.cos(theta) * y))
scale_idx = np.argwhere(kvectotal == keypoints[i, 2])[0][0]
x0 = keypoints[i, 0]
y0 = keypoints[i, 1]
gaussian_window = multivariate_normal(mean=[x0, y0], cov=8)
weight = magpyr[int(x0 + xrot), int(y0 + yrot), scale_idx] * gaussian_window.pdf([x0 + xrot, y0 + yrot])
angle = oripyr[int(x0 + xrot), int(y0 + yrot), scale_idx] - keypoints[i, 3]
if angle < 0:
angle = 36 + angle
bin_idx = np.clip(np.floor((8.0 / 36) * angle), 0, 7).astype(int)
descriptors[i, 32 * int((x + 8) / 4) + 8 * int((y + 8) / 4) + bin_idx] += weight
descriptors[i, :] = descriptors[i, :] / norm(descriptors[i, :])
descriptors[i, :] = np.clip(descriptors[i, :], 0, 0.2)
descriptors[i, :] = descriptors[i, :] / norm(descriptors[i, :])
return [keypoints, descriptors]
def read_features(in_fname):
"""
read image features from a file
:param in_fname: file path
:return: [keypoints, descriptor]
"""
infile = open(in_fname, 'r')
if infile is None:
return ([], [])
tokens = infile.readline().split()
assert (len(tokens) == 2)
num_pts = int(tokens[0])
num_dims = int(tokens[1])
descs = np.zeros((num_pts, num_dims), np.float32)
kps = []
values = infile.read().split()
j = 0
for i in range(0, num_pts):
x = values[j]
j += 1
y = values[j]
j += 1
sz = values[j]
j += 1
angle = values[j]
j += 1
descs[i, :] = values[j:j + num_dims]
j += num_dims
kps.append(cv2.KeyPoint( float(x), float(y), float(sz), float(angle)))
# for i in range(len(kps)):
# print(kps.pt[0])
# print(kps.pt[1])
# print(kps.size)
# print(kps.angle)
return (kps, descs)
def write_features(out_fname, kps, descs):
"""
write [keypoints, descriptor] got by SIFT algorithm to out_fname file
:param out_fname: file path
:param kps: keypoints
:param descs: descriptors
:return:
"""
outfile = open(out_fname, 'w')
num_pts = len(kps)
if num_pts == 0:
line = "0 128\n"
outfile.write(line)
outfile.close()
return
num_dims = descs.shape[1]
assert (descs.shape[0] == num_pts)
line = "%d %d" % (num_pts, num_dims)
outfile.write(line)
for i in range(0, num_pts):
kp = kps[i]
desc = descs[i, :]
outfile.write("\n%f %f %f %f" % (kp[0], kp[1], kp[2], kp[3]))
for i in range(0, num_dims):
if i % 20 == 0:
outfile.write('\n')
else:
outfile.write(' ')
outfile.write('%f' % desc[i])
if __name__=="__main__":
# imagename="pattern_resized.png"
# threshold=5
# out_fname='pattern_keypoints.txt'
# # original = ndimage.imread(imagename, flatten=True)
# # print(original.shape)
#
# [keypoints, descriptors] = detect_keypoints(imagename, threshold)
# print("keypoints: {}".format(keypoints))
# print("descriptors: {}".format(descriptors))
# write_features(out_fname, keypoints, descriptors)
# pattern_keys = read_features('keypoints_dir/left01.txt')[0]
# for i in range(len(pattern_keys)):
# print(pattern_keys[i].pt)
# print(pattern_keys[i].size)
# print(pattern_keys[i].angle)
# print(pattern_keys)
import os
img_root='meilan_note6_resized'
img_names=os.listdir(img_root)
img_paths=[os.path.join(img_root,f) for f in img_names]
threshold = 5
print(img_paths)
for i,pimg_path in enumerate(img_paths):
[keypoints, descriptors] = detect_keypoints(pimg_path, threshold)
write_features('keypoints_dir/left{:02d}.txt'.format(i+1), keypoints, descriptors)