-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSIFT.py
More file actions
381 lines (308 loc) · 16.3 KB
/
Copy pathSIFT.py
File metadata and controls
381 lines (308 loc) · 16.3 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
'''
Created on Apr 1, 2015
@author: kena
'''
####################################################################
#
# Python module to perform SIFT_orig feature detection. this is
# a simple implementation of the SIFT_orig algorithm (Lowe 2004)
#
####################################################################
import cv2
import numpy as np
from cv2 import waitKey, DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS
#from cv2 import INTER_LINEAR
#===========================Global Variables and Parameters=============================
iDistMin = 0.5
sigmaMin = 0.50
stackSpace = 3
numOctaves = 3
CTreshold = 0.0315 # This is reduced to prevent too many points being discarded on contrast
curveRatio = 5 # Edgeness ratio used to discard points on edges
lbda = 1.5 #lambda, gaussian window size
#---------------------------descriptor parameters------------------------------------------
dWindow = 16
numBins = 36
numHist = 4
numHBins = 8
lbdaDesc = 8
#----------------------------scale space structures----------------------------------------
oStack = np.empty([numOctaves,stackSpace+3], dtype=object) #structure for holding the octaves of stacks
diffOfGauss = np.empty([numOctaves,stackSpace+2], dtype=object) #structure for holding the difference of Gaussians
#----------------------------key points structures-----------------------------------------
#structure for holding the keypoints at different scales
kPoints = np.empty([1,4],dtype=np.float)
#np.delete(kPoints,0)
ipolKPoints = np.empty([1,8],dtype = np.float)
mStack = np.empty([numOctaves], dtype = object)
orStack = np.empty([numOctaves], dtype = object)
finKPoints = np.empty([1,8],dtype = np.float)
# convKeyPoints = np.
#==========================================================================================
def getImage(url):
try:
imageRead = cv2.imread(url, 0)
image = cv2.resize(imageRead,(int(imageRead.shape[0]/0.5),int(imageRead.shape[1]/0.5)),0,0)
initImage = cv2.normalize(image.astype('float'), None, 0.0, 1.0, cv2.NORM_MINMAX)
return initImage, imageRead
except:
return None, None
def GBlur(image, sigma):
outputArr = cv2.GaussianBlur(image,(0,0),sigma)
return outputArr
def resample(image):
W = image.shape[1]; W2 = int(W/2)
H = image.shape[0]; H2 = int(H/2)
output = np.empty([H2,W2])
for i in range(H2):
for j in range(W2):
output[int(i),int(j)] = image[i*2,j*2]
return output
def BuildOctaves(initImage):
#compute the first octave. The initial image is resized and Interpolated by a factor of 2
iDist = iDistMin
oStack[0,0] = initImage
for j in range(1,stackSpace+3):
k = (2.0**(float(j)/float(stackSpace)))
sigma = (iDist/iDistMin)*sigmaMin*k
oStack[0,j] = GBlur(oStack[0,0], sigma)
#build other octaves
for i in range(1,numOctaves):
iDist = iDistMin*(2**i)
oStack[i,0] = resample(oStack[i-1,oStack[i-1].shape[0]-3])
for j in range(1,stackSpace+3):
k = (2**(float(j)/float(stackSpace)))
sigma = (iDist/iDistMin)*sigmaMin*k
oStack[i,j] = GBlur(oStack[i,0], sigma)
def DiffOfGauss():
for i in range(numOctaves):
for j in range(stackSpace+2):
diffOfGauss[i,j] = oStack[i,j+1] - oStack[i,j]
def LocalExtrema():
global kPoints
for a in range(numOctaves):
for b in range(2,stackSpace):
#find the keypoints
for i in range(1,diffOfGauss[a,0].shape[0]-1):
for j in range(1,diffOfGauss[a,0].shape[1]-1):
#is a local minimum
if (((diffOfGauss[a,b][i,j] < diffOfGauss[a,b][i-1,j-1]) and
(diffOfGauss[a,b][i,j] < diffOfGauss[a,b][i-1,j]) and
(diffOfGauss[a,b][i,j] < diffOfGauss[a,b][i-1,j+1]) and
(diffOfGauss[a,b][i,j] < diffOfGauss[a,b][i+1,j-1]) and
(diffOfGauss[a,b][i,j] < diffOfGauss[a,b][i+1,j]) and
(diffOfGauss[a,b][i,j] < diffOfGauss[a,b][i+1,j+1]) and
(diffOfGauss[a,b][i,j] < diffOfGauss[a,b][i,j-1]) and
(diffOfGauss[a,b][i,j] < diffOfGauss[a,b][i,j+1]) and
(diffOfGauss[a,b][i,j] < diffOfGauss[a,b-1][i-1,j-1]) and
(diffOfGauss[a,b][i,j] < diffOfGauss[a,b-1][i-1,j]) and
(diffOfGauss[a,b][i,j] < diffOfGauss[a,b-1][i-1,j+1]) and
(diffOfGauss[a,b][i,j] < diffOfGauss[a,b-1][i+1,j-1]) and
(diffOfGauss[a,b][i,j] < diffOfGauss[a,b-1][i+1,j]) and
(diffOfGauss[a,b][i,j] < diffOfGauss[a,b-1][i+1,j+1]) and
(diffOfGauss[a,b][i,j] < diffOfGauss[a,b-1][i,j-1]) and
(diffOfGauss[a,b][i,j] < diffOfGauss[a,b-1][i,j]) and
(diffOfGauss[a,b][i,j] < diffOfGauss[a,b-1][i,j+1]) and
(diffOfGauss[a,b][i,j] < diffOfGauss[a,b+1][i-1,j-1]) and
(diffOfGauss[a,b][i,j] < diffOfGauss[a,b+1][i-1,j]) and
(diffOfGauss[a,b][i,j] < diffOfGauss[a,b+1][i-1,j+1]) and
(diffOfGauss[a,b][i,j] < diffOfGauss[a,b+1][i+1,j-1]) and
(diffOfGauss[a,b][i,j] < diffOfGauss[a,b+1][i+1,j]) and
(diffOfGauss[a,b][i,j] < diffOfGauss[a,b+1][i+1,j+1]) and
(diffOfGauss[a,b][i,j] < diffOfGauss[a,b+1][i,j-1]) and
(diffOfGauss[a,b][i,j] < diffOfGauss[a,b+1][i,j]) and
(diffOfGauss[a,b][i,j] < diffOfGauss[a,b+1][i,j+1])) or
#is a local maximum
((diffOfGauss[a,b][i,j] > diffOfGauss[a,b][i-1,j-1]) and
(diffOfGauss[a,b][i,j] > diffOfGauss[a,b][i-1,j]) and
(diffOfGauss[a,b][i,j] > diffOfGauss[a,b][i-1,j+1]) and
(diffOfGauss[a,b][i,j] > diffOfGauss[a,b][i+1,j-1]) and
(diffOfGauss[a,b][i,j] > diffOfGauss[a,b][i+1,j]) and
(diffOfGauss[a,b][i,j] > diffOfGauss[a,b][i+1,j+1]) and
(diffOfGauss[a,b][i,j] > diffOfGauss[a,b][i,j-1]) and
(diffOfGauss[a,b][i,j] > diffOfGauss[a,b][i,j+1]) and
(diffOfGauss[a,b][i,j] > diffOfGauss[a,b-1][i-1,j-1]) and
(diffOfGauss[a,b][i,j] > diffOfGauss[a,b-1][i-1,j]) and
(diffOfGauss[a,b][i,j] > diffOfGauss[a,b-1][i-1,j+1]) and
(diffOfGauss[a,b][i,j] > diffOfGauss[a,b-1][i+1,j-1]) and
(diffOfGauss[a,b][i,j] > diffOfGauss[a,b-1][i+1,j]) and
(diffOfGauss[a,b][i,j] > diffOfGauss[a,b-1][i+1,j+1]) and
(diffOfGauss[a,b][i,j] > diffOfGauss[a,b-1][i,j-1]) and
(diffOfGauss[a,b][i,j] > diffOfGauss[a,b-1][i,j]) and
(diffOfGauss[a,b][i,j] > diffOfGauss[a,b-1][i,j+1]) and
(diffOfGauss[a,b][i,j] > diffOfGauss[a,b+1][i-1,j-1]) and
(diffOfGauss[a,b][i,j] > diffOfGauss[a,b+1][i-1,j]) and
(diffOfGauss[a,b][i,j] > diffOfGauss[a,b+1][i-1,j+1]) and
(diffOfGauss[a,b][i,j] > diffOfGauss[a,b+1][i+1,j-1]) and
(diffOfGauss[a,b][i,j] > diffOfGauss[a,b+1][i+1,j]) and
(diffOfGauss[a,b][i,j] > diffOfGauss[a,b+1][i+1,j+1]) and
(diffOfGauss[a,b][i,j] > diffOfGauss[a,b+1][i,j-1]) and
(diffOfGauss[a,b][i,j] > diffOfGauss[a,b+1][i,j]) and
(diffOfGauss[a,b][i,j] > diffOfGauss[a,b+1][i,j+1]))): #the point is a max or a min
kPoints = np.insert(kPoints, 0, [a,b,i,j]) #(kPoints,1,[a,b,i,j],axis=0)
#delete the last record. this is needed to remove the initial values in the array
for i in range(0,4):
kPoints = np.delete(kPoints, kPoints.shape[0]-1)
def KeyPointsFilter():
global ipolKPoints
xCap = np.empty([1,2])
#validpoint(a,b,i,j,x,y,sigma,tetha)
#interpolate the points
for inc in range(0,kPoints.shape[0],4):
# 8 dimensional array
candidatePoint = np.array([kPoints[inc],kPoints[inc+1],kPoints[inc+2],kPoints[inc+3],0.0,0.0,0.0,0.0],dtype = float)
success = False
tries = 0
while (tries < 5):
a = candidatePoint[0]; b = candidatePoint[1]
i = candidatePoint[2]; j = candidatePoint[3]
if (not(((0 < i ) and (i < diffOfGauss[a,2].shape[0]-1)) and ((0 < j ) and (j < diffOfGauss[a,1].shape[1]-1)))):
break #point out of bounds
gMatrix = np.matrix([((diffOfGauss[a,b][i+1,j])-(diffOfGauss[a,b][i-1,j]))/2, #column difference
((diffOfGauss[a,b][i,j+1])-(diffOfGauss[a,b][i,j-1]))/2]) #row difference
HMatrix = np.matrix([[(diffOfGauss[a,b][i+1,j])+(diffOfGauss[a,b][i-1,j])-(2*diffOfGauss[a,b][i,j]),((diffOfGauss[a,b][i+1,j+1])-(diffOfGauss[a,b][i+1,j-1])-(diffOfGauss[a,b][i-1,j+1])+(diffOfGauss[a,b][i-1,j-1]))/4],
[((diffOfGauss[a,b][i+1,j+1])-(diffOfGauss[a,b][i+1,j-1])-(diffOfGauss[a,b][i-1,j+1])+(diffOfGauss[a,b][i-1,j-1]))/4,(diffOfGauss[a,b][i,j+1])+(diffOfGauss[a,b][i,j-1])-(2*diffOfGauss[a,b][i,j])]])
Trace = np.trace(HMatrix)
Det = np.linalg.det(HMatrix)
Harris = (Trace**2)/Det
xCap = gMatrix.dot(-(np.linalg.inv(HMatrix)))
if (abs(xCap[0,0]) < 0.6 and abs(xCap[0,1]) < 0.6 ): #only test for difference of x,y
success = True
iDist = iDistMin*(2**a)
k = (2**(float(b)/float(stackSpace)))
sigma = (iDist/iDistMin)*sigmaMin*k
candidatePoint[4] = round(iDist*(xCap[0,0]+i)) #i increment
candidatePoint[5] = round(iDist*(xCap[0,1]+j)) #j increment
DxCap = diffOfGauss[a,b][i,j] - (gMatrix.dot(HMatrix.dot(gMatrix.transpose()))/2)
candidatePoint[6] = sigma
break
else:
#round these values ---
candidatePoint[2] += round(xCap[0,0])
candidatePoint[3] += round(xCap[0,1])
tries += 1
if (success):
if (abs(DxCap) >= 0.8*CTreshold):
if (Harris < ((curveRatio+1)**2)/curveRatio):
ipolKPoints = np.insert(ipolKPoints, 0, candidatePoint)
# else:
# print "point discarded based on Harris"
# else:
# print "point discarded based on threshold"
# else:
# print "point discarded because unstable"
#delete the last record
for i in range(0,8):
ipolKPoints = np.delete(ipolKPoints, ipolKPoints.shape[0]-1)
#reshape the array into a matrix
ipolKPoints = np.reshape(ipolKPoints,(ipolKPoints.shape[0]/8,8))
def Grad_Orient():
#pre-compute gradient and orientation information
for a in range(numOctaves):
tempOrient = np.empty([oStack[a, 2].shape[0], oStack[a, 1].shape[1]])
tempGrad = np.empty([oStack[a, 2].shape[0], oStack[a, 1].shape[1]])
for i in range(1, oStack[a, 2].shape[0] - 1):
for j in range(1, oStack[a, 2].shape[1] - 1):
tempGrad[i, j] = np.sqrt((oStack[a, 2][i + 1, j] - oStack[a, 2][i - 1, j]) ** 2 + (oStack[a, 2][i, j + 1] - oStack[a, 2][i, j - 1]) ** 2)
try:
tempOrient[i, j] = np.arctan((oStack[a, 2][i, j + 1] - oStack[a, 2][i, j - 1]) / (oStack[a, 2][i + 1, j] - oStack[a, 2][i - 1, j]))
except:
print("Exception: division by zero")
mStack[a] = tempGrad
orStack[a] = tempOrient
def Orientation():
#determine the orientation of feature points
global ipolKPoints
global finKPoints
for a in range(0,ipolKPoints.shape[0]):
oBin = np.zeros([numBins]) #initialized
sigma = ipolKPoints[a,6]
oc = ipolKPoints[a,0]
iDist = iDistMin*(2**oc)
y = ipolKPoints[a,2]
x = ipolKPoints[a,3] #may have to change to x and y
W = np.abs(((x - (3*lbda*sigma))/iDist) - ((x + (3*lbda*sigma))/iDist))
H = np.abs(((y - (3*lbda*sigma))/iDist) - ((y + (3*lbda*sigma))/iDist))
if (0 < x-W and x+W < orStack[oc].shape[1] and 0 < y-H and y+H < orStack[oc].shape[0]):
mPatch = np.empty([H,W])
orPatch = np.empty([H,W])
for i in range(0,int(H)):
for j in range(0,int(W)):
#fill the patches
m = int(y-(H/2))+i; n = int(x-(H/2))+j
mPatch[i,j] = mStack[oc][m,n]
orPatch[i,j] = orStack[oc][m,n]
#gaussian blur the mPatch
gPatch = cv2.GaussianBlur(mPatch,(0,0),lbda*sigma)
for i in range(0,int(H)):
for j in range(0,int(W)):
binNum = np.round(np.mod((numBins/(2*np.pi))*(orPatch[i,j]),2*np.pi))
oBin[binNum] += gPatch[i,j]
#get the orientation of the point and add to the vector
maxOrient = np.max(oBin)
ipolKPoints[a,7] = maxOrient
finKPoints = np.insert(finKPoints, 0, ipolKPoints[a])
#else:
#print "Point too close to border"
for i in range(0,8):
finKPoints = np.delete(finKPoints, finKPoints.shape[0]-1)
finKPoints = np.reshape(finKPoints,(finKPoints.shape[0]/8,8))
def convertKeyPoints(keyPoints):
convKeyPoints = np.empty([1],dtype=object)
for i in range(keyPoints.shape[0]):
y = keyPoints[i,2]
x = keyPoints[i,3]
sigma = keyPoints[i,6]
size = int(2*lbdaDesc*sigma)
theta = keyPoints[i,7]
oc = int(keyPoints[i,0])
kp = cv2.KeyPoint(x,y,_size = size, _angle = theta*180/np.pi,_octave = oc)
convKeyPoints = np.insert(convKeyPoints,convKeyPoints.shape[0],kp,axis=0)
convKeyPoints = np.delete(convKeyPoints,0)
return convKeyPoints
def SiftPoints(imageURL,displayImage):
image, origImage = getImage(imageURL)
if not(image is None):
BuildOctaves(image)
DiffOfGauss()
LocalExtrema()
KeyPointsFilter()
Grad_Orient()
Orientation()
if (displayImage):
cPoints = convertKeyPoints(finKPoints)
outImage = cv2.drawKeypoints(origImage,cPoints,-1,flags = DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
#outImage = cv2.drawMatches(origImage,cPoints,None,None,None,-1,-1)
return finKPoints, outImage
return finKPoints, None
else:
return None, None
# def showMatches(image1URL, image2URL):
# image1, origImage1 = getImage(image1URL)
# image2, origImage2 = getImage(image2URL)
# if not(image1 is None or image2 is None):
# BuildOctaves(image1)
# DiffOfGauss()
# LocalExtrema()
# KeyPointsFilter()
# Grad_Orient()
# Orientation()
# k1 = finKPoints
#
# BuildOctaves(image1)
# DiffOfGauss()
# LocalExtrema()
# KeyPointsFilter()
# Grad_Orient()
# Orientation()
# k2 = finKPoints
#
# outImage = cv2.drawMatches(origImage,cPoints,-1,flags = DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
#
#
# #outImage = cv2.drawMatches(origImage,cPoints,None,None,None,-1,-1)
# return finKPoints, outImage
# return finKPoints, None
#
# else:
# return None, None