You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1074 lines
34 KiB

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