Debian Codegraph @

src/thirdparty/VLFeat/imopv.c

raw
1 /** @file imopv.c
2 ** @brief Vectorized image operations - Definition
3 ** @author Andrea Vedaldi
4 **/
5
6 /*
7 Copyright (C) 2007-12 Andrea Vedaldi and Brian Fulkerson.
8 All rights reserved.
9
10 This file is part of the VLFeat library and is made available under
11 the terms of the BSD license (see the COPYING file).
12 */
13
14 /** @file imopv.h
15 **
16 ** This module provides the following image operations:
17 **
18 ** - <b>Separable convolution.</b> The function ::vl_imconvcol_vf()
19 ** can be used to compute separable convolutions.
20 **
21 ** - <b>Convolution by a triangular kernel.</b> The function
22 ** vl_imconvcoltri_vf() is an optimized convolution routine for
23 ** triangular kernels.
24 **
25 ** - <b>Distance transform.</b> ::vl_image_distance_transform_f() is
26 ** a linear algorithm to compute the distance transform of an
27 ** image.
28 **
29 ** @remark Some operations are optimized to exploit possible SIMD
30 ** instructions. This requires image data to be properly aligned (typically
31 ** to 16 bytes). Similalry, the image stride (the number of bytes to skip to move
32 ** to the next image row), must be aligned.
33 **/
34
35 #ifndef VL_IMOPV_INSTANTIATING
36
37 #include "imopv.h"
38 #include "imopv_sse2.h"
39 #include "mathop.h"
40
41 #define FLT VL_TYPE_FLOAT
42 #define VL_IMOPV_INSTANTIATING
43 #include "imopv.c"
44
45 #define FLT VL_TYPE_DOUBLE
46 #define VL_IMOPV_INSTANTIATING
47 #include "imopv.c"
48
49 #define FLT VL_TYPE_UINT32
50 #define VL_IMOPV_INSTANTIATING
51 #include "imopv.c"
52
53 #define FLT VL_TYPE_INT32
54 #define VL_IMOPV_INSTANTIATING
55 #include "imopv.c"
56
57 /* VL_IMOPV_INSTANTIATING */
58 #endif
59
60 #if defined(VL_IMOPV_INSTANTIATING) || defined(__DOXYGEN__)
61
62 #include "float.h"
63
64 /* ---------------------------------------------------------------- */
65 /* Image Convolution */
66 /* ---------------------------------------------------------------- */
67
68 #if (FLT == VL_TYPE_FLOAT || FLT == VL_TYPE_DOUBLE)
69
70 /** @fn vl_imconvcol_vd(double*,vl_size,double const*,vl_size,vl_size,vl_size,double const*,vl_index,vl_index,int,unsigned int)
71 ** @brief Convolve image along columns
72 **
73 ** @param dst destination image.
74 ** @param dst_stride width of the destination image including padding.
75 ** @param src source image.
76 ** @param src_width width of the source image.
77 ** @param src_height height of the source image.
78 ** @param src_stride width of the source image including padding.
79 ** @param filt filter kernel.
80 ** @param filt_begin coordinate of the first filter element.
81 ** @param filt_end coordinate of the last filter element.
82 ** @param step sub-sampling step.
83 ** @param flags operation modes.
84 **
85 ** The function convolves the column of the image @a src by the
86 ** filter @a filt and saves the result to the image @a dst. The size
87 ** of @a dst must be equal to the size of @a src. Formally, this
88 ** results in the calculation
89 **
90 ** @f[
91 ** \mathrm{dst} [x,y] = \sum_{p=y-\mathrm{filt\_end}}^{y-\mathrm{filt\_begin}}
92 ** \mathrm{src}[x,y] \mathrm{filt}[y - p - \mathrm{filt\_begin}]
93 ** @f]
94 **
95 ** The function subsamples the image along the columns according to
96 ** the parameter @a step. Setting @a step to 1 (one) computes the
97 ** elements @f$\mathrm{dst}[x,y]@f$ for all pairs (x,0), (x,1), (x,2)
98 ** and so on. Setting @a step two 2 (two) computes only (x,0), (x,2)
99 ** and so on (in this case the height of the destination image is
100 ** <code>floor(src_height/step)+1)</code>.
101 **
102 ** Calling twice the function can be used to compute 2-D separable
103 ** convolutions. Use the flag ::VL_TRANSPOSE to transpose the result
104 ** (in this case @a dst has transposed dimension as well).
105 **
106 ** The function allows the support of the filter to be any range.
107 ** Usually the support is <code>@a filt_end = -@a filt_begin</code>.
108 **
109 ** The convolution operation may pick up values outside the image
110 ** boundary. To cope with this edge cases, the function either pads
111 ** the image by zero (::VL_PAD_BY_ZERO) or with the values at the
112 ** boundary (::VL_PAD_BY_CONTINUITY).
113 **/
114
115 /** @fn vl_imconvcol_vf(float*,vl_size,float const*,vl_size,vl_size,vl_size,float const*,vl_index,vl_index,int,unsigned int)
116 ** @see ::vl_imconvcol_vd
117 **/
118
119 VL_EXPORT void
120 VL_XCAT(vl_imconvcol_v, SFX)
121 (T* dst, vl_size dst_stride,
122 T const* src,
123 vl_size src_width, vl_size src_height, vl_size src_stride,
124 T const* filt, vl_index filt_begin, vl_index filt_end,
125 int step, unsigned int flags)
126 {
127 vl_index x = 0 ;
128 vl_index y ;
129 vl_index dheight = (src_height - 1) / step + 1 ;
130 vl_bool transp = flags & VL_TRANSPOSE ;
131 vl_bool zeropad = (flags & VL_PAD_MASK) == VL_PAD_BY_ZERO ;
132
133 /* dispatch to accelerated version */
134 #ifndef VL_DISABLE_SSE2
135 if (vl_cpu_has_sse2() && vl_get_simd_enabled()) {
136 VL_XCAT3(_vl_imconvcol_v,SFX,_sse2)
137 (dst,dst_stride,
138 src,src_width,src_height,src_stride,
139 filt,filt_begin,filt_end,
140 step,flags) ;
141 return ;
142 }
143 #endif
144
145 /* let filt point to the last sample of the filter */
146 filt += filt_end - filt_begin ;
147
148 while (x < (signed)src_width) {
149 /* Calculate dest[x,y] = sum_p image[x,p] filt[y - p]
150 * where supp(filt) = [filt_begin, filt_end] = [fb,fe].
151 *
152 * CHUNK_A: y - fe <= p < 0
153 * completes VL_MAX(fe - y, 0) samples
154 * CHUNK_B: VL_MAX(y - fe, 0) <= p < VL_MIN(y - fb, height - 1)
155 * completes fe - VL_MAX(fb, height - y) + 1 samples
156 * CHUNK_C: completes all samples
157 */
158 T const *filti ;
159 vl_index stop ;
160
161 for (y = 0 ; y < (signed)src_height ; y += step) {
162 T acc = 0 ;
163 T v = 0, c ;
164 T const* srci ;
165
166 filti = filt ;
167 stop = filt_end - y ;
168 srci = src + x - stop * src_stride ;
169
170 if (stop > 0) {
171 if (zeropad) {
172 v = 0 ;
173 } else {
174 v = *(src + x) ;
175 }
176 while (filti > filt - stop) {
177 c = *filti-- ;
178 acc += v * c ;
179 srci += src_stride ;
180 }
181 }
182
183 stop = filt_end - VL_MAX(filt_begin, y - (signed)src_height + 1) + 1 ;
184 while (filti > filt - stop) {
185 v = *srci ;
186 c = *filti-- ;
187 acc += v * c ;
188 srci += src_stride ;
189 }
190
191 if (zeropad) v = 0 ;
192
193 stop = filt_end - filt_begin + 1 ;
194 while (filti > filt - stop) {
195 c = *filti-- ;
196 acc += v * c ;
197 }
198
199 if (transp) {
200 *dst = acc ; dst += 1 ;
201 } else {
202 *dst = acc ; dst += dst_stride ;
203 }
204 } /* next y */
205 if (transp) {
206 dst += 1 * dst_stride - dheight * 1 ;
207 } else {
208 dst += 1 * 1 - dheight * dst_stride ;
209 }
210 x += 1 ;
211 } /* next x */
212 }
213
214 /* VL_TYPE_FLOAT, VL_TYPE_DOUBLE */
215 #endif
216
217 /* ---------------------------------------------------------------- */
218 /* Image distance transform */
219 /* ---------------------------------------------------------------- */
220
221 #if (FLT == VL_TYPE_FLOAT || FLT == VL_TYPE_DOUBLE)
222
223 /** @fn ::vl_image_distance_transform_d(double const*,vl_size,vl_size,vl_size,vl_size,double*,vl_uindex*,double,double)
224 ** @brief Compute the distance transform of an image
225 ** @param image image.
226 ** @param numColumns number of columns of the image.
227 ** @param numRows number of rows of the image.
228 ** @param columnStride offset from one column to the next.
229 ** @param rowStride offset from one row to the next.
230 ** @param distanceTransform distance transform (out).
231 ** @param indexes nearest neighbor indexes (in/out).
232 ** @param coeff quadratic cost coefficient (non-negative).
233 ** @param offset quadratic cost offset.
234 **
235 ** The function computes the distance transform along the first
236 ** dimension of the image @a image. Let @f$ I(u,v) @f$ be @a image.
237 ** Its distance transfrom @f$ D(u,v) @f$ is given by:
238 **
239 ** @f[
240 ** u^*(u,v) = \min_{u'} I(u',v) + \mathtt{coeff} (u' - u - \mathtt{offset})^2,
241 ** \quad D(u,v) = I(u^*(u,v),v).
242 ** @f]
243 **
244 ** Notice that @a coeff must be non negative.
245 **
246 ** The function fills in the buffer @a distanceTransform with @f$ D
247 ** @f$. This buffer must have the same size as @a image.
248 **
249 ** If @a indexes is not @c NULL, it must be a matrix of the same size
250 ** o the image. The function interprets the value of this matrix as
251 ** indexes of the pixels, i.e @f$ \mathtt{indexes}(u,v) @f$ is the
252 ** index of pixel @f$ (u,v) @f$. On output, the matrix @a indexes
253 ** contains @f$ \mathtt{indexes}(u^*(u,v),v) @f$. This information
254 ** can be used to determine for each pixel @f$ (u,v) @f$ its
255 ** &ldquo;nearest neighbor&rdquo.
256 **
257 ** Notice that by swapping @a numRows and @a numColumns and @a
258 ** columnStride and @a rowStride, the function can be made to operate
259 ** along the other image dimension. Specifically, to compute the
260 ** distance transform along columns and rows, call the functinon
261 ** twice:
262 ***
263 ** @code
264 ** for (i = 0 ; i < numColumns * numRows ; ++i) indexes[i] = i ;
265 ** vl_image_distance_transform_d(image,numColumns,numRows,1,numColumns,
266 ** distanceTransform,indexes,u_coeff,u_offset) ;
267 ** vl_image_distance_transform_d(distanceTransform,numRows,numColumns,numColumns,1,
268 ** distanceTransform,indexes,u_coeff,u_offset) ;
269 ** @endcode
270 **
271 ** @par Algorithm
272 **
273 ** The function implements the algorithm described in:
274 ** P. F. Felzenszwalb and D. P. Huttenlocher, <em>Distance Transforms
275 ** of Sampled Functions,</em> Technical Report, Cornell University,
276 ** 2004.
277 **
278 ** Since the algorithm operates along one dimension per time,
279 ** consider the 1D version of the problem for simplicity:
280 **
281 ** @f[
282 ** d(y) = \min_{x} g(y;x), \quad g(y;x) = f(x) + \alpha (y - x - \beta)^2,
283 ** \quad x,y \in \{0,1,\dots,N-1\}.
284 ** @f]
285 **
286 ** Hence the distance transform @f$ d(y) @f$ is the lower envelope of
287 ** the family of parabolas @f$ g(y;x) @f$ indexed by @f$ x
288 ** @f$. Notice that all parabolas have the same curvature and that
289 ** their centers are located at @f$ x + \beta, @f$ @f$ x=0,\dots,N-1
290 ** @f$. The algorithm considers one parabola per time, from left to
291 ** right, and finds the interval for which the parabola belongs to
292 ** the lower envelope (if any).
293 **
294 ** Initially, only the leftmost parabola @f$ g(y;0) @f$ has been
295 ** considered, and its validity interval is @f$(-\infty, \infty) @f$.
296 ** Then the second parabola @f$ g(y;1) @f$ is considered. As long as
297 ** @f$ \alpha > 0 @f$, the two parabolas @f$ g(y;0),\ g(y;1) @f$
298 ** intersect at a unique point @f$ \bar y @f$. Then the first
299 ** parabola belongs to the envelope in the interval @f$ (-\infty,
300 ** \bar y] @f$ and the second one in the interval @f$ (\bar y,
301 ** +\infty] @f$. When the third parabola @f$ g(y;2) @f$ is
302 ** considered, the intersection point @f$ \hat y @f$ with the
303 ** previously added parabola @f$ g(y;1) @f$ is found. Now two cases
304 ** may arise:
305 **
306 ** - @f$ \hat y > \bar y @f$, in which case all three parabolas
307 ** belong to the envelope in the intervals @f$ (-\infty,\bar y],
308 ** (\bar y, \hat y], (\hat y, +\infty] @f$.
309 **
310 ** - @f$ \hat y \leq \bar y @f$, in which case the second parabola
311 ** @f$ g(y;1) @f$ has no point beloning to the envelope, and it is
312 ** removed. One then remains with the two parabolas @f$ g(y;0),\
313 ** g(y;2) @f$ and the algorithm is re-iterated.
314 **
315 ** The algorithm proceeds in this fashion. Every time a new parabola
316 ** is considered, its intersection point with the previously added
317 ** parabola on the left is computed, and that parabola is potentially
318 ** removed. The cost of an iteration is 1 plus the number of deleted
319 ** parabolas. Since there are @f$ N @f$ iterations and at most @f$ N
320 ** @f$ parabolas to delete overall, the complexity is linear,
321 ** i.e. @f$ O(N) @f$.
322 **/
323
324 /** @fn ::vl_image_distance_transform_f(float const*,vl_size,vl_size,vl_size,vl_size,float*,vl_uindex*,float,float)
325 ** @see ::vl_image_distance_transform_d
326 **/
327
328 VL_EXPORT void
329 VL_XCAT(vl_image_distance_transform_,SFX)
330 (T const * image,
331 vl_size numColumns,
332 vl_size numRows,
333 vl_size columnStride,
334 vl_size rowStride,
335 T * distanceTransform,
336 vl_uindex * indexes,
337 T coeff,
338 T offset)
339 {
340 /* Each image pixel corresponds to a parabola. The algorithm scans
341 such parabolas from left to right, keeping track of which
342 parabolas belong to the lower envelope and in which interval. There are
343 NUM active parabolas, FROM stores the beginning of the interval
344 for which a certain parabola is part of the envoelope, and WHICH store
345 the index of the parabola (that is, the pixel x from which the parabola
346 originated).
347 */
348 vl_uindex x, y ;
349 T * from = vl_malloc (sizeof(T) * (numColumns + 1)) ;
350 T * base = vl_malloc (sizeof(T) * numColumns) ;
351 vl_uindex * baseIndexes = vl_malloc (sizeof(vl_uindex) * numColumns) ;
352 vl_uindex * which = vl_malloc (sizeof(vl_uindex) * numColumns) ;
353 vl_uindex num = 0 ;
354
355 for (y = 0 ; y < numRows ; ++y) {
356 num = 0 ;
357 for (x = 0 ; x < numColumns ; ++x) {
358 T r = image[x * columnStride + y * rowStride] ;
359 T x2 = x * x ;
360 #if (FLT == VL_TYPE_FLOAT)
361 T from_ = - VL_INFINITY_F ;
362 #else
363 T from_ = - VL_INFINITY_D ;
364 #endif
365
366 /*
367 Add next parabola (there are NUM so far). The algorithm finds
368 intersection INTERS with the previously added parabola. If
369 the intersection is on the right of the "starting point" of
370 this parabola, then the previous parabola is kept, and the
371 new one is added to its right. Otherwise the new parabola
372 "eats" the old one, which gets deleted and the check is
373 repeated with the parabola added before the deleted one.
374 */
375
376 while (num >= 1) {
377 vl_uindex x_ = which[num - 1] ;
378 T x2_ = x_ * x_ ;
379 T r_ = image[x_ * columnStride + y * rowStride] ;
380 T inters ;
381 if (r == r_) {
382 /* handles the case r = r_ = \pm inf */
383 inters = (x + x_) / 2.0 + offset ;
384 }
385 #if (FLT == VL_TYPE_FLOAT)
386 else if (coeff > VL_EPSILON_F)
387 #else
388 else if (coeff > VL_EPSILON_D)
389 #endif
390 {
391 inters = ((r - r_) + coeff * (x2 - x2_)) / (x - x_) / (2*coeff) + offset ;
392 } else {
393 /* If coeff is very small, the parabolas are flat (= lines).
394 In this case the previous parabola should be deleted if the current
395 pixel has lower score */
396 #if (FLT == VL_TYPE_FLOAT)
397 inters = (r < r_) ? - VL_INFINITY_F : VL_INFINITY_F ;
398 #else
399 inters = (r < r_) ? - VL_INFINITY_D : VL_INFINITY_D ;
400 #endif
401 }
402 if (inters <= from [num - 1]) {
403 /* delete a previous parabola */
404 -- num ;
405 } else {
406 /* accept intersection */
407 from_ = inters ;
408 break ;
409 }
410 }
411
412 /* add a new parabola */
413 which[num] = x ;
414 from[num] = from_ ;
415 base[num] = r ;
416 if (indexes) baseIndexes[num] = indexes[x * columnStride + y * rowStride] ;
417 num ++ ;
418 } /* next column */
419
420 #if (FLT == VL_TYPE_FLOAT)
421 from[num] = VL_INFINITY_F ;
422 #else
423 from[num] = VL_INFINITY_D ;
424 #endif
425
426 /* fill in */
427 num = 0 ;
428 for (x = 0 ; x < numColumns ; ++x) {
429 double delta ;
430 while (x >= from[num + 1]) ++ num ;
431 delta = (double) x - (double) which[num] - offset ;
432 distanceTransform[x * columnStride + y * rowStride]
433 = base[num] + coeff * delta * delta ;
434 if (indexes) {
435 indexes[x * columnStride + y * rowStride]
436 = baseIndexes[num] ;
437 }
438 }
439 } /* next row */
440
441 vl_free (from) ;
442 vl_free (which) ;
443 vl_free (base) ;
444 vl_free (baseIndexes) ;
445 }
446
447 /* VL_TYPE_FLOAT, VL_TYPE_DOUBLE */
448 #endif
449
450 /* ---------------------------------------------------------------- */
451 /* Image convolution by a triangular kernel */
452 /* ---------------------------------------------------------------- */
453
454 #if (FLT == VL_TYPE_FLOAT || FLT == VL_TYPE_DOUBLE)
455
456 /** @fn vl_imconvcoltri_d(double*,vl_size,double const*,vl_size,vl_size,vl_size,vl_size,vl_size,int unsigned)
457 ** @brief Convolve an image along the columns with a triangular kernel
458 ** @param dest destination image.
459 ** @param destStride destination image stride.
460 ** @param image image to convolve.
461 ** @param imageWidth width of the image.
462 ** @param imageHeight height of the image.
463 ** @param imageStride width of the image including padding.
464 ** @param filterSize size of the triangular filter.
465 ** @param step sub-sampling step.
466 ** @param flags operation modes.
467 **
468 ** The function convolves the columns of the image @a image with the
469 ** triangular kernel
470 **
471 ** @f[
472 ** k(t) = \frac{1}{\Delta^2} \max\{ \Delta - |t|, 0 \},
473 ** \quad t \in \mathbb{Z}
474 ** @f]
475 **
476 ** The paramter @f$ \Delta @f$, equal to the function argument @a
477 ** filterSize, controls the width of the kernel. Notice that the
478 ** support of @f$ k(x) @f$ as a continuous function of @f$ x @f$ is
479 ** the open interval @f$ (-\Delta,\Delta) @f$, which has length @f$
480 ** 2\Delta @f$. However, @f$ k(x) @f$ restricted to the ingeter
481 ** domain @f$ x \in \mathcal{Z} @f$ has support @f$ \{ -\Delta + 1,
482 ** \Delta +2, \dots, \Delta-1 \} @f$, which counts @f$ 2 \Delta - 1
483 ** @f$ elements only. In particular, the discrete kernel is symmetric
484 ** about the origin for all values of @f$ \Delta @f$.
485 **
486 ** The normalization factor @f$ 1 / \Delta^2 @f$ guaratnees that the
487 ** filter is normalized to one, i.e.:
488 **
489 ** @f[
490 ** \sum_{t=-\infty}^{+\infty} k(t) = 1
491 ** @f]
492 **
493 ** @par Algorithm
494 **
495 ** The function exploits the fact that convolution by a triangular
496 ** kernel can be expressed as the repeated convolution by a
497 ** rectangular kernel, and that the latter can be performed in time
498 ** indepenedent on the fiter width by using an integral-image type
499 ** trick. Overall, the algorithm complexity is independent on the
500 ** parameter @a filterSize and linear in the nubmer of image pixels.
501 **
502 ** @see ::vl_imconvcol_vd for details on the meaning of the other parameters.
503 **/
504
505 /** @fn vl_imconvcoltri_f(float*,vl_size,float const*,vl_size,vl_size,vl_size,vl_size,vl_size,int unsigned)
506 ** @brief Convolve an image along the columns with a triangular kernel
507 ** @see ::vl_imconvcoltri_d()
508 **/
509
510 VL_EXPORT void
511 VL_XCAT(vl_imconvcoltri_, SFX)
512 (T * dest, vl_size destStride,
513 T const * image,
514 vl_size imageWidth, vl_size imageHeight, vl_size imageStride,
515 vl_size filterSize,
516 vl_size step, unsigned int flags)
517 {
518 vl_index x, y, dheight ;
519 vl_bool transp = flags & VL_TRANSPOSE ;
520 vl_bool zeropad = (flags & VL_PAD_MASK) == VL_PAD_BY_ZERO ;
521 T scale = (T) (1.0 / ((double)filterSize * (double)filterSize)) ;
522 T * buffer = vl_malloc (sizeof(T) * (imageHeight + filterSize)) ;
523 buffer += filterSize ;
524
525 if (imageHeight == 0) {
526 return ;
527 }
528
529 x = 0 ;
530 dheight = (imageHeight - 1) / step + 1 ;
531
532 while (x < (signed)imageWidth) {
533 T const * imagei ;
534 imagei = image + x + imageStride * (imageHeight - 1) ;
535
536 /* We decompose the convolution by a triangluar signal as the convolution
537 * by two rectangular signals. The rectangular convolutions are computed
538 * quickly by computing the integral signals. Each rectangular convolution
539 * introduces a delay, which is compensated by convolving each in opposite
540 * directions.
541 */
542
543 /* integrate backward the column */
544 buffer[imageHeight - 1] = *imagei ;
545 for (y = (signed)imageHeight - 2 ; y >= 0 ; --y) {
546 imagei -= imageStride ;
547 buffer[y] = buffer[y + 1] + *imagei ;
548 }
549 if (zeropad) {
550 for ( ; y >= - (signed)filterSize ; --y) {
551 buffer[y] = buffer[y + 1] ;
552 }
553 } else {
554 for ( ; y >= - (signed)filterSize ; --y) {
555 buffer[y] = buffer[y + 1] + *imagei ;
556 }
557 }
558
559 /* compute the filter forward */
560 for (y = - (signed)filterSize ;
561 y < (signed)imageHeight - (signed)filterSize ; ++y) {
562 buffer[y] = buffer[y] - buffer[y + filterSize] ;
563 }
564 if (! zeropad) {
565 for (y = (signed)imageHeight - (signed)filterSize ;
566 y < (signed)imageHeight ;
567 ++y) {
568 buffer[y] = buffer[y] - buffer[imageHeight - 1] *
569 ((signed)imageHeight - (signed)filterSize - y) ;
570 }
571 }
572
573 /* integrate forward the column */
574 for (y = - (signed)filterSize + 1 ;
575 y < (signed)imageHeight ; ++y) {
576 buffer[y] += buffer[y - 1] ;
577 }
578
579 /* compute the filter backward */
580 {
581 vl_size stride = transp ? 1 : destStride ;
582 dest += dheight * stride ;
583 for (y = step * (dheight - 1) ; y >= 0 ; y -= step) {
584 dest -= stride ;
585 *dest = scale * (buffer[y] - buffer[y - (signed)filterSize]) ;
586 }
587 dest += transp ? destStride : 1 ;
588 }
589 x += 1 ;
590 } /* next x */
591 vl_free (buffer - filterSize) ;
592 }
593
594 /* VL_TYPE_FLOAT, VL_TYPE_DOUBLE */
595 #endif
596
597 /* ---------------------------------------------------------------- */
598 /* Gaussian Smoothing */
599 /* ---------------------------------------------------------------- */
600
601 #if (FLT == VL_TYPE_FLOAT || FLT == VL_TYPE_DOUBLE)
602
603 /** @fn vl_imsmooth_d(double*,vl_size,double const*,vl_size,vl_size,vl_size,double,double)
604 ** @brief Smooth an image with a Gaussian filter
605 ** @param smoothed
606 ** @param smoothedStride
607 ** @param image
608 ** @param width
609 ** @param height
610 ** @param stride
611 ** @param sigmax
612 ** @param sigmay
613 **/
614
615 /** @fn vl_imsmooth_f(float*,vl_size,float const*,vl_size,vl_size,vl_size,double,double)
616 ** @brief Smooth an image with a Gaussian filter
617 ** @see ::vl_imsmooth_d
618 **/
619
620 static T*
621 VL_XCAT(_vl_new_gaussian_fitler_,SFX)(vl_size *size, double sigma)
622 {
623 T* filter ;
624 T mass = (T)1.0 ;
625 vl_index i ;
626 vl_size width = vl_ceil_d(sigma * 3.0) ;
627 *size = 2 * width + 1 ;
628
629 assert(size) ;
630
631 filter = vl_malloc((*size) * sizeof(T)) ;
632 filter[width] = 1.0 ;
633 for (i = 1 ; i <= (signed)width ; ++i) {
634 double x = (double)i / sigma ;
635 double g = exp(-0.5 * x * x) ;
636 mass += g + g ;
637 filter[width-i] = g ;
638 filter[width+i] = g ;
639 }
640 for (i = 0 ; i < (signed)(*size) ; ++i) {filter[i] /= mass ;}
641 return filter ;
642 }
643
644 VL_EXPORT void
645 VL_XCAT(vl_imsmooth_, SFX)
646 (T * smoothed, vl_size smoothedStride,
647 T const *image, vl_size width, vl_size height, vl_size stride,
648 double sigmax, double sigmay)
649 {
650 T *filterx, *filtery, *buffer ;
651 vl_size sizex, sizey ;
652
653 filterx = VL_XCAT(_vl_new_gaussian_fitler_,SFX)(&sizex,sigmax) ;
654 if (sigmax == sigmay) {
655 filtery = filterx ;
656 sizey = sizex ;
657 } else {
658 filtery = VL_XCAT(_vl_new_gaussian_fitler_,SFX)(&sizey,sigmay) ;
659 }
660 buffer = vl_malloc(width*height*sizeof(T)) ;
661
662 VL_XCAT(vl_imconvcol_v,SFX) (buffer, height,
663 image, width, height, stride,
664 filtery,
665 -((signed)sizey-1)/2, ((signed)sizey-1)/2,
666 1, VL_PAD_BY_CONTINUITY | VL_TRANSPOSE) ;
667
668 VL_XCAT(vl_imconvcol_v,SFX) (smoothed, smoothedStride,
669 buffer, height, width, height,
670 filterx,
671 -((signed)sizex-1)/2, ((signed)sizex-1)/2,
672 1, VL_PAD_BY_CONTINUITY | VL_TRANSPOSE) ;
673
674 vl_free(buffer) ;
675 vl_free(filterx) ;
676 if (sigmax != sigmay) {
677 vl_free(filtery) ;
678 }
679 }
680
681 /* VL_TYPE_FLOAT, VL_TYPE_DOUBLE */
682 #endif
683
684 /* ---------------------------------------------------------------- */
685 /* Image Gradient */
686 /* ---------------------------------------------------------------- */
687
688 #if (FLT == VL_TYPE_FLOAT || FLT == VL_TYPE_DOUBLE)
689
690 /** @fn vl_imgradient_d(double*,double*,vl_size,vl_size,double*,vl_size,vl_size,vl_size)
691 ** @brief Compute image gradient
692 ** @param xGradient Pointer to amplitude gradient plane
693 ** @param yGradient Pointer to angle gradient plane
694 ** @param gradWidthStride Width of the gradient plane including padding
695 ** @param gradHeightStride Height of the gradient plane including padding
696 ** @param image Pointer to the source image
697 ** @param imageWidth Source image width
698 ** @param imageHeight Source image height
699 ** @param imageStride Width of the image including padding.
700 **
701 ** This functions computes the amplitudes and angles of input image gradient.
702 **
703 ** Gradient is computed simple by gradient kernel \f$ (-1 ~ 1) \f$,
704 ** \f$ (-1 ~ 1)^T \f$ for border pixels and with sobel filter kernel
705 ** \f$ (-0.5 ~ 0 ~ 0.5) \f$, \f$ (-0.5 ~ 0 ~ 0.5)^T \f$ otherwise on the input
706 ** image @a image yielding x-gradient \f$ dx \f$, stored in @a xGradient and
707 ** y-gradient \f$ dy \f$, stored in @a yGradient, respectively.
708 **
709 ** This function also allows to process only part of the input image
710 ** defining the @a imageStride as original image width and @a width as
711 ** width of the sub-image.
712 **
713 ** Also it allows to easily align the output data by definition
714 ** of the @a gradWidthStride and @a gradHeightStride .
715 **/
716
717 /** @fn vl_imgradient_f(float*,float*,vl_size,vl_size,float*,vl_size,vl_size,vl_size)
718 ** @brief Compute image gradient
719 ** @see ::vl_imgradient_d
720 **/
721
722 VL_EXPORT void
723 VL_XCAT(vl_imgradient_, SFX)
724 (T * xGradient, T * yGradient,
725 vl_size gradWidthStride, vl_size gradHeightStride,
726 T const * image,
727 vl_size imageWidth, vl_size imageHeight,
728 vl_size imageStride)
729 {
730 /* Shortcuts */
731 vl_index const xo = 1 ;
732 vl_index const yo = imageStride ;
733 vl_size const w = imageWidth;
734 vl_size const h = imageHeight;
735
736 T const *src, *end ;
737 T *pgrad_x, *pgrad_y;
738 vl_size y;
739
740 src = image ;
741 pgrad_x = xGradient ;
742 pgrad_y = yGradient ;
743
744 /* first pixel of the first row */
745 *pgrad_x = src[+xo] - src[0] ;
746 pgrad_x += gradWidthStride;
747 *pgrad_y = src[+yo] - src[0] ;
748 pgrad_y += gradWidthStride;
749 src++;
750
751 /* middle pixels of the first row */
752 end = (src - 1) + w - 1 ;
753 while (src < end) {
754 *pgrad_x = 0.5 * (src[+xo] - src[-xo]) ;
755 pgrad_x += gradWidthStride;
756 *pgrad_y = src[+yo] - src[0] ;
757 pgrad_y += gradWidthStride;
758 src++;
759 }
760
761 /* last pixel of the first row */
762 *pgrad_x = src[0] - src[-xo] ;
763 pgrad_x += gradWidthStride;
764 *pgrad_y = src[+yo] - src[0] ;
765 pgrad_y += gradWidthStride;
766 src++;
767
768 xGradient += gradHeightStride;
769 pgrad_x = xGradient;
770 yGradient += gradHeightStride;
771 pgrad_y = yGradient;
772 image += yo;
773 src = image;
774
775 for (y = 1 ; y < h -1 ; ++y) {
776
777 /* first pixel of the middle rows */
778 *pgrad_x = src[+xo] - src[0] ;
779 pgrad_x += gradWidthStride;
780 *pgrad_y = 0.5 * (src[+yo] - src[-yo]) ;
781 pgrad_y += gradWidthStride;
782 src++;
783
784 /* middle pixels of the middle rows */
785 end = (src - 1) + w - 1 ;
786 while (src < end) {
787 *pgrad_x = 0.5 * (src[+xo] - src[-xo]) ;
788 pgrad_x += gradWidthStride;
789 *pgrad_y = 0.5 * (src[+yo] - src[-yo]) ;
790 pgrad_y += gradWidthStride;
791 src++;
792 }
793
794 /* last pixel of the middle row */
795 *pgrad_x = src[0] - src[-xo] ;
796 pgrad_x += gradWidthStride;
797 *pgrad_y = 0.5 * (src[+yo] - src[-yo]) ;
798 pgrad_y += gradWidthStride;
799 src++;
800
801 xGradient += gradHeightStride;
802 pgrad_x = xGradient;
803 yGradient += gradHeightStride;
804 pgrad_y = yGradient;
805 image += yo;
806 src = image;
807 }
808
809 /* first pixel of the last row */
810 *pgrad_x = src[+xo] - src[0] ;
811 pgrad_x += gradWidthStride;
812 *pgrad_y = src[ 0] - src[-yo] ;
813 pgrad_y += gradWidthStride;
814 src++;
815
816 /* middle pixels of the last row */
817 end = (src - 1) + w - 1 ;
818 while (src < end) {
819 *pgrad_x = 0.5 * (src[+xo] - src[-xo]) ;
820 pgrad_x += gradWidthStride;
821 *pgrad_y = src[0] - src[-yo] ;
822 pgrad_y += gradWidthStride;
823 src++;
824 }
825
826 /* last pixel of the last row */
827 *pgrad_x = src[0] - src[-xo] ;
828 *pgrad_y = src[0] - src[-yo] ;
829 }
830 /* VL_TYPE_FLOAT, VL_TYPE_DOUBLE */
831 #endif
832
833
834 /** @fn vl_imgradient_polar_d(double*,double*,vl_size,vl_size,double const*,vl_size,vl_size,vl_size)
835 ** @brief Compute gradient mangitudes and directions of an image.
836 ** @param amplitudeGradient Pointer to amplitude gradient plane
837 ** @param angleGradient Pointer to angle gradient plane
838 ** @param gradWidthStride Width of the gradient plane including padding
839 ** @param gradHeightStride Height of the gradient plane including padding
840 ** @param image Pointer to the source image
841 ** @param imageWidth Source image width
842 ** @param imageHeight Source image height
843 ** @param imageStride Width of the source image including padding.
844 **
845 ** This functions computes the amplitudes and angles of input image gradient.
846 **
847 ** Gradient is computed simple by gradient kernel \f$ (-1 ~ 1) \f$,
848 ** \f$ (-1 ~ 1)^T \f$ for border pixels and with sobel filter kernel
849 ** \f$ (-0.5 ~ 0 ~ 0.5) \f$, \f$ (-0.5 ~ 0 ~ 0.5)^T \f$ otherwise on
850 ** the input image @a image yielding x-gradient \f$ dx \f$, stored in
851 ** @a xGradient and y-gradient \f$ dy \f$, stored in @a yGradient,
852 ** respectively.
853 **
854 ** The amplitude of the gradient, stored in plane @a
855 ** amplitudeGradient, is then calculated as \f$ \sqrt(dx^2+dy^2) \f$
856 ** and the angle of the gradient, stored in @a angleGradient is \f$
857 ** atan(\frac{dy}{dx}) \f$ normalised into interval 0 and @f$ 2\pi
858 ** @f$.
859 **
860 ** This function also allows to process only part of the input image
861 ** defining the @a imageStride as original image width and @a width
862 ** as width of the sub-image.
863 **
864 ** Also it allows to easily align the output data by definition
865 ** of the @a gradWidthStride and @a gradHeightStride .
866 **/
867
868 /** @fn vl_imgradient_polar_f(float*,float*,vl_size,vl_size,float const*,vl_size,vl_size,vl_size)
869 ** @see ::vl_imgradient_polar_d
870 **/
871
872 #if (FLT == VL_TYPE_FLOAT || FLT == VL_TYPE_DOUBLE)
873
874 VL_EXPORT void
875 VL_XCAT(vl_imgradient_polar_, SFX)
876 (T * gradientModulus, T * gradientAngle,
877 vl_size gradientHorizontalStride, vl_size gradHeightStride,
878 T const* image,
879 vl_size imageWidth, vl_size imageHeight, vl_size imageStride)
880 {
881 /* Shortcuts */
882 vl_index const xo = 1 ;
883 vl_index const yo = imageStride ;
884 vl_size const w = imageWidth;
885 vl_size const h = imageHeight;
886
887 T const *src, *end;
888 T *pgrad_angl, *pgrad_ampl;
889 T gx, gy ;
890 vl_size y;
891
892 #define SAVE_BACK \
893 *pgrad_ampl = vl_fast_sqrt_f (gx*gx + gy*gy) ; \
894 pgrad_ampl += gradientHorizontalStride ; \
895 *pgrad_angl = vl_mod_2pi_f (vl_fast_atan2_f (gy, gx) + 2*VL_PI) ; \
896 pgrad_angl += gradientHorizontalStride ; \
897 ++src ; \
898
899 src = image ;
900 pgrad_angl = gradientAngle ;
901 pgrad_ampl = gradientModulus ;
902
903 /* first pixel of the first row */
904 gx = src[+xo] - src[0] ;
905 gy = src[+yo] - src[0] ;
906 SAVE_BACK ;
907
908 /* middle pixels of the first row */
909 end = (src - 1) + w - 1 ;
910 while (src < end) {
911 gx = 0.5 * (src[+xo] - src[-xo]) ;
912 gy = src[+yo] - src[0] ;
913 SAVE_BACK ;
914 }
915
916 /* last pixel of the first row */
917 gx = src[0] - src[-xo] ;
918 gy = src[+yo] - src[0] ;
919 SAVE_BACK ;
920
921 gradientModulus += gradHeightStride;
922 pgrad_ampl = gradientModulus;
923 gradientAngle += gradHeightStride;
924 pgrad_angl = gradientAngle;
925 image += imageStride;
926 src = image;
927
928 for (y = 1 ; y < h -1 ; ++y) {
929
930 /* first pixel of the middle rows */
931 gx = src[+xo] - src[0] ;
932 gy = 0.5 * (src[+yo] - src[-yo]) ;
933 SAVE_BACK ;
934
935 /* middle pixels of the middle rows */
936 end = (src - 1) + w - 1 ;
937 while (src < end) {
938 gx = 0.5 * (src[+xo] - src[-xo]) ;
939 gy = 0.5 * (src[+yo] - src[-yo]) ;
940 SAVE_BACK ;
941 }
942
943 /* last pixel of the middle row */
944 gx = src[0] - src[-xo] ;
945 gy = 0.5 * (src[+yo] - src[-yo]) ;
946 SAVE_BACK ;
947
948 gradientModulus += gradHeightStride;
949 pgrad_ampl = gradientModulus;
950 gradientAngle += gradHeightStride;
951 pgrad_angl = gradientAngle;
952 image += imageStride;
953 src = image;
954 }
955
956 /* first pixel of the last row */
957 gx = src[+xo] - src[0] ;
958 gy = src[ 0] - src[-yo] ;
959 SAVE_BACK ;
960
961 /* middle pixels of the last row */
962 end = (src - 1) + w - 1 ;
963 while (src < end) {
964 gx = 0.5 * (src[+xo] - src[-xo]) ;
965 gy = src[0] - src[-yo] ;
966 SAVE_BACK ;
967 }
968
969 /* last pixel of the last row */
970 gx = src[0] - src[-xo] ;
971 gy = src[0] - src[-yo] ;
972 SAVE_BACK ;
973
974 }
975 /* VL_TYPE_FLOAT, VL_TYPE_DOUBLE */
976 #endif
977
978 /* ---------------------------------------------------------------- */
979 /* Integral Image */
980 /* ---------------------------------------------------------------- */
981
982 /** @fn vl_imintegral_d(double*,vl_size,double const*,vl_size,vl_size,vl_size)
983 ** @brief Compute integral image
984 **
985 ** @param integral integral image.
986 ** @param integralStride integral image stride.
987 ** @param image source image.
988 ** @param imageWidth source image width.
989 ** @param imageHeight source image height.
990 ** @param imageStride source image stride.
991 **
992 ** Let @f$ I(x,y), (x,y) \in [0, W-1] \times [0, H-1] @f$. The
993 ** function computes the integral image @f$ J(x,y) @f$ of @f$ I(x,g)
994 ** @f$:
995 **
996 ** @f[
997 ** J(x,y) = \sum_{x'=0}^{x} \sum_{y'=0}^{y} I(x',y')
998 ** @f]
999 **
1000 ** The integral image @f$ J(x,y) @f$ can be used to compute quickly
1001 ** the integral of of @f$ I(x,y) @f$ in a rectangular region @f$ R =
1002 ** [x',x'']\times[y',y''] @f$:
1003 **
1004 ** @f[
1005 ** \sum_{(x,y)\in[x',x'']\times[y',y'']} I(x,y) =
1006 ** (J(x'',y'') - J(x'-1, y'')) - (J(x'',y'-1) - J(x'-1,y'-1)).
1007 ** @f]
1008 **
1009 ** Note that the order of operations is important when the integral image
1010 ** has an unsigned data type (e.g. ::vl_uint32). The formula
1011 ** is easily derived as follows:
1012 **
1013 ** @f{eqnarray*}
1014 ** \sum_{(x,y)\in R} I(x,y)
1015 ** &=& \sum_{x=x'}^{x''} \sum_{y=y'}^{y''} I(x,y)\\
1016 ** &=& \sum_{x=0}^{x''} \sum_{y=y'}^{y''} I(x,y)
1017 ** - \sum_{x=0}^{x'-1} \sum_{y=y'}^{y''} I(x,y)\\
1018 ** &=& \sum_{x=0}^{x''} \sum_{y=0}^{y''} I(x,y)
1019 ** - \sum_{x=0}^{x''} \sum_{y=0}^{y'-1} I(x,y)
1020 ** - \sum_{x=0}^{x'-1} \sum_{y=0}^{y''} I(x,y)
1021 ** + \sum_{x=0}^{x'-1} \sum_{y=0}^{y'-1} I(x,y)\\
1022 ** &=& J(x'',y'') - J(x'-1,y'') - J(x'',y'-1) + J(x'-1,y'-1).
1023 ** @f}
1024 **/
1025
1026 /** @fn vl_imintegral_f(float*,vl_size,float const*,vl_size,vl_size,vl_size)
1027 ** @brief Compute integral image
1028 ** @see ::vl_imintegral_d.
1029 **/
1030
1031 /** @fn vl_imintegral_ui32(vl_uint32*,vl_size,vl_uint32 const*,vl_size,vl_size,vl_size)
1032 ** @brief Compute integral image
1033 ** @see ::vl_imintegral_d.
1034 **/
1035
1036 /** @fn vl_imintegral_i32(vl_int32*,vl_size,vl_int32 const*,vl_size,vl_size,vl_size)
1037 ** @brief Compute integral image
1038 ** @see ::vl_imintegral_d.
1039 **/
1040
1041 VL_EXPORT void
1042 VL_XCAT(vl_imintegral_, SFX)
1043 (T * integral, vl_size integralStride,
1044 T const * image,
1045 vl_size imageWidth, vl_size imageHeight, vl_size imageStride)
1046 {
1047 vl_uindex x, y ;
1048 T temp = 0 ;
1049
1050 if (imageHeight > 0) {
1051 for (x = 0 ; x < imageWidth ; ++ x) {
1052 temp += *image++ ;
1053 *integral++ = temp ;
1054 }
1055 }
1056
1057 for (y = 1 ; y < imageHeight ; ++ y) {
1058 T * integralPrev ;
1059 integral += integralStride - imageWidth ;
1060 image += imageStride - imageWidth ;
1061 integralPrev = integral - integralStride ;
1062
1063 temp = 0 ;
1064 for (x = 0 ; x < imageWidth ; ++ x) {
1065 temp += *image++ ;
1066 *integral++ = *integralPrev++ + temp ;
1067 }
1068 }
1069 }
1070
1071 /* endif VL_IMOPV_INSTANTIATING */
1072 #undef FLT
1073 #undef VL_IMOPV_INSTANTIATING
1074 #endif