-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculateKeypoints.m
174 lines (137 loc) · 10.2 KB
/
calculateKeypoints.m
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
%function that calculates the keypoints and filters out the ones on edges
%and with low contrast
function calculateKeypoints=calculateKeypoints(dogDescriptors, originalImage)
contrastLimit = 0.03;
isKeypoint = false;
% keypointsMap = cell(size(dogDescriptors,1), size(dogDescriptors,1)-2);
keypointsMap = cell(size(dogDescriptors,1), size(dogDescriptors{1},4)-2);
cant = 0;
% size(dogDescriptors{1},3)
for octave = 1:size(dogDescriptors,1)
for layer = 2:(size(dogDescriptors{octave},4)-1)
% keypointsMap{octave}{layer-1} = zeros(size(dogDescriptors{octave},1), size(dogDescriptors{octave},2));
keypointsMap{octave,layer-1} = zeros(size(dogDescriptors{octave},1), size(dogDescriptors{octave},2));
%each of the points are to be compared - if it takes too long, do it in C++
for row = 2:size(dogDescriptors{octave},1)-1
for column = 2:size(dogDescriptors{octave},2)-1
%checks if it is maxima
isKeypoint = false;
if dogDescriptors{octave}(row,column,1,layer) > dogDescriptors{octave}(row-1,column,1,layer) && ...
dogDescriptors{octave}(row,column,1,layer) > dogDescriptors{octave}(row-1,column-1,1,layer) && ...
dogDescriptors{octave}(row,column,1,layer) > dogDescriptors{octave}(row,column-1,1,layer) && ...
dogDescriptors{octave}(row,column,1,layer) > dogDescriptors{octave}(row+1,column-1,1,layer) && ...
dogDescriptors{octave}(row,column,1,layer) > dogDescriptors{octave}(row+1,column,1,layer) && ...
dogDescriptors{octave}(row,column,1,layer) > dogDescriptors{octave}(row+1,column+1,1,layer) && ...
dogDescriptors{octave}(row,column,1,layer) > dogDescriptors{octave}(row,column+1,1,layer) && ...
dogDescriptors{octave}(row,column,1,layer) > dogDescriptors{octave}(row-1,column+1,1,layer) && ...
dogDescriptors{octave}(row,column,1,layer) > dogDescriptors{octave}(row-1,column,1,layer-1) && ...
dogDescriptors{octave}(row,column,1,layer) > dogDescriptors{octave}(row-1,column-1,1,layer-1) && ...
dogDescriptors{octave}(row,column,1,layer) > dogDescriptors{octave}(row,column-1,1,layer-1) && ...
dogDescriptors{octave}(row,column,1,layer) > dogDescriptors{octave}(row+1,column-1,1,layer-1) && ...
dogDescriptors{octave}(row,column,1,layer) > dogDescriptors{octave}(row+1,column,1,layer-1) && ...
dogDescriptors{octave}(row,column,1,layer) > dogDescriptors{octave}(row+1,column+1,1,layer-1) && ...
dogDescriptors{octave}(row,column,1,layer) > dogDescriptors{octave}(row,column+1,1,layer-1) && ...
dogDescriptors{octave}(row,column,1,layer) > dogDescriptors{octave}(row-1,column+1,1,layer-1) && ...
dogDescriptors{octave}(row,column,1,layer) > dogDescriptors{octave}(row,column,1,layer-1) && ...
dogDescriptors{octave}(row,column,1,layer) > dogDescriptors{octave}(row-1,column,1,layer+1) && ...
dogDescriptors{octave}(row,column,1,layer) > dogDescriptors{octave}(row-1,column-1,1,layer+1) && ...
dogDescriptors{octave}(row,column,1,layer) > dogDescriptors{octave}(row,column-1,1,layer+1) && ...
dogDescriptors{octave}(row,column,1,layer) > dogDescriptors{octave}(row+1,column-1,1,layer+1) && ...
dogDescriptors{octave}(row,column,1,layer) > dogDescriptors{octave}(row+1,column,1,layer+1) && ...
dogDescriptors{octave}(row,column,1,layer) > dogDescriptors{octave}(row+1,column+1,1,layer+1) && ...
dogDescriptors{octave}(row,column,1,layer) > dogDescriptors{octave}(row,column+1,1,layer+1) && ...
dogDescriptors{octave}(row,column,1,layer) > dogDescriptors{octave}(row-1,column+1,1,layer+1) && ...
dogDescriptors{octave}(row,column,1,layer) > dogDescriptors{octave}(row,column,1,layer+1)
if(keypointsMap{octave,layer-1}(row,column) == 0)
keypointsMap{octave,layer-1}(row,column) = 1;
cant = cant + 1;
isKeypoint = true;
end
end
%checks if it is minima
if dogDescriptors{octave}(row,column,1,layer) < dogDescriptors{octave}(row-1,column,1,layer) && ...
dogDescriptors{octave}(row,column,1,layer) < dogDescriptors{octave}(row-1,column-1,1,layer) && ...
dogDescriptors{octave}(row,column,1,layer) < dogDescriptors{octave}(row,column-1,1,layer) && ...
dogDescriptors{octave}(row,column,1,layer) < dogDescriptors{octave}(row+1,column-1,1,layer) && ...
dogDescriptors{octave}(row,column,1,layer) < dogDescriptors{octave}(row+1,column,1,layer) && ...
dogDescriptors{octave}(row,column,1,layer) < dogDescriptors{octave}(row+1,column+1,1,layer) && ...
dogDescriptors{octave}(row,column,1,layer) < dogDescriptors{octave}(row,column+1,1,layer) && ...
dogDescriptors{octave}(row,column,1,layer) < dogDescriptors{octave}(row-1,column+1,1,layer) && ...
dogDescriptors{octave}(row,column,1,layer) < dogDescriptors{octave}(row-1,column,1,layer-1) && ...
dogDescriptors{octave}(row,column,1,layer) < dogDescriptors{octave}(row-1,column-1,1,layer-1) && ...
dogDescriptors{octave}(row,column,1,layer) < dogDescriptors{octave}(row,column-1,1,layer-1) && ...
dogDescriptors{octave}(row,column,1,layer) < dogDescriptors{octave}(row+1,column-1,1,layer-1) && ...
dogDescriptors{octave}(row,column,1,layer) < dogDescriptors{octave}(row+1,column,1,layer-1) && ...
dogDescriptors{octave}(row,column,1,layer) < dogDescriptors{octave}(row+1,column+1,1,layer-1) && ...
dogDescriptors{octave}(row,column,1,layer) < dogDescriptors{octave}(row,column+1,1,layer-1) && ...
dogDescriptors{octave}(row,column,1,layer) < dogDescriptors{octave}(row-1,column+1,1,layer-1) && ...
dogDescriptors{octave}(row,column,1,layer) < dogDescriptors{octave}(row,column,1,layer-1) && ...
dogDescriptors{octave}(row,column,1,layer) < dogDescriptors{octave}(row-1,column,1,layer+1) && ...
dogDescriptors{octave}(row,column,1,layer) < dogDescriptors{octave}(row-1,column-1,1,layer+1) && ...
dogDescriptors{octave}(row,column,1,layer) < dogDescriptors{octave}(row,column-1,1,layer+1) && ...
dogDescriptors{octave}(row,column,1,layer) < dogDescriptors{octave}(row+1,column-1,1,layer+1) && ...
dogDescriptors{octave}(row,column,1,layer) < dogDescriptors{octave}(row+1,column,1,layer+1) && ...
dogDescriptors{octave}(row,column,1,layer) < dogDescriptors{octave}(row+1,column+1,1,layer+1) && ...
dogDescriptors{octave}(row,column,1,layer) < dogDescriptors{octave}(row,column+1,1,layer+1) && ...
dogDescriptors{octave}(row,column,1,layer) < dogDescriptors{octave}(row-1,column+1,1,layer+1) && ...
dogDescriptors{octave}(row,column,1,layer) < dogDescriptors{octave}(row,column,1,layer+1)
if(keypointsMap{octave,layer-1}(row,column) == 0)
keypointsMap{octave,layer-1}(row,column) = 1;
cant = cant + 1;
isKeypoint = true;
end
end
%checks the contrast - for now just the simplest way,
%without doing tailor expansion
if(isKeypoint==true)
if(abs(dogDescriptors{octave}(row,column,1,layer))<contrastLimit)
keypointsMap{octave,layer-1}(row,column) = 0;
isKeypoint = false;
cant = cant - 1;
end
end
%checks the points on the ridges
if(isKeypoint==true)
%used material to calculate derivatives: http://www.sci.utah.edu/~tolga/ece6532/Derivatives
%also wiki: http://en.wikipedia.org/wiki/Edge_detection
DerivativeYY = (dogDescriptors{octave}(row-1,column,1,layer) + ...
dogDescriptors{octave}(row+1, column, 1, layer) - ...
2.0*dogDescriptors{octave}(row, column, 1, layer));
DerivativeXX = (dogDescriptors{octave}(row,column-1,1,layer) + ...
dogDescriptors{octave}(row, column+1, 1, layer) - ...
2.0*dogDescriptors{octave}(row, column, 1, layer));
DerivativeXY = (dogDescriptors{octave}(row-1,column-1,1,layer) + ...
dogDescriptors{octave}(row+1,column+1,1,layer) - ...
dogDescriptors{octave}(row+1, column-1, 1, layer) - ...
dogDescriptors{octave}(row-1, column+1, 1, layer))/4;
trTerm = DerivativeXX + DerivativeYY;
DeterminantH = DerivativeXX * DerivativeYY - DerivativeXY*DerivativeXY;
if(DeterminantH<0)
% DeterminantH
end
ratio = (trTerm*trTerm)/DeterminantH;
%r=10 is the value proposed in section 4.1 of Lowe
%paper, however experimentally 5 seems to be better
%ratio
threshold = ((5+1)^2)/5;
if(ratio>=threshold || DeterminantH<0)
keypointsMap{octave,layer-1}(row,column) = 0;
isKeypoint = false;
cant = cant -1;
end
end
end
end
end
end
withPointsImage = originalImage;
returnData = cell(4,1);
returnData{1} = keypointsMap;
returnData{2} = withPointsImage;
returnData{3} = dogDescriptors;
%number of keypoints
returnData{4} = cant;
% qtyDep
cant
calculateKeypoints = returnData;
end