diff options
Diffstat (limited to 'SD-VBS/benchmarks/sift/src/c/sift.c')
-rw-r--r-- | SD-VBS/benchmarks/sift/src/c/sift.c | 314 |
1 files changed, 314 insertions, 0 deletions
diff --git a/SD-VBS/benchmarks/sift/src/c/sift.c b/SD-VBS/benchmarks/sift/src/c/sift.c new file mode 100644 index 0000000..ea0d308 --- /dev/null +++ b/SD-VBS/benchmarks/sift/src/c/sift.c | |||
@@ -0,0 +1,314 @@ | |||
1 | /******************************** | ||
2 | Author: Sravanthi Kota Venkata | ||
3 | ********************************/ | ||
4 | |||
5 | #include <math.h> | ||
6 | #include "sift.h" | ||
7 | |||
8 | /** SIFT- Scale Invariant Feature Transform. This algorithm is based on | ||
9 | David Lowe's implementation of sift. So, we will use the parameter | ||
10 | values from Lowe's implementation. | ||
11 | See: http://www.cs.ubc.ca/~lowe/keypoints/ | ||
12 | |||
13 | SIFT extracts from an image a collection of frames or keypoints. These | ||
14 | are oriented disks attacked to blob-like structures of the image. As | ||
15 | the image translates, rotates and scales, the frames track these blobs | ||
16 | and the deformation. | ||
17 | |||
18 | 'BoundaryPoint' [bool] | ||
19 | If set to 1, frames too close to the image boundaries are discarded. | ||
20 | |||
21 | 'Sigma0' [pixels] | ||
22 | Smoothing of the level 0 of octave 0 of the scale space. | ||
23 | (Note that Lowe's 1.6 value refers to the level -1 of octave 0.) | ||
24 | |||
25 | 'SigmaN' [pixels] | ||
26 | Nominal smoothing of the input image. Typically set to 0.5. | ||
27 | |||
28 | 'Threshold' | ||
29 | Threshold used to eliminate weak keypoints. Typical values for | ||
30 | intensity images in the range [0,1] are around 0.01. Smaller | ||
31 | values mean more keypoints. | ||
32 | |||
33 | **/ | ||
34 | |||
35 | F2D* sift(F2D* I) | ||
36 | { | ||
37 | int rows, cols, K; | ||
38 | int subLevels, omin, Octaves, r, NBP, NBO, smin, smax, intervals, o; | ||
39 | float sigma, sigman, sigma0, thresh, magnif; | ||
40 | int discardBoundaryPoints; | ||
41 | F2D *descriptors; | ||
42 | F2D **gss, *tfr; | ||
43 | F2D **dogss; | ||
44 | I2D* i_, *j_, *s_; | ||
45 | F2D *oframes, *frames; | ||
46 | int i, j, k, m, startRow, startCol, endRow, endCol, firstIn=1; | ||
47 | float minVal; | ||
48 | I2D* tx1, *ty1, *ts1; | ||
49 | I2D* x1, *y1, *s1, *txys; | ||
50 | |||
51 | rows = I->height; | ||
52 | cols = I->width; | ||
53 | |||
54 | /** | ||
55 | Lowe's choices | ||
56 | Octaves - octave | ||
57 | subLevels - sub-level for image | ||
58 | sigma - sigma value for gaussian kernel, for smoothing the image | ||
59 | At each successive octave, the data is spatially downsampled by half | ||
60 | **/ | ||
61 | |||
62 | subLevels = 3; | ||
63 | omin = -1; | ||
64 | minVal = log2f(MIN(rows,cols)); | ||
65 | Octaves = (int)(floor(minVal))-omin-4; /* Upto 16x16 images */ | ||
66 | sigma0 = 1.6 * pow(2, (1.0/subLevels)); | ||
67 | sigman = 0.5; | ||
68 | thresh = (0.04/subLevels)/2; | ||
69 | r = 10; | ||
70 | |||
71 | #ifdef test | ||
72 | subLevels = 1; | ||
73 | Octaves = 1; | ||
74 | sigma0 = pow(0.6 * 2, 1); | ||
75 | sigman = 1.0; | ||
76 | thresh = (1/subLevels)/2; | ||
77 | r = 1; | ||
78 | #endif | ||
79 | |||
80 | NBP = 4; | ||
81 | NBO = 8 ; | ||
82 | magnif = 3.0 ; | ||
83 | discardBoundaryPoints = 1 ; | ||
84 | |||
85 | smin = -1; | ||
86 | smax = subLevels+1; | ||
87 | intervals = smax - smin + 1; | ||
88 | |||
89 | /** | ||
90 | We build gaussian pyramid for the input image. Given image I, | ||
91 | we sub-sample the image into octave 'Octaves' number of levels. At | ||
92 | each level, we smooth the image with varying sigman values. | ||
93 | |||
94 | Gaussiam pyramid can be assumed as a 2-D matrix, where each | ||
95 | element is an image. Number of rows corresponds to the number | ||
96 | of scales of the pyramid (octaves, "Octaves"). Row 0 (scale 0) is the | ||
97 | size of the actual image, Row 1 (scale 1) is half the actual | ||
98 | size and so on. | ||
99 | |||
100 | At each scale, the image is smoothened with different sigma values. | ||
101 | So, each row has "intervals" number of smoothened images, starting | ||
102 | with least blurred. | ||
103 | |||
104 | gss holds the entire gaussian pyramid. | ||
105 | **/ | ||
106 | |||
107 | gss = gaussianss(I, sigman, Octaves, subLevels, omin, -1, subLevels+1, sigma0); | ||
108 | |||
109 | /** | ||
110 | Once we build the gaussian pyramid, we compute DOG, the | ||
111 | Difference of Gaussians. At every scale, we do: | ||
112 | |||
113 | dogss[fixedScale][0] = gss[fixedScale][1] - gss[fixedScale][0] | ||
114 | |||
115 | Difference of gaussian gives us edge information, at each scale. | ||
116 | In order to detect keypoints on the intervals per octave, we | ||
117 | inspect DOG images at highest and lowest scales of the octave, for | ||
118 | extrema detection. | ||
119 | **/ | ||
120 | |||
121 | dogss = diffss(gss, Octaves, intervals); | ||
122 | |||
123 | /** The extraction of keypoints is carried one octave per time **/ | ||
124 | for(o=0; o<Octaves; o++) | ||
125 | { | ||
126 | F2D *temp; | ||
127 | F2D *t; | ||
128 | F2D *negate; | ||
129 | F2D *idxf, *idxft; | ||
130 | I2D *idx; | ||
131 | int i1, j1; | ||
132 | I2D *s, *i, *j, *x, *y; | ||
133 | int sizeRows = dogss[o*intervals]->height; | ||
134 | int sizeCols = dogss[o*intervals]->width; | ||
135 | |||
136 | { | ||
137 | int i,j,k=0; | ||
138 | temp = fMallocHandle(intervals-1, sizeRows*sizeCols); | ||
139 | negate = fMallocHandle(intervals-1, sizeRows*sizeCols); | ||
140 | |||
141 | /** | ||
142 | Keypoints are detected as points of local extrema of the | ||
143 | DOG pyramid, at a given octave. In softlocalmax function, the | ||
144 | keypoints are extracted by looking at 9x9x9 neighborhood samples. | ||
145 | |||
146 | We populate temp and negate arrays with the values of the DOG | ||
147 | pyramid for a given octave, o. Since we are interested in both | ||
148 | local maxima and minima, we compute negate matrix, which is the | ||
149 | negated values of the DOG pyramid. | ||
150 | |||
151 | **/ | ||
152 | |||
153 | for(i1=0; i1<(intervals-1); i1++) | ||
154 | { | ||
155 | for(j=0; j<sizeCols; j++) | ||
156 | { | ||
157 | for(i=0; i<sizeRows; i++) | ||
158 | { | ||
159 | asubsref(temp,k) = subsref(dogss[o*intervals+i1],i,j); | ||
160 | asubsref(negate,k++) = -subsref(dogss[o*intervals+i1],i,j); | ||
161 | } | ||
162 | } | ||
163 | } | ||
164 | } | ||
165 | |||
166 | /** | ||
167 | siftlocalmax returns indices k, that correspond to local maxima and | ||
168 | minima. | ||
169 | The 80% tricks discards early very weak points before refinement. | ||
170 | **/ | ||
171 | idxf = siftlocalmax( temp, 0.8*thresh, intervals, sizeRows, sizeCols); | ||
172 | t = siftlocalmax( negate, 0.8*thresh, intervals, sizeRows, sizeCols); | ||
173 | |||
174 | idxft = fHorzcat(idxf, t); | ||
175 | |||
176 | /** | ||
177 | Since indices is the 1-D index of the temp/negate arrays, we compute | ||
178 | the x,y and intervals(s) co-ordinates corresponding to each index. | ||
179 | **/ | ||
180 | |||
181 | idx = ifDeepCopy(idxft); | ||
182 | |||
183 | x = iSetArray(idx->height,idx->width,0); | ||
184 | y = iSetArray(idx->height,idx->width,0); | ||
185 | s_ = iSetArray(idx->height,idx->width,0); | ||
186 | |||
187 | { | ||
188 | int i, j; | ||
189 | for(i=0; i<idx->height; i++) | ||
190 | { | ||
191 | for(j=0; j<idx->width; j++) | ||
192 | { | ||
193 | int v, u, w, z; | ||
194 | w = subsref(idx,i,j); | ||
195 | v = ceil((w/(sizeRows*sizeCols)) + 0.5); | ||
196 | u = floor(w/(sizeRows*sizeCols)); | ||
197 | z = w - (sizeRows*sizeCols*u); | ||
198 | |||
199 | /** v is the interval number, s **/ | ||
200 | subsref(s_,i,j) = v; | ||
201 | /** row number of the index **/ | ||
202 | subsref(y,i,j) = ceil((z / sizeRows)+0.5); | ||
203 | /** col number of the index **/ | ||
204 | subsref(x,i,j) = z - (sizeCols * floor(z / sizeRows)); | ||
205 | } | ||
206 | } | ||
207 | } | ||
208 | |||
209 | { | ||
210 | |||
211 | tx1 = isMinus(x, 1); | ||
212 | x1 = iReshape(tx1, 1, (tx1->height*tx1->width)); | ||
213 | |||
214 | ty1 = isMinus(y, 1); | ||
215 | y1 = iReshape(ty1, 1, (ty1->height*ty1->width)); | ||
216 | |||
217 | ts1 = isPlus(s_, (smin-1)); | ||
218 | s1 = iReshape(ts1, 1, (ts1->height*ts1->width)); | ||
219 | |||
220 | txys = iVertcat(y1, x1); | ||
221 | i_ = iVertcat(txys, s1); | ||
222 | } | ||
223 | |||
224 | /** | ||
225 | Stack all x,y,s into oframes. | ||
226 | Row 0 of oframes = x | ||
227 | Row 1 of oframes = y | ||
228 | Row 2 of oframes = s | ||
229 | **/ | ||
230 | oframes = fiDeepCopy(i_); | ||
231 | |||
232 | { | ||
233 | int i,j; | ||
234 | F2D* temp; | ||
235 | temp = oframes; | ||
236 | |||
237 | /** | ||
238 | Remove points too close to the boundary | ||
239 | **/ | ||
240 | |||
241 | if(discardBoundaryPoints) | ||
242 | oframes = filterBoundaryPoints(sizeRows, sizeCols, temp); | ||
243 | fFreeHandle(temp); | ||
244 | } | ||
245 | |||
246 | /** | ||
247 | Refine the location, threshold strength and remove points on edges | ||
248 | **/ | ||
249 | if( asubsref(oframes,0) != 0) | ||
250 | { | ||
251 | F2D* temp_; | ||
252 | temp_ = fTranspose(oframes); | ||
253 | fFreeHandle(oframes); | ||
254 | oframes = siftrefinemx(temp_, temp, smin, thresh, r, sizeRows, sizeCols, intervals-1); | ||
255 | fFreeHandle(temp_); | ||
256 | |||
257 | if( firstIn == 0) | ||
258 | { | ||
259 | tfr = fDeepCopy(frames); | ||
260 | fFreeHandle(frames); | ||
261 | frames = fHorzcat(tfr, oframes); | ||
262 | fFreeHandle(tfr); | ||
263 | } | ||
264 | else | ||
265 | frames = fDeepCopy(oframes); | ||
266 | firstIn = 0; | ||
267 | } | ||
268 | else if(Octaves == 1) | ||
269 | frames = fDeepCopy(oframes); | ||
270 | |||
271 | fFreeHandle(oframes); | ||
272 | iFreeHandle(y); | ||
273 | iFreeHandle(x); | ||
274 | iFreeHandle(s_); | ||
275 | iFreeHandle(y1); | ||
276 | iFreeHandle(x1); | ||
277 | iFreeHandle(s1); | ||
278 | iFreeHandle(ty1); | ||
279 | iFreeHandle(tx1); | ||
280 | iFreeHandle(ts1); | ||
281 | iFreeHandle(txys); | ||
282 | iFreeHandle(i_); | ||
283 | iFreeHandle(idx); | ||
284 | fFreeHandle(idxf); | ||
285 | fFreeHandle(idxft); | ||
286 | fFreeHandle(temp); | ||
287 | fFreeHandle(t); | ||
288 | fFreeHandle(negate); | ||
289 | } | ||
290 | |||
291 | { int s; | ||
292 | for(o=0; o<Octaves; o++) | ||
293 | { | ||
294 | for(s=0; s<(intervals-1); s++) | ||
295 | { | ||
296 | fFreeHandle(dogss[o*intervals+s]); | ||
297 | } | ||
298 | } | ||
299 | for(o=0; o<Octaves; o++) | ||
300 | { | ||
301 | for(s=0; s<(intervals); s++) | ||
302 | { | ||
303 | fFreeHandle(gss[o*intervals+s]); | ||
304 | } | ||
305 | } | ||
306 | } | ||
307 | free(gss); | ||
308 | free(dogss); | ||
309 | |||
310 | return frames; | ||
311 | } | ||
312 | |||
313 | |||
314 | |||