Debian Codegraph @

src/thirdparty/VLFeat/dsift.c

raw
1 /** @file dsift.c
2 ** @brief Dense SIFT - Definition
3 ** @author Andrea Vedaldi
4 ** @author Brian Fulkerson
5 **/
6
7 /*
8 Copyright (C) 2007-12 Andrea Vedaldi and Brian Fulkerson.
9 All rights reserved.
10
11 This file is part of the VLFeat library and is made available under
12 the terms of the BSD license (see the COPYING file).
13 */
14
15 #include "dsift.h"
16 #include "pgm.h"
17 #include "mathop.h"
18 #include "imopv.h"
19 #include <math.h>
20 #include <string.h>
21
22 /**
23 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
24 @page dsift Dense Scale Invariant Feature Transform (DSIFT)
25 @author Andrea Vedaldi
26 @author Brian Fulkerson
27 @tableofcontents
28 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
29
30 @ref dsift.h implements a dense version of @ref sift.h "SIFT". This is
31 an object that can quickly compute descriptors for densely sampled
32 keypoints with identical size and orientation. It can be reused for
33 multiple images of the same size.
34
35 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
36 @section dsift-intro Overview
37 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
38
39 @sa @ref sift "The SIFT module", @ref dsift-tech "Technical details"
40
41 This module implements a fast algorithm for the calculation of a large
42 number of SIFT descriptors of densely sampled features of the same
43 scale and orientation. See the @ref sift "SIFT module" for an
44 overview of SIFT.
45
46 The feature frames (keypoints) are indirectly specified by the
47 sampling steps (::vl_dsift_set_steps) and the sampling bounds
48 (::vl_dsift_set_bounds). The descriptor geometry (number and size of
49 the spatial bins and number of orientation bins) can be customized
50 (::vl_dsift_set_geometry, ::VlDsiftDescriptorGeometry).
51
52 @image html dsift-geom.png "Dense SIFT descriptor geometry"
53
54 By default, SIFT uses a Gaussian windowing function that discounts
55 contributions of gradients further away from the descriptor
56 centers. This function can be changed to a flat window by invoking
57 ::vl_dsift_set_flat_window. In this case, gradients are accumulated
58 using only bilinear interpolation, but instad of being reweighted by a
59 Gassuain window, they are all weighted equally. However, after
60 gradients have been accumulated into a spatial bin, the whole bin is
61 reweighted by the average of the Gaussian window over the spatial
62 support of that bin. This &ldquo;approximation&rdquo; substantially
63 improves speed with little or no loss of performance in applications.
64
65 Keypoints are sampled in such a way that the centers of the spatial
66 bins are at integer coordinates within the image boundaries. For
67 instance, the top-left bin of the top-left descriptor is centered on
68 the pixel (0,0). The bin immediately to the right at
69 (<code>binSizeX</code>,0), where <code>binSizeX</code> is a paramtere
70 in the ::VlDsiftDescriptorGeometry structure. ::vl_dsift_set_bounds
71 can be used to further restrict sampling to the keypoints in an image.
72
73 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
74 @section dsift-usage Usage
75 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
76
77 DSIFT is implemented by a ::VlDsiftFilter object that can be used
78 to process a sequence of images of a given geometry.
79 To use the <b>DSIFT filter</b>:
80
81 - Initialize a new DSIFT filter object by ::vl_dsift_new (or the simplified
82 ::vl_dsift_new_basic). Customize the descriptor parameters by
83 ::vl_dsift_set_steps, ::vl_dsift_set_geometry, etc.
84 - Process an image by ::vl_dsift_process.
85 - Retrieve the number of keypoints (::vl_dsift_get_keypoint_num), the
86 keypoints (::vl_dsift_get_keypoints), and their descriptors
87 (::vl_dsift_get_descriptors).
88 - Optionally repeat for more images.
89 - Delete the DSIFT filter by ::vl_dsift_delete.
90
91 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
92 @section dsift-tech Technical details
93 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
94
95 This section extends the @ref sift-tech-descriptor "SIFT descriptor section"
96 and specialzies it to the case of dense keypoints.
97
98 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
99 @subsection dsift-tech-descriptor-dense Dense descriptors
100 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
101
102 When computing descriptors for many keypoints differing only by their
103 position (and with null rotation), further simplifications are
104 possible. In this case, in fact,
105
106 @f{eqnarray*}
107 \mathbf{x} &=& m \sigma \hat{\mathbf{x}} + T,\\
108 h(t,i,j)
109 &=&
110 m \sigma \int
111 g_{\sigma_\mathrm{win}}(\mathbf{x} - T)\,
112 w_\mathrm{ang}(\angle J(\mathbf{x}) - \theta_t)\,
113 w\left(\frac{x - T_x}{m\sigma} - \hat{x}_i\right)\,
114 w\left(\frac{y - T_y}{m\sigma} - \hat{y}_j\right)\,
115 |J(\mathbf{x})|\,
116 d\mathbf{x}.
117 @f}
118
119 Since many different values of @e T are sampled, this is conveniently
120 expressed as a separable convolution. First, we translate by @f$
121 \mathbf{x}_{ij} = m\sigma(\hat x_i,\ \hat y_i)^\top @f$ and we use the
122 symmetry of the various binning and windowing functions to write
123
124 @f{eqnarray*}
125 h(t,i,j)
126 &=&
127 m \sigma \int
128 g_{\sigma_\mathrm{win}}(T' - \mathbf{x} - \mathbf{x}_{ij})\,
129 w_\mathrm{ang}(\angle J(\mathbf{x}) - \theta_t)\,
130 w\left(\frac{T'_x - x}{m\sigma}\right)\,
131 w\left(\frac{T'_y - y}{m\sigma}\right)\,
132 |J(\mathbf{x})|\,
133 d\mathbf{x},
134 \\
135 T' &=& T + m\sigma
136 \left[\begin{array}{cc} x_i \\ y_j \end{array}\right].
137 @f}
138
139 Then we define kernels
140
141 @f{eqnarray*}
142 k_i(x) &=&
143 \frac{1}{\sqrt{2\pi} \sigma_{\mathrm{win}}}
144 \exp\left(
145 -\frac{1}{2}
146 \frac{(x-x_i)^2}{\sigma_{\mathrm{win}}^2}
147 \right)
148 w\left(\frac{x}{m\sigma}\right),
149 \\
150 k_j(y) &=&
151 \frac{1}{\sqrt{2\pi} \sigma_{\mathrm{win}}}
152 \exp\left(
153 -\frac{1}{2}
154 \frac{(y-y_j)^2}{\sigma_{\mathrm{win}}^2}
155 \right)
156 w\left(\frac{y}{m\sigma}\right),
157 @f}
158
159 and obtain
160
161 @f{eqnarray*}
162 h(t,i,j) &=& (k_ik_j * \bar J_t)\left( T + m\sigma
163 \left[\begin{array}{cc} x_i \\ y_j \end{array}\right] \right),
164 \\
165 \bar J_t(\mathbf{x}) &=& w_\mathrm{ang}(\angle J(\mathbf{x}) - \theta_t)\,|J(\mathbf{x})|.
166 @f}
167
168 Furthermore, if we use a flat rather than Gaussian windowing function,
169 the kernels do not depend on the bin, and we have
170
171 @f{eqnarray*}
172 k(z) &=&
173 \frac{1}{\sigma_{\mathrm{win}}}
174 w\left(\frac{z}{m\sigma}\right),
175 \\
176 h(t,i,j) &=& (k(x)k(y) * \bar J_t)\left( T + m\sigma
177 \left[\begin{array}{cc} x_i \\ y_j \end{array}\right] \right),
178 @f}
179
180 (here @f$ \sigma_\mathrm{win} @f$ is the side of the flat window).
181
182 @note In this case the binning functions @f$ k(z) @f$ are triangular
183 and the convolution can be computed in time independent on the filter
184 (i.e. descriptor bin) support size by integral signals.
185
186 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
187 @subsection dsift-tech-sampling Sampling
188 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
189
190 To avoid resampling and dealing with special boundary conditions, we
191 impose some mild restrictions on the geometry of the descriptors that
192 can be computed. In particular, we impose that the bin centers @f$ T +
193 m\sigma (x_i,\ y_j) @f$ are always at integer coordinates within the
194 image boundaries. This eliminates the need for costly interpolation.
195 This condition amounts to (expressed in terms of the @e x coordinate,
196 and equally applicable to @e y)
197
198 @f[
199 \{0,\dots, W-1\} \ni T_x + m\sigma x_i =
200 T_x + m\sigma i - \frac{N_x-1}{2}
201 = \bar T_x + m\sigma i,
202 \qquad i = 0,\dots,N_x-1.
203 @f]
204
205 Notice that for this condition to be satisfied, the @em descriptor
206 center @f$ T_x @f$ needs to be either fractional or integer depending
207 on @f$ N_x @f$ being even or odd. To eliminate this complication,
208 it is simpler to use as a reference not the descriptor center @e T,
209 but the coordinates of the upper-left bin @f$ \bar T @f$. Thus we
210 sample the latter on a regular (integer) grid
211
212 @f[
213 \left[\begin{array}{cc}
214 0 \\
215 0
216 \end{array}\right]
217 \leq
218 \bar T =
219 \left[\begin{array}{cc}
220 \bar T_x^{\min} + p \Delta_x \\
221 \bar T_y^{\min} + q \Delta_y \\
222 \end{array}\right]
223 \leq
224 \left[\begin{array}{cc}
225 W - 1 - m\sigma N_x \\
226 H - 1 - m\sigma N_y
227 \end{array}\right],
228 \quad
229 \bar T =
230 \left[\begin{array}{cc}
231 T_x - \frac{N_x - 1}{2} \\
232 T_y - \frac{N_y - 1}{2} \\
233 \end{array}\right]
234 @f]
235
236 and we impose that the bin size @f$ m \sigma @f$ is integer as well.
237
238 **/
239
240 /** ------------------------------------------------------------------
241 ** @internal @brief Initialize new convolution kernel
242 ** @param binSize
243 ** @param numBins
244 ** @param binIndex negative to use flat window.
245 ** @param windowSize
246 ** @return a pointer to new filter.
247 **/
248
249 float *
250 _vl_dsift_new_kernel (int binSize, int numBins, int binIndex, double windowSize)
251 {
252 int filtLen = 2 * binSize - 1 ;
253 float * ker = vl_malloc (sizeof(float) * filtLen) ;
254 float * kerIter = ker ;
255 float delta = binSize * (binIndex - 0.5F * (numBins - 1)) ;
256 /*
257 float sigma = 0.5F * ((numBins - 1) * binSize + 1) ;
258 float sigma = 0.5F * ((numBins) * binSize) ;
259 */
260 float sigma = (float) binSize * (float) windowSize ;
261 int x ;
262
263 for (x = - binSize + 1 ; x <= + binSize - 1 ; ++ x) {
264 float z = (x - delta) / sigma ;
265 *kerIter++ = (1.0F - fabsf(x) / binSize) *
266 ((binIndex >= 0) ? expf(- 0.5F * z*z) : 1.0F) ;
267 }
268 return ker ;
269 }
270
271 static float
272 _vl_dsift_get_bin_window_mean
273 (int binSize, int numBins, int binIndex, double windowSize)
274 {
275 float delta = binSize * (binIndex - 0.5F * (numBins - 1)) ;
276 /*float sigma = 0.5F * ((numBins - 1) * binSize + 1) ;*/
277 float sigma = (float) binSize * (float) windowSize ;
278 int x ;
279
280 float acc = 0.0 ;
281 for (x = - binSize + 1 ; x <= + binSize - 1 ; ++ x) {
282 float z = (x - delta) / sigma ;
283 acc += ((binIndex >= 0) ? expf(- 0.5F * z*z) : 1.0F) ;
284 }
285 return acc /= (2 * binSize - 1) ;
286 }
287
288 /** ------------------------------------------------------------------
289 ** @internal @brief Normalize histogram
290 ** @param begin first element of the histogram.
291 ** @param end last plus one element of the histogram.
292 **
293 ** The function divides the specified histogram by its l2 norm.
294 **/
295
296 VL_INLINE float
297 _vl_dsift_normalize_histogram (float * begin, float * end)
298 {
299 float * iter ;
300 float norm = 0.0F ;
301
302 for (iter = begin ; iter < end ; ++ iter) {
303 norm += (*iter) * (*iter) ;
304 }
305 norm = vl_fast_sqrt_f (norm) + VL_EPSILON_F ;
306
307 for (iter = begin; iter < end ; ++ iter) {
308 *iter /= norm ;
309 }
310 return norm ;
311 }
312
313 /** ------------------------------------------------------------------
314 ** @internal @brief Free internal buffers
315 ** @param self DSIFT filter.
316 **/
317
318 static void
319 _vl_dsift_free_buffers (VlDsiftFilter* self)
320 {
321 if (self->frames) {
322 vl_free(self->frames) ;
323 self->frames = NULL ;
324 }
325 if (self->descrs) {
326 vl_free(self->descrs) ;
327 self->descrs = NULL ;
328 }
329 if (self->grads) {
330 int t ;
331 for (t = 0 ; t < self->numGradAlloc ; ++t)
332 if (self->grads[t]) vl_free(self->grads[t]) ;
333 vl_free(self->grads) ;
334 self->grads = NULL ;
335 }
336 self->numFrameAlloc = 0 ;
337 self->numBinAlloc = 0 ;
338 self->numGradAlloc = 0 ;
339 }
340
341 /** ------------------------------------------------------------------
342 ** @internal @brief Updates internal buffers to current geometry
343 **/
344
345 VL_EXPORT void
346 _vl_dsift_update_buffers (VlDsiftFilter * self)
347 {
348 int x1 = self->boundMinX ;
349 int x2 = self->boundMaxX ;
350 int y1 = self->boundMinY ;
351 int y2 = self->boundMaxY ;
352
353 int rangeX = x2 - x1 - (self->geom.numBinX - 1) * self->geom.binSizeX ;
354 int rangeY = y2 - y1 - (self->geom.numBinY - 1) * self->geom.binSizeY ;
355
356 int numFramesX = (rangeX >= 0) ? rangeX / self->stepX + 1 : 0 ;
357 int numFramesY = (rangeY >= 0) ? rangeY / self->stepY + 1 : 0 ;
358
359 self->numFrames = numFramesX * numFramesY ;
360 self->descrSize = self->geom.numBinT *
361 self->geom.numBinX *
362 self->geom.numBinY ;
363 }
364
365 /** ------------------------------------------------------------------
366 ** @internal @brief Allocate internal buffers
367 ** @param self DSIFT filter.
368 **
369 ** The function (re)allocates the internal buffers in accordance with
370 ** the current image and descriptor geometry.
371 **/
372
373 static void
374 _vl_dsift_alloc_buffers (VlDsiftFilter* self)
375 {
376 _vl_dsift_update_buffers (self) ;
377 {
378 int numFrameAlloc = vl_dsift_get_keypoint_num (self) ;
379 int numBinAlloc = vl_dsift_get_descriptor_size (self) ;
380 int numGradAlloc = self->geom.numBinT ;
381
382 /* see if we need to update the buffers */
383 if (numBinAlloc != self->numBinAlloc ||
384 numGradAlloc != self->numGradAlloc ||
385 numFrameAlloc != self->numFrameAlloc) {
386
387 int t ;
388
389 _vl_dsift_free_buffers(self) ;
390
391 self->frames = vl_malloc(sizeof(VlDsiftKeypoint) * numFrameAlloc) ;
392 self->descrs = vl_malloc(sizeof(float) * numBinAlloc * numFrameAlloc) ;
393 self->grads = vl_malloc(sizeof(float*) * numGradAlloc) ;
394 for (t = 0 ; t < numGradAlloc ; ++t) {
395 self->grads[t] =
396 vl_malloc(sizeof(float) * self->imWidth * self->imHeight) ;
397 }
398 self->numBinAlloc = numBinAlloc ;
399 self->numGradAlloc = numGradAlloc ;
400 self->numFrameAlloc = numFrameAlloc ;
401 }
402 }
403 }
404
405 /** ------------------------------------------------------------------
406 ** @brief Create a new DSIFT filter
407 **
408 ** @param imWidth width of the image.
409 ** @param imHeight height of the image
410 **
411 ** @return new filter.
412 **/
413
414 VL_EXPORT VlDsiftFilter *
415 vl_dsift_new (int imWidth, int imHeight)
416 {
417 VlDsiftFilter * self = vl_malloc (sizeof(VlDsiftFilter)) ;
418 self->imWidth = imWidth ;
419 self->imHeight = imHeight ;
420
421 self->stepX = 5 ;
422 self->stepY = 5 ;
423
424 self->boundMinX = 0 ;
425 self->boundMinY = 0 ;
426 self->boundMaxX = imWidth - 1 ;
427 self->boundMaxY = imHeight - 1 ;
428
429 self->geom.numBinX = 4 ;
430 self->geom.numBinY = 4 ;
431 self->geom.numBinT = 8 ;
432 self->geom.binSizeX = 5 ;
433 self->geom.binSizeY = 5 ;
434
435 self->useFlatWindow = VL_FALSE ;
436 self->windowSize = 2.0 ;
437
438 self->convTmp1 = vl_malloc(sizeof(float) * self->imWidth * self->imHeight) ;
439 self->convTmp2 = vl_malloc(sizeof(float) * self->imWidth * self->imHeight) ;
440
441 self->numBinAlloc = 0 ;
442 self->numFrameAlloc = 0 ;
443 self->numGradAlloc = 0 ;
444
445 self->descrSize = 0 ;
446 self->numFrames = 0 ;
447 self->grads = NULL ;
448 self->frames = NULL ;
449 self->descrs = NULL ;
450
451 _vl_dsift_update_buffers(self) ;
452 return self ;
453 }
454
455 /** ------------------------------------------------------------------
456 ** @brief Create a new DSIFT filter (basic interface)
457 ** @param imWidth width of the image.
458 ** @param imHeight height of the image.
459 ** @param step sampling step.
460 ** @param binSize bin size.
461 ** @return new filter.
462 **
463 ** The descriptor geometry matches the standard SIFT descriptor.
464 **/
465
466 VL_EXPORT VlDsiftFilter *
467 vl_dsift_new_basic (int imWidth, int imHeight, int step, int binSize)
468 {
469 VlDsiftFilter* self = vl_dsift_new(imWidth, imHeight) ;
470 VlDsiftDescriptorGeometry geom = *vl_dsift_get_geometry(self) ;
471 geom.binSizeX = binSize ;
472 geom.binSizeY = binSize ;
473 vl_dsift_set_geometry(self, &geom) ;
474 vl_dsift_set_steps(self, step, step) ;
475 return self ;
476 }
477
478 /** ------------------------------------------------------------------
479 ** @brief Delete DSIFT filter
480 ** @param self DSIFT filter.
481 **/
482
483 VL_EXPORT void
484 vl_dsift_delete (VlDsiftFilter * self)
485 {
486 _vl_dsift_free_buffers (self) ;
487 if (self->convTmp2) vl_free (self->convTmp2) ;
488 if (self->convTmp1) vl_free (self->convTmp1) ;
489 vl_free (self) ;
490 }
491
492
493 /** ------------------------------------------------------------------
494 ** @internal @brief Process with Gaussian window
495 ** @param self DSIFT filter.
496 **/
497
498 VL_INLINE void
499 _vl_dsift_with_gaussian_window (VlDsiftFilter * self)
500 {
501 int binx, biny, bint ;
502 int framex, framey ;
503 float *xker, *yker ;
504
505 int Wx = self->geom.binSizeX - 1 ;
506 int Wy = self->geom.binSizeY - 1 ;
507
508 for (biny = 0 ; biny < self->geom.numBinY ; ++biny) {
509
510 yker = _vl_dsift_new_kernel (self->geom.binSizeY,
511 self->geom.numBinY,
512 biny,
513 self->windowSize) ;
514
515 for (binx = 0 ; binx < self->geom.numBinX ; ++binx) {
516
517 xker = _vl_dsift_new_kernel(self->geom.binSizeX,
518 self->geom.numBinX,
519 binx,
520 self->windowSize) ;
521
522 for (bint = 0 ; bint < self->geom.numBinT ; ++bint) {
523
524 vl_imconvcol_vf (self->convTmp1, self->imHeight,
525 self->grads[bint], self->imWidth, self->imHeight,
526 self->imWidth,
527 yker, -Wy, +Wy, 1,
528 VL_PAD_BY_CONTINUITY|VL_TRANSPOSE) ;
529
530 vl_imconvcol_vf (self->convTmp2, self->imWidth,
531 self->convTmp1, self->imHeight, self->imWidth,
532 self->imHeight,
533 xker, -Wx, +Wx, 1,
534 VL_PAD_BY_CONTINUITY|VL_TRANSPOSE) ;
535
536 {
537 float *dst = self->descrs
538 + bint
539 + binx * self->geom.numBinT
540 + biny * (self->geom.numBinX * self->geom.numBinT) ;
541
542 float *src = self->convTmp2 ;
543
544 int frameSizeX = self->geom.binSizeX * (self->geom.numBinX - 1) + 1 ;
545 int frameSizeY = self->geom.binSizeY * (self->geom.numBinY - 1) + 1 ;
546 int descrSize = vl_dsift_get_descriptor_size (self) ;
547
548 for (framey = self->boundMinY ;
549 framey <= self->boundMaxY - frameSizeY + 1 ;
550 framey += self->stepY) {
551 for (framex = self->boundMinX ;
552 framex <= self->boundMaxX - frameSizeX + 1 ;
553 framex += self->stepX) {
554 *dst = src [(framex + binx * self->geom.binSizeX) * 1 +
555 (framey + biny * self->geom.binSizeY) * self->imWidth] ;
556 dst += descrSize ;
557 } /* framex */
558 } /* framey */
559 }
560
561 } /* for bint */
562 vl_free (xker) ;
563 } /* for binx */
564 vl_free (yker) ;
565 } /* for biny */
566 }
567
568 /** ------------------------------------------------------------------
569 ** @internal @brief Process with flat window.
570 ** @param self DSIFT filter object.
571 **/
572
573 VL_INLINE void
574 _vl_dsift_with_flat_window (VlDsiftFilter* self)
575 {
576 int binx, biny, bint ;
577 int framex, framey ;
578
579 /* for each orientation bin */
580 for (bint = 0 ; bint < self->geom.numBinT ; ++bint) {
581
582 vl_imconvcoltri_f (self->convTmp1, self->imHeight,
583 self->grads [bint], self->imWidth, self->imHeight,
584 self->imWidth,
585 self->geom.binSizeY, /* filt size */
586 1, /* subsampling step */
587 VL_PAD_BY_CONTINUITY|VL_TRANSPOSE) ;
588
589 vl_imconvcoltri_f (self->convTmp2, self->imWidth,
590 self->convTmp1, self->imHeight, self->imWidth,
591 self->imHeight,
592 self->geom.binSizeX,
593 1,
594 VL_PAD_BY_CONTINUITY|VL_TRANSPOSE) ;
595
596 for (biny = 0 ; biny < self->geom.numBinY ; ++biny) {
597
598 /*
599 This fast version of DSIFT does not use a proper Gaussian
600 weighting scheme for the gradiens that are accumulated on the
601 spatial bins. Instead each spatial bins is accumulated based on
602 the triangular kernel only, equivalent to bilinear interpolation
603 plus a flat, rather than Gaussian, window. Eventually, however,
604 the magnitude of the spatial bins in the SIFT descriptor is
605 reweighted by the average of the Gaussian window on each bin.
606 */
607
608 float wy = _vl_dsift_get_bin_window_mean
609 (self->geom.binSizeY, self->geom.numBinY, biny,
610 self->windowSize) ;
611
612 /* The convolution functions vl_imconvcoltri_* convolve by a
613 * triangular kernel with unit integral. Instead for SIFT the
614 * triangular kernel should have unit height. This is
615 * compensated for by multiplying by the bin size:
616 */
617
618 wy *= self->geom.binSizeY ;
619
620 for (binx = 0 ; binx < self->geom.numBinX ; ++binx) {
621 float w ;
622 float wx = _vl_dsift_get_bin_window_mean (self->geom.binSizeX,
623 self->geom.numBinX,
624 binx,
625 self->windowSize) ;
626
627 float *dst = self->descrs
628 + bint
629 + binx * self->geom.numBinT
630 + biny * (self->geom.numBinX * self->geom.numBinT) ;
631
632 float *src = self->convTmp2 ;
633
634 int frameSizeX = self->geom.binSizeX * (self->geom.numBinX - 1) + 1 ;
635 int frameSizeY = self->geom.binSizeY * (self->geom.numBinY - 1) + 1 ;
636 int descrSize = vl_dsift_get_descriptor_size (self) ;
637
638 wx *= self->geom.binSizeX ;
639 w = wx * wy ;
640
641 for (framey = self->boundMinY ;
642 framey <= self->boundMaxY - frameSizeY + 1 ;
643 framey += self->stepY) {
644 for (framex = self->boundMinX ;
645 framex <= self->boundMaxX - frameSizeX + 1 ;
646 framex += self->stepX) {
647 *dst = w * src [(framex + binx * self->geom.binSizeX) * 1 +
648 (framey + biny * self->geom.binSizeY) * self->imWidth] ;
649 dst += descrSize ;
650 } /* framex */
651 } /* framey */
652 } /* binx */
653 } /* biny */
654 } /* bint */
655 }
656
657 /** ------------------------------------------------------------------
658 ** @brief Compute keypoints and descriptors
659 **
660 ** @param self DSIFT filter.
661 ** @param im image data.
662 **/
663
664 void vl_dsift_process (VlDsiftFilter* self, float const* im)
665 {
666 int t, x, y ;
667
668 /* update buffers */
669 _vl_dsift_alloc_buffers (self) ;
670
671 /* clear integral images */
672 for (t = 0 ; t < self->geom.numBinT ; ++t)
673 memset (self->grads[t], 0,
674 sizeof(float) * self->imWidth * self->imHeight) ;
675
676 #undef at
677 #define at(x,y) (im[(y)*self->imWidth+(x)])
678
679 /* Compute gradients, their norm, and their angle */
680
681 for (y = 0 ; y < self->imHeight ; ++ y) {
682 for (x = 0 ; x < self->imWidth ; ++ x) {
683 float gx, gy ;
684 float angle, mod, nt, rbint ;
685 int bint ;
686
687 /* y derivative */
688 if (y == 0) {
689 gy = at(x,y+1) - at(x,y) ;
690 } else if (y == self->imHeight - 1) {
691 gy = at(x,y) - at(x,y-1) ;
692 } else {
693 gy = 0.5F * (at(x,y+1) - at(x,y-1)) ;
694 }
695
696 /* x derivative */
697 if (x == 0) {
698 gx = at(x+1,y) - at(x,y) ;
699 } else if (x == self->imWidth - 1) {
700 gx = at(x,y) - at(x-1,y) ;
701 } else {
702 gx = 0.5F * (at(x+1,y) - at(x-1,y)) ;
703 }
704
705 /* angle and modulus */
706 angle = vl_fast_atan2_f (gy,gx) ;
707 mod = vl_fast_sqrt_f (gx*gx + gy*gy) ;
708
709 /* quantize angle */
710 nt = vl_mod_2pi_f (angle) * (self->geom.numBinT / (2*VL_PI)) ;
711 bint = (int) vl_floor_f (nt) ;
712 rbint = nt - bint ;
713
714 /* write it back */
715 self->grads [(bint ) % self->geom.numBinT][x + y * self->imWidth] = (1 - rbint) * mod ;
716 self->grads [(bint + 1) % self->geom.numBinT][x + y * self->imWidth] = ( rbint) * mod ;
717 }
718 }
719
720 if (self->useFlatWindow) {
721 _vl_dsift_with_flat_window(self) ;
722 } else {
723 _vl_dsift_with_gaussian_window(self) ;
724 }
725
726 {
727 VlDsiftKeypoint* frameIter = self->frames ;
728 float * descrIter = self->descrs ;
729 int framex, framey, bint ;
730
731 int frameSizeX = self->geom.binSizeX * (self->geom.numBinX - 1) + 1 ;
732 int frameSizeY = self->geom.binSizeY * (self->geom.numBinY - 1) + 1 ;
733 int descrSize = vl_dsift_get_descriptor_size (self) ;
734
735 float deltaCenterX = 0.5F * self->geom.binSizeX * (self->geom.numBinX - 1) ;
736 float deltaCenterY = 0.5F * self->geom.binSizeY * (self->geom.numBinY - 1) ;
737
738 float normConstant = frameSizeX * frameSizeY ;
739
740 for (framey = self->boundMinY ;
741 framey <= self->boundMaxY - frameSizeY + 1 ;
742 framey += self->stepY) {
743
744 for (framex = self->boundMinX ;
745 framex <= self->boundMaxX - frameSizeX + 1 ;
746 framex += self->stepX) {
747
748 frameIter->x = framex + deltaCenterX ;
749 frameIter->y = framey + deltaCenterY ;
750
751 /* mass */
752 {
753 float mass = 0 ;
754 for (bint = 0 ; bint < descrSize ; ++ bint)
755 mass += descrIter[bint] ;
756 mass /= normConstant ;
757 frameIter->norm = mass ;
758 }
759
760 /* L2 normalize */
761 _vl_dsift_normalize_histogram (descrIter, descrIter + descrSize) ;
762
763 /* clamp */
764 for(bint = 0 ; bint < descrSize ; ++ bint)
765 if (descrIter[bint] > 0.2F) descrIter[bint] = 0.2F ;
766
767 /* L2 normalize */
768 _vl_dsift_normalize_histogram (descrIter, descrIter + descrSize) ;
769
770 frameIter ++ ;
771 descrIter += descrSize ;
772 } /* for framex */
773 } /* for framey */
774 }
775 }