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.

1072 lines
37 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /** @file hog.c
  2. ** @brief Histogram of Oriented Gradients (HOG) - 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. #include "hog.h"
  12. #include "mathop.h"
  13. #include <string.h>
  14. /**
  15. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  16. @page hog Histogram of Oriented Gradients (HOG) features
  17. @author Andrea Vedaldi
  18. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  19. @ref hog.h implements the Histogram of Oriented Gradients (HOG) features
  20. in the variants of Dalal Triggs @cite{dalal05histograms} and of UOCTTI
  21. @cite{felzenszwalb09object}. Applications include object detection
  22. and deformable object detection.
  23. - @ref hog-overview
  24. - @ref hog-tech
  25. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  26. @section hog-overview Overview
  27. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  28. HOG is a standard image feature used, among others, in object detection
  29. and deformable object detection. It decomposes the image into square cells
  30. of a given size (typically eight pixels), compute a histogram of oriented
  31. gradient in each cell (similar to @ref sift), and then renormalizes
  32. the cells by looking into adjacent blocks.
  33. VLFeat implements two HOG variants: the original one of Dalal-Triggs
  34. @cite{dalal05histograms} and the one proposed in Felzenszwalb et al.
  35. @cite{felzenszwalb09object}.
  36. In order to use HOG, start by creating a new HOG object, set the desired
  37. parameters, pass a (color or grayscale) image, and read off the results.
  38. @code
  39. VlHog * hog = vl_hog_new(VlHogVariantDalalTriggs, numOrientations, VL_FALSE) ;
  40. vl_hog_put_image(hog, image, height, width, numChannels, cellSize) ;
  41. hogWidth = vl_hog_get_width(hog) ;
  42. hogHeight = vl_hog_get_height(hog) ;
  43. hogDimenison = vl_hog_get_dimension(hog) ;
  44. hogArray = vl_malloc(hogWidth*hogHeight*hogDimension*sizeof(float)) ;
  45. vl_hog_extract(hog, hogArray) ;
  46. vl_hog_delete(hog) ;
  47. @endcode
  48. HOG is a feature array of the dimension returned by ::vl_hog_get_width,
  49. ::vl_hog_get_height, with each feature (histogram) having
  50. dimension ::vl_hog_get_dimension. The array is stored in row major order,
  51. with the slowest varying dimension beying the dimension indexing the histogram
  52. elements.
  53. The number of entreis in the histogram as well as their meaning depends
  54. on the HOG variant and is detailed later. However, it is usually
  55. unnecessary to know such details. @ref hog.h provides support for
  56. creating an inconic representation of a HOG feature array:
  57. @code
  58. glyphSize = vl_hog_get_glyph_size(hog) ;
  59. imageHeight = glyphSize * hogArrayHeight ;
  60. imageWidth = glyphSize * hogArrayWidth ;
  61. image = vl_malloc(sizeof(float)*imageWidth*imageHeight) ;
  62. vl_hog_render(hog, image, hogArray) ;
  63. @endcode
  64. It is often convenient to mirror HOG features from left to right. This
  65. can be obtained by mirroring an array of HOG cells, but the content
  66. of each cell must also be rearranged. This can be done by
  67. the permutation obtaiend by ::vl_hog_get_permutation.
  68. Furthermore, @ref hog.h suppots computing HOG features not from
  69. images but from vector fields.
  70. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  71. @section hog-tech Technical details
  72. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  73. HOG divdes the input image into square cells of size @c cellSize,
  74. fitting as many cells as possible, filling the image domain from
  75. the upper-left corner down to the right one. For each row and column,
  76. the last cell is at least half contained in the image.
  77. More precisely, the number of cells obtained in this manner is:
  78. @code
  79. hogWidth = (width + cellSize/2) / cellSize ;
  80. hogHeight = (height + cellSize/2) / cellSize ;
  81. @endcode
  82. Then the image gradient @f$ \nabla \ell(x,y) @f$
  83. is computed by using central difference (for colour image
  84. the channel with the largest gradient at that pixel is used).
  85. The gradient @f$ \nabla \ell(x,y) @f$ is assigned to one of @c 2*numOrientations orientation in the
  86. range @f$ [0,2\pi) @f$ (see @ref hog-conventions for details).
  87. Contributions are then accumulated by using bilinear interpolation
  88. to four neigbhour cells, as in @ref sift.
  89. This results in an histogram @f$h_d@f$ of dimension
  90. 2*numOrientations, called of @e directed orientations
  91. since it accounts for the direction as well as the orientation
  92. of the gradient. A second histogram @f$h_u@f$ of undirected orientations
  93. of half the size is obtained by folding @f$ h_d @f$ into two.
  94. Let a block of cell be a @f$ 2\times 2 @f$ sub-array of cells.
  95. Let the norm of a block be the @f$ l^2 @f$ norm of the stacking of the
  96. respective unoriented histogram. Given a HOG cell, four normalisation
  97. factors are then obtained as the inverse of the norm of the four
  98. blocks that contain the cell.
  99. For the Dalal-Triggs variant, each histogram @f$ h_d @f$ is copied
  100. four times, normalised using the four different normalisation factors,
  101. the four vectors are stacked, saturated at 0.2, and finally stored as the descriptor
  102. of the cell. This results in a @c numOrientations * 4 dimensional
  103. cell descriptor. Blocks are visited from left to right and top to bottom
  104. when forming the final descriptor.
  105. For the UOCCTI descriptor, the same is done for both the undirected
  106. as well as the directed orientation histograms. This would yield
  107. a dimension of @c 4*(2+1)*numOrientations elements, but the resulting
  108. vector is projected down to @c (2+1)*numOrientations elements
  109. by averaging corresponding histogram dimensions. This was shown to
  110. be an algebraic approximation of PCA for descriptors computed on natural
  111. images.
  112. In addition, for the UOCTTI variant the l1 norm of each of the
  113. four l2 normalised undirected histograms is computed and stored
  114. as additional four dimensions, for a total of
  115. @c 4+3*numOrientations dimensions.
  116. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  117. @subsection hog-conventions Conventions
  118. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  119. The orientation of a gradient is expressed as the angle it forms with the
  120. horizontal axis of the image. Angles are measured clock-wise (as the vertical
  121. image axis points downards), and the null angle corresponds to
  122. an horizontal vector pointing right. The quantized directed
  123. orientations are @f$ \mathrm{k} \pi / \mathrm{numOrientations} @f$, where
  124. @c k is an index that varies in the ingeger
  125. range @f$ \{0, \dots, 2\mathrm{numOrientations} - 1\} @f$.
  126. Note that the orientations capture the orientation of the gradeint;
  127. image edges would be oriented at 90 degrees from these.
  128. **/
  129. /* ---------------------------------------------------------------- */
  130. /** @brief Create a new HOG object
  131. ** @param variant HOG descriptor variant.
  132. ** @param numOrientations number of distinguished orientations.
  133. ** @param transposed wether images are transposed (column major).
  134. ** @return the new HOG object.
  135. **
  136. ** The function creates a new HOG object to extract descriptors of
  137. ** the prescribed @c variant. The angular resolution is set by
  138. ** @a numOrientations, which specifies the number of <em>undirected</em>
  139. ** orientations. The object can work with column major images
  140. ** by setting @a transposed to true.
  141. **/
  142. VlHog *
  143. vl_hog_new (VlHogVariant variant, vl_size numOrientations, vl_bool transposed)
  144. {
  145. vl_index o, k ;
  146. VlHog * self = (VlHog*)vl_calloc(1, sizeof(VlHog)) ;
  147. assert(numOrientations >= 1) ;
  148. self->variant = variant ;
  149. self->numOrientations = numOrientations ;
  150. self->glyphSize = 21 ;
  151. self->transposed = transposed ;
  152. self->useBilinearOrientationAssigment = VL_FALSE ;
  153. self->orientationX = (float*)vl_malloc(sizeof(float) * self->numOrientations) ;
  154. self->orientationY = (float*)vl_malloc(sizeof(float) * self->numOrientations) ;
  155. /*
  156. Create a vector along the center of each orientation bin. These
  157. are used to map gradients to bins. If the image is transposed,
  158. then this can be adjusted here by swapping X and Y in these
  159. vectors.
  160. */
  161. for(o = 0 ; o < (signed)self->numOrientations ; ++o) {
  162. double angle = o * VL_PI / self->numOrientations ;
  163. if (!self->transposed) {
  164. self->orientationX[o] = (float) cos(angle) ;
  165. self->orientationY[o] = (float) sin(angle) ;
  166. } else {
  167. self->orientationX[o] = (float) sin(angle) ;
  168. self->orientationY[o] = (float) cos(angle) ;
  169. }
  170. }
  171. /*
  172. If the number of orientation is equal to 9, one gets:
  173. Uoccti:: 18 directed orientations + 9 undirected orientations + 4 texture
  174. DalalTriggs:: 9 undirected orientations x 4 blocks.
  175. */
  176. switch (self->variant) {
  177. case VlHogVariantUoctti:
  178. self->dimension = 3*self->numOrientations + 4 ;
  179. break ;
  180. case VlHogVariantDalalTriggs:
  181. self->dimension = 4*self->numOrientations ;
  182. break ;
  183. default:
  184. assert(0) ;
  185. }
  186. /*
  187. A permutation specifies how to permute elements in a HOG
  188. descriptor to flip it horizontally. Since the first orientation
  189. of index 0 points to the right, this must be swapped with orientation
  190. self->numOrientation that points to the left (for the directed case,
  191. and to itself for the undirected one).
  192. */
  193. self->permutation = (vl_index*)vl_malloc(self->dimension * sizeof(vl_index)) ;
  194. switch (self->variant) {
  195. case VlHogVariantUoctti:
  196. for(o = 0 ; o < (signed)self->numOrientations ; ++o) {
  197. vl_index op = self->numOrientations - o ;
  198. self->permutation[o] = op ;
  199. self->permutation[o + self->numOrientations] = (op + self->numOrientations) % (2*self->numOrientations) ;
  200. self->permutation[o + 2*self->numOrientations] = (op % self->numOrientations) + 2*self->numOrientations ;
  201. }
  202. for (k = 0 ; k < 4 ; ++k) {
  203. /* The texture features correspond to four displaced block around
  204. a cell. These permute with a lr flip as for DalalTriggs. */
  205. vl_index blockx = k % 2 ;
  206. vl_index blocky = k / 2 ;
  207. vl_index q = (1 - blockx) + blocky * 2 ;
  208. self->permutation[k + self->numOrientations * 3] = q + self->numOrientations * 3 ;
  209. }
  210. break ;
  211. case VlHogVariantDalalTriggs:
  212. for(k = 0 ; k < 4 ; ++k) {
  213. /* Find the corresponding block. Blocks are listed in order 1,2,3,4,...
  214. from left to right and top to bottom */
  215. vl_index blockx = k % 2 ;
  216. vl_index blocky = k / 2 ;
  217. vl_index q = (1 - blockx) + blocky * 2 ;
  218. for(o = 0 ; o < (signed)self->numOrientations ; ++o) {
  219. vl_index op = self->numOrientations - o ;
  220. self->permutation[o + k*self->numOrientations] = (op % self->numOrientations) + q*self->numOrientations ;
  221. }
  222. }
  223. break ;
  224. default:
  225. assert(0) ;
  226. }
  227. /*
  228. Create glyphs for representing the HOG features/ filters. The glyphs
  229. are simple bars, oriented orthogonally to the gradients to represent
  230. image edges. If the object is configured to work on transposed image,
  231. the glyphs images are also stored in column-major.
  232. */
  233. self->glyphs = (float*)vl_calloc(self->glyphSize * self->glyphSize * self->numOrientations, sizeof(float)) ;
  234. #define atglyph(x,y,k) self->glyphs[(x) + self->glyphSize * (y) + self->glyphSize * self->glyphSize * (k)]
  235. for (o = 0 ; o < (signed)self->numOrientations ; ++o) {
  236. double angle = fmod(o * VL_PI / self->numOrientations + VL_PI/2, VL_PI) ;
  237. double x2 = self->glyphSize * cos(angle) / 2 ;
  238. double y2 = self->glyphSize * sin(angle) / 2 ;
  239. if (angle <= VL_PI / 4 || angle >= VL_PI * 3 / 4) {
  240. /* along horizontal direction */
  241. double slope = y2 / x2 ;
  242. double offset = (1 - slope) * (self->glyphSize - 1) / 2 ;
  243. vl_index skip = (1 - fabs(cos(angle))) / 2 * self->glyphSize ;
  244. vl_index i, j ;
  245. for (i = skip ; i < (signed)self->glyphSize - skip ; ++i) {
  246. j = vl_round_d(slope * i + offset) ;
  247. if (! self->transposed) {
  248. atglyph(i,j,o) = 1 ;
  249. } else {
  250. atglyph(j,i,o) = 1 ;
  251. }
  252. }
  253. } else {
  254. /* along vertical direction */
  255. double slope = x2 / y2 ;
  256. double offset = (1 - slope) * (self->glyphSize - 1) / 2 ;
  257. vl_index skip = (1 - sin(angle)) / 2 * self->glyphSize ;
  258. vl_index i, j ;
  259. for (j = skip ; j < (signed)self->glyphSize - skip; ++j) {
  260. i = vl_round_d(slope * j + offset) ;
  261. if (! self->transposed) {
  262. atglyph(i,j,o) = 1 ;
  263. } else {
  264. atglyph(j,i,o) = 1 ;
  265. }
  266. }
  267. }
  268. }
  269. return self ;
  270. }
  271. /* ---------------------------------------------------------------- */
  272. /** @brief Delete a HOG object
  273. ** @param self HOG object to delete.
  274. **/
  275. void
  276. vl_hog_delete (VlHog * self)
  277. {
  278. if (self->orientationX) {
  279. vl_free(self->orientationX) ;
  280. self->orientationX = NULL ;
  281. }
  282. if (self->orientationY) {
  283. vl_free(self->orientationY) ;
  284. self->orientationY = NULL ;
  285. }
  286. if (self->glyphs) {
  287. vl_free(self->glyphs) ;
  288. self->glyphs = NULL ;
  289. }
  290. if (self->permutation) {
  291. vl_free(self->permutation) ;
  292. self->permutation = NULL ;
  293. }
  294. if (self->hog) {
  295. vl_free(self->hog) ;
  296. self->hog = NULL ;
  297. }
  298. if (self->hogNorm) {
  299. vl_free(self->hogNorm) ;
  300. self->hogNorm = NULL ;
  301. }
  302. vl_free(self) ;
  303. }
  304. /* ---------------------------------------------------------------- */
  305. /** @brief Get HOG glyph size
  306. ** @param self HOG object.
  307. ** @return size (height and width) of a glyph.
  308. **/
  309. vl_size
  310. vl_hog_get_glyph_size (VlHog const * self)
  311. {
  312. return self->glyphSize ;
  313. }
  314. /* ---------------------------------------------------------------- */
  315. /** @brief Get HOG left-right flip permutation
  316. ** @param self HOG object.
  317. ** @return left-right permutation.
  318. **
  319. ** The function returns a pointer to an array @c permutation of ::vl_hog_get_dimension
  320. ** elements. Given a HOG descriptor (for a cell) @c hog, which is also
  321. ** a vector of ::vl_hog_get_dimension elements, the
  322. ** descriptor obtained for the same image flipped horizotnally is
  323. ** given by <code>flippedHog[i] = hog[permutation[i]]</code>.
  324. **/
  325. vl_index const *
  326. vl_hog_get_permutation (VlHog const * self)
  327. {
  328. return self->permutation ;
  329. }
  330. /* ---------------------------------------------------------------- */
  331. /** @brief Turn bilinear interpolation of assignments on or off
  332. ** @param self HOG object.
  333. ** @param x @c true if orientations should be assigned with bilinear interpolation.
  334. **/
  335. void
  336. vl_hog_set_use_bilinear_orientation_assignments (VlHog * self, vl_bool x) {
  337. self->useBilinearOrientationAssigment = x ;
  338. }
  339. /** @brief Tell whether assignments use bilinear interpolation or not
  340. ** @param self HOG object.
  341. ** @return @c true if orientations are be assigned with bilinear interpolation.
  342. **/
  343. vl_bool
  344. vl_hog_get_use_bilinear_orientation_assignments (VlHog const * self) {
  345. return self->useBilinearOrientationAssigment ;
  346. }
  347. /* ---------------------------------------------------------------- */
  348. /** @brief Render a HOG descriptor to a glyph image
  349. ** @param self HOG object.
  350. ** @param image glyph image (output).
  351. ** @param descriptor HOG descriptor.
  352. ** @param width HOG descriptor width.
  353. ** @param height HOG descriptor height.
  354. **
  355. ** The function renders the HOG descriptor or filter
  356. ** @a descriptor as an image (for visualization) and stores the result in
  357. ** the buffer @a image. This buffer
  358. ** must be an array of dimensions @c width*glyphSize
  359. ** by @c height*glyphSize elements, where @c glyphSize is
  360. ** obtained from ::vl_hog_get_glyph_size and is the size in pixels
  361. ** of the image element used to represent the descriptor of one
  362. ** HOG cell.
  363. **/
  364. void
  365. vl_hog_render (VlHog const * self,
  366. float * image,
  367. float const * descriptor,
  368. vl_size width,
  369. vl_size height)
  370. {
  371. vl_index x, y, k, cx, cy ;
  372. vl_size hogStride = width * height ;
  373. assert(self) ;
  374. assert(image) ;
  375. assert(descriptor) ;
  376. assert(width > 0) ;
  377. assert(height > 0) ;
  378. for (y = 0 ; y < (signed)height ; ++y) {
  379. for (x = 0 ; x < (signed)width ; ++x) {
  380. float minWeight = 0 ;
  381. float maxWeight = 0 ;
  382. for (k = 0 ; k < (signed)self->numOrientations ; ++k) {
  383. float weight ;
  384. float const * glyph = self->glyphs + k * (self->glyphSize*self->glyphSize) ;
  385. float * glyphImage = image + self->glyphSize * x + y * width * (self->glyphSize*self->glyphSize) ;
  386. switch (self->variant) {
  387. case VlHogVariantUoctti:
  388. weight =
  389. descriptor[k * hogStride] +
  390. descriptor[(k + self->numOrientations) * hogStride] +
  391. descriptor[(k + 2 * self->numOrientations) * hogStride] ;
  392. break ;
  393. case VlHogVariantDalalTriggs:
  394. weight =
  395. descriptor[k * hogStride] +
  396. descriptor[(k + self->numOrientations) * hogStride] +
  397. descriptor[(k + 2 * self->numOrientations) * hogStride] +
  398. descriptor[(k + 3 * self->numOrientations) * hogStride] ;
  399. break ;
  400. default:
  401. abort() ;
  402. }
  403. maxWeight = VL_MAX(weight, maxWeight) ;
  404. minWeight = VL_MIN(weight, minWeight);
  405. for (cy = 0 ; cy < (signed)self->glyphSize ; ++cy) {
  406. for (cx = 0 ; cx < (signed)self->glyphSize ; ++cx) {
  407. *glyphImage++ += weight * (*glyph++) ;
  408. }
  409. glyphImage += (width - 1) * self->glyphSize ;
  410. }
  411. } /* next orientation */
  412. {
  413. float * glyphImage = image + self->glyphSize * x + y * width * (self->glyphSize*self->glyphSize) ;
  414. for (cy = 0 ; cy < (signed)self->glyphSize ; ++cy) {
  415. for (cx = 0 ; cx < (signed)self->glyphSize ; ++cx) {
  416. float value = *glyphImage ;
  417. *glyphImage++ = VL_MAX(minWeight, VL_MIN(maxWeight, value)) ;
  418. }
  419. glyphImage += (width - 1) * self->glyphSize ;
  420. }
  421. }
  422. ++ descriptor ;
  423. } /* next column of cells (x) */
  424. } /* next row of cells (y) */
  425. }
  426. /* ---------------------------------------------------------------- */
  427. /** @brief Get the dimension of the HOG features
  428. ** @param self HOG object.
  429. ** @return imension of a HOG cell descriptors.
  430. **/
  431. vl_size
  432. vl_hog_get_dimension (VlHog const * self)
  433. {
  434. return self->dimension ;
  435. }
  436. /** @brief Get the width of the HOG cell array
  437. ** @param self HOG object.
  438. ** @return number of HOG cells in the horizontal direction.
  439. **/
  440. vl_size
  441. vl_hog_get_width (VlHog * self)
  442. {
  443. return self->hogWidth ;
  444. }
  445. /** @brief Get the height of the HOG cell array
  446. ** @param self HOG object.
  447. ** @return number of HOG cells in the vertical direction.
  448. **/
  449. vl_size
  450. vl_hog_get_height (VlHog * self)
  451. {
  452. return self->hogHeight ;
  453. }
  454. /* ---------------------------------------------------------------- */
  455. /** @internal @brief Prepare internal buffers
  456. ** @param self HOG object.
  457. ** @param width image width.
  458. ** @param height image height.
  459. ** @param cellSize size of a HOG cell.
  460. **/
  461. static void
  462. vl_hog_prepare_buffers (VlHog * self, vl_size width, vl_size height, vl_size cellSize)
  463. {
  464. vl_size hogWidth = (width + cellSize/2) / cellSize ;
  465. vl_size hogHeight = (height + cellSize/2) / cellSize ;
  466. assert(width > 3) ;
  467. assert(height > 3) ;
  468. assert(hogWidth > 0) ;
  469. assert(hogHeight > 0) ;
  470. if (self->hog &&
  471. self->hogWidth == hogWidth &&
  472. self->hogHeight == hogHeight) {
  473. /* a suitable buffer is already allocated */
  474. memset(self->hog, 0, sizeof(float) * hogWidth * hogHeight * self->numOrientations * 2) ;
  475. memset(self->hogNorm, 0, sizeof(float) * hogWidth * hogHeight) ;
  476. return ;
  477. }
  478. if (self->hog) {
  479. vl_free(self->hog) ;
  480. self->hog = NULL ;
  481. }
  482. if (self->hogNorm) {
  483. vl_free(self->hogNorm) ;
  484. self->hogNorm = NULL ;
  485. }
  486. self->hog = (float*)vl_calloc(hogWidth * hogHeight * self->numOrientations * 2, sizeof(float)) ;
  487. self->hogNorm = (float*)vl_calloc(hogWidth * hogHeight, sizeof(float)) ;
  488. self->hogWidth = hogWidth ;
  489. self->hogHeight = hogHeight ;
  490. }
  491. /* ---------------------------------------------------------------- */
  492. /** @brief Process features starting from an image
  493. ** @param self HOG object.
  494. ** @param image image to process.
  495. ** @param width image width.
  496. ** @param height image height.
  497. ** @param numChannels number of image channles.
  498. ** @param cellSize size of a HOG cell.
  499. **
  500. ** The buffer @c hog must be a three-dimensional array.
  501. ** The first two dimensions are @c (width + cellSize/2)/cellSize and
  502. ** @c (height + cellSize/2)/cellSize, where divisions are integer.
  503. ** This is approximately @c width/cellSize and @c height/cellSize,
  504. ** adjusted so that the last cell is at least half contained in the
  505. ** image.
  506. **
  507. ** The image @c width and @c height must be not smaller than three
  508. ** pixels and not smaller than @c cellSize.
  509. **/
  510. void
  511. vl_hog_put_image (VlHog * self,
  512. float const * image,
  513. vl_size width, vl_size height, vl_size numChannels,
  514. vl_size cellSize)
  515. {
  516. vl_size hogStride ;
  517. vl_size channelStride = width * height ;
  518. vl_index x, y ;
  519. vl_uindex k ;
  520. assert(self) ;
  521. assert(image) ;
  522. /* clear features */
  523. vl_hog_prepare_buffers(self, width, height, cellSize) ;
  524. hogStride = self->hogWidth * self->hogHeight ;
  525. #define at(x,y,k) (self->hog[(x) + (y) * self->hogWidth + (k) * hogStride])
  526. /* compute gradients and map the to HOG cells by bilinear interpolation */
  527. for (y = 1 ; y < (signed)height - 1 ; ++y) {
  528. for (x = 1 ; x < (signed)width - 1 ; ++x) {
  529. float gradx = 0 ;
  530. float grady = 0 ;
  531. float gradNorm ;
  532. float orientationWeights [2] = {-1, -1} ;
  533. vl_index orientationBins [2] = {-1, -1} ;
  534. vl_index orientation = 0 ;
  535. float hx, hy, wx1, wx2, wy1, wy2 ;
  536. vl_index binx, biny, o ;
  537. /*
  538. Compute the gradient at (x,y). The image channel with
  539. the maximum gradient at each location is selected.
  540. */
  541. {
  542. float const * iter = image + y * width + x ;
  543. float gradNorm2 = 0 ;
  544. for (k = 0 ; k < numChannels ; ++k) {
  545. float gradx_ = *(iter + 1) - *(iter - 1) ;
  546. float grady_ = *(iter + width) - *(iter - width) ;
  547. float gradNorm2_ = gradx_ * gradx_ + grady_ * grady_ ;
  548. if (gradNorm2_ > gradNorm2) {
  549. gradx = gradx_ ;
  550. grady = grady_ ;
  551. gradNorm2 = gradNorm2_ ;
  552. }
  553. iter += channelStride ;
  554. }
  555. gradNorm = sqrtf(gradNorm2) ;
  556. }
  557. /*
  558. Map the gradient to the closest and second closets orientation bins.
  559. There are numOrientations orientation in the interval [0,pi).
  560. The next numOriantations are the symmetric ones, for a total
  561. of 2*numOrientation directed orientations.
  562. */
  563. for (k = 0 ; k < self->numOrientations ; ++k) {
  564. float orientationScore_ = gradx * self->orientationX[k] + grady * self->orientationY[k] ;
  565. vl_index orientationBin_ = k ;
  566. if (orientationScore_ < 0) {
  567. orientationScore_ = - orientationScore_ ;
  568. orientationBin_ += self->numOrientations ;
  569. }
  570. if (orientationScore_ > orientationWeights[0]) {
  571. orientationBins[1] = orientationBins[0] ;
  572. orientationWeights[1] = orientationWeights[0] ;
  573. orientationBins[0] = orientationBin_ ; ;
  574. orientationWeights[0] = orientationScore_ ;
  575. } else if (orientationScore_ > orientationWeights[1]) {
  576. orientationBins[1] = orientationBin_ ;
  577. orientationWeights[1] = orientationScore_ ;
  578. }
  579. }
  580. if (self->useBilinearOrientationAssigment) {
  581. /* min(1.0,...) guards against small overflows causing NaNs */
  582. float angle0 = acosf(VL_MIN(orientationWeights[0] / VL_MAX(gradNorm, 1e-10),1.0)) ;
  583. orientationWeights[1] = angle0 / (VL_PI / self->numOrientations) ;
  584. orientationWeights[0] = 1 - orientationWeights[1] ;
  585. } else {
  586. orientationWeights[0] = 1 ;
  587. orientationBins[1] = -1 ;
  588. }
  589. for (o = 0 ; o < 2 ; ++o) {
  590. float ow ;
  591. /*
  592. Accumulate the gradient. hx is the distance of the
  593. pixel x to the cell center at its left, in units of cellSize.
  594. With this parametrixation, a pixel on the cell center
  595. has hx = 0, which gradually increases to 1 moving to the next
  596. center.
  597. */
  598. orientation = orientationBins[o] ;
  599. if (orientation < 0) continue ;
  600. /* (x - (w-1)/2) / w = (x + 0.5)/w - 0.5 */
  601. hx = (x + 0.5) / cellSize - 0.5 ;
  602. hy = (y + 0.5) / cellSize - 0.5 ;
  603. binx = vl_floor_f(hx) ;
  604. biny = vl_floor_f(hy) ;
  605. wx2 = hx - binx ;
  606. wy2 = hy - biny ;
  607. wx1 = 1.0 - wx2 ;
  608. wy1 = 1.0 - wy2 ;
  609. ow = orientationWeights[o] ;
  610. /*VL_PRINTF("%d %d - %d %d %f %f - %f %f %f %f - %d \n ",x,y,binx,biny,hx,hy,wx1,wx2,wy1,wy2,o);*/
  611. if (binx >= 0 && biny >=0) {
  612. at(binx,biny,orientation) += gradNorm * ow * wx1 * wy1 ;
  613. }
  614. if (binx < (signed)self->hogWidth - 1 && biny >=0) {
  615. at(binx+1,biny,orientation) += gradNorm * ow * wx2 * wy1 ;
  616. }
  617. if (binx < (signed)self->hogWidth - 1 && biny < (signed)self->hogHeight - 1) {
  618. at(binx+1,biny+1,orientation) += gradNorm * ow * wx2 * wy2 ;
  619. }
  620. if (binx >= 0 && biny < (signed)self->hogHeight - 1) {
  621. at(binx,biny+1,orientation) += gradNorm * ow * wx1 * wy2 ;
  622. }
  623. } /* next o */
  624. } /* next x */
  625. } /* next y */
  626. }
  627. /* ---------------------------------------------------------------- */
  628. /** @brief Process features starting from a field in polar notation
  629. ** @param self HOG object.
  630. ** @param modulus image gradient modulus.
  631. ** @param angle image gradient angle.
  632. ** @param directed wrap the gradient angles at 2pi (directed) or pi (undirected).
  633. ** @param width image width.
  634. ** @param height image height.
  635. ** @param cellSize size of a HOG cell.
  636. **
  637. ** The function behaves like ::vl_hog_put_image, but foregoes the internal
  638. ** computation of the gradient field, allowing the user to specify
  639. ** their own. Angles are measure clockwise, the y axis pointing downwards,
  640. ** starting from the x axis (pointing to the right).
  641. **/
  642. void vl_hog_put_polar_field (VlHog * self,
  643. float const * modulus,
  644. float const * angle,
  645. vl_bool directed,
  646. vl_size width, vl_size height,
  647. vl_size cellSize)
  648. {
  649. vl_size hogStride ;
  650. vl_index x, y, o ;
  651. vl_index period = self->numOrientations * (directed ? 2 : 1) ;
  652. double angleStep = VL_PI / self->numOrientations ;
  653. assert(self) ;
  654. assert(modulus) ;
  655. assert(angle) ;
  656. /* clear features */
  657. vl_hog_prepare_buffers(self, width, height, cellSize) ;
  658. hogStride = self->hogWidth * self->hogHeight ;
  659. #define at(x,y,k) (self->hog[(x) + (y) * self->hogWidth + (k) * hogStride])
  660. #define atNorm(x,y) (self->hogNorm[(x) + (y) * self->hogWidth])
  661. /* fill HOG cells from gradient field */
  662. for (y = 0 ; y < (signed)height ; ++y) {
  663. for (x = 0 ; x < (signed)width ; ++x) {
  664. float ho, hx, hy, wo1, wo2, wx1, wx2, wy1, wy2 ;
  665. vl_index bino, binx, biny ;
  666. float orientationWeights [2] = {0,0} ;
  667. vl_index orientationBins [2] = {-1,-1} ;
  668. vl_index orientation = 0 ;
  669. float thisAngle = *angle++ ;
  670. float thisModulus = *modulus++ ;
  671. if (thisModulus <= 0.0f) continue ;
  672. /* (x - (w-1)/2) / w = (x + 0.5)/w - 0.5 */
  673. ho = (float)thisAngle / angleStep ;
  674. bino = vl_floor_f(ho) ;
  675. wo2 = ho - bino ;
  676. wo1 = 1.0f - wo2 ;
  677. while (bino < 0) { bino += self->numOrientations * 2 ; }
  678. if (self->useBilinearOrientationAssigment) {
  679. orientationBins[0] = bino % period ;
  680. orientationBins[1] = (bino + 1) % period ;
  681. orientationWeights[0] = wo1 ;
  682. orientationWeights[1] = wo2 ;
  683. } else {
  684. orientationBins[0] = (bino + ((wo1 > wo2) ? 0 : 1)) % period ;
  685. orientationWeights[0] = 1 ;
  686. orientationBins[1] = -1 ;
  687. }
  688. for (o = 0 ; o < 2 ; ++o) {
  689. /*
  690. Accumulate the gradient. hx is the distance of the
  691. pixel x to the cell center at its left, in units of cellSize.
  692. With this parametrixation, a pixel on the cell center
  693. has hx = 0, which gradually increases to 1 moving to the next
  694. center.
  695. */
  696. orientation = orientationBins[o] ;
  697. if (orientation < 0) continue ;
  698. hx = (x + 0.5) / cellSize - 0.5 ;
  699. hy = (y + 0.5) / cellSize - 0.5 ;
  700. binx = vl_floor_f(hx) ;
  701. biny = vl_floor_f(hy) ;
  702. wx2 = hx - binx ;
  703. wy2 = hy - biny ;
  704. wx1 = 1.0 - wx2 ;
  705. wy1 = 1.0 - wy2 ;
  706. wx1 *= orientationWeights[o] ;
  707. wx2 *= orientationWeights[o] ;
  708. wy1 *= orientationWeights[o] ;
  709. wy2 *= orientationWeights[o] ;
  710. /*VL_PRINTF("%d %d - %d %d %f %f - %f %f %f %f - %d \n ",x,y,binx,biny,hx,hy,wx1,wx2,wy1,wy2,o);*/
  711. if (binx >= 0 && biny >=0) {
  712. at(binx,biny,orientation) += thisModulus * wx1 * wy1 ;
  713. }
  714. if (binx < (signed)self->hogWidth - 1 && biny >=0) {
  715. at(binx+1,biny,orientation) += thisModulus * wx2 * wy1 ;
  716. }
  717. if (binx < (signed)self->hogWidth - 1 && biny < (signed)self->hogHeight - 1) {
  718. at(binx+1,biny+1,orientation) += thisModulus * wx2 * wy2 ;
  719. }
  720. if (binx >= 0 && biny < (signed)self->hogHeight - 1) {
  721. at(binx,biny+1,orientation) += thisModulus * wx1 * wy2 ;
  722. }
  723. } /* next o */
  724. } /* next x */
  725. } /* next y */
  726. }
  727. /* ---------------------------------------------------------------- */
  728. /** @brief Extract HOG features
  729. ** @param self HOG object.
  730. ** @param features HOG features (output).
  731. **
  732. ** This method is called after ::vl_hog_put_image or ::vl_hog_put_polar_field
  733. ** in order to retrieve the computed HOG features. The buffer @c features must have the dimensions returned by
  734. ** ::vl_hog_get_width, ::vl_hog_get_height, and ::vl_hog_get_dimension.
  735. **/
  736. void
  737. vl_hog_extract (VlHog * self, float * features)
  738. {
  739. vl_index x, y ;
  740. vl_uindex k ;
  741. vl_size hogStride = self->hogWidth * self->hogHeight ;
  742. assert(features) ;
  743. #define at(x,y,k) (self->hog[(x) + (y) * self->hogWidth + (k) * hogStride])
  744. #define atNorm(x,y) (self->hogNorm[(x) + (y) * self->hogWidth])
  745. /*
  746. Compute the squared L2 norm of the unoriented version of each HOG
  747. cell histogram. The unoriented version is obtained by folding
  748. the 2*numOrientations compotnent into numOrientations only.
  749. */
  750. {
  751. float const * iter = self->hog ;
  752. for (k = 0 ; k < self->numOrientations ; ++k) {
  753. float * niter = self->hogNorm ;
  754. float * niterEnd = self->hogNorm + self->hogWidth * self->hogHeight ;
  755. vl_size stride = self->hogWidth*self->hogHeight*self->numOrientations ;
  756. while (niter != niterEnd) {
  757. float h1 = *iter ;
  758. float h2 = *(iter + stride) ;
  759. float h = h1 + h2 ;
  760. *niter += h * h ;
  761. niter++ ;
  762. iter++ ;
  763. }
  764. }
  765. }
  766. /*
  767. HOG block-normalisation.
  768. The Dalal-Triggs implementation computes a normalized descriptor for
  769. each block of 2x2 cells, by stacking the histograms of each cell
  770. into a vector and L2-normalizing and truncating the result.
  771. Each block-level descriptor is then decomposed back into cells
  772. and corresponding parts are stacked into cell-level descritpors.
  773. Each HOG cell is contained in exactly
  774. four 2x2 cell blocks. For example, the cell number 5 in the following
  775. figure is contained in blocks 1245, 2356, 4578, 5689:
  776. +---+---+---+
  777. | 1 | 2 | 3 |
  778. +---+---+---+
  779. | 4 | 5 | 6 |
  780. +---+---+---+
  781. | 7 | 8 | 9 |
  782. +---+---+---+
  783. Hence, when block-level descriptors are decomposed back
  784. into cells, each cell receives contributions from four blocks. So,
  785. if each cell started with a D-dimensional histogram, it
  786. ends up with a 4D dimesional descriptor vector.
  787. Note however that this is just a convenient way of rewriting the
  788. blocks as per-cell contributions, but the block information
  789. is unchanged. In particular, barring boundary effects,
  790. in an array of H x W cells there are approximately HW blocks;
  791. hence the L2 norm of all the blocks stacked is approximately HW
  792. (because individual blocks are L2-normalized). Since this does
  793. not change in the final HOG descriptor,
  794. the L2 norm of the HOG descriptor of an image should be approximately
  795. the same as the area of the image divided by the
  796. area of a HOG cell. This can be used as a sanity check.
  797. The UoCTTI variant differs in some non-negligible ways. First,
  798. it includes both oriented and unoriented histograms, as well
  799. as four components capturing texture. Second, and most importantly,
  800. it merges the four chunks of block-level descirptors landing in
  801. each cell into one by taking their average. This makes sense
  802. because, ultimately, these four sub-descriptors are identical
  803. to the original cell histogram, just with four different normalisations
  804. applied.
  805. */
  806. {
  807. float const * iter = self->hog ;
  808. for (y = 0 ; y < (signed)self->hogHeight ; ++y) {
  809. for (x = 0 ; x < (signed)self->hogWidth ; ++x) {
  810. /* norm of upper-left, upper-right, ... cells */
  811. vl_index xm = VL_MAX(x - 1, 0) ;
  812. vl_index xp = VL_MIN(x + 1, (signed)self->hogWidth - 1) ;
  813. vl_index ym = VL_MAX(y - 1, 0) ;
  814. vl_index yp = VL_MIN(y + 1, (signed)self->hogHeight - 1) ;
  815. double norm1 = atNorm(xm,ym) ;
  816. double norm2 = atNorm(x,ym) ;
  817. double norm3 = atNorm(xp,ym) ;
  818. double norm4 = atNorm(xm,y) ;
  819. double norm5 = atNorm(x,y) ;
  820. double norm6 = atNorm(xp,y) ;
  821. double norm7 = atNorm(xm,yp) ;
  822. double norm8 = atNorm(x,yp) ;
  823. double norm9 = atNorm(xp,yp) ;
  824. double factor1, factor2, factor3, factor4 ;
  825. double t1 = 0 ;
  826. double t2 = 0 ;
  827. double t3 = 0 ;
  828. double t4 = 0 ;
  829. float * oiter = features + x + self->hogWidth * y ;
  830. /* each factor is the inverse of the l2 norm of one of the 2x2 blocks surrounding
  831. cell x,y */
  832. #if 0
  833. if (self->transposed) {
  834. /* if the image is transposed, y and x are swapped */
  835. factor1 = 1.0 / VL_MAX(sqrt(norm1 + norm2 + norm4 + norm5), 1e-10) ;
  836. factor3 = 1.0 / VL_MAX(sqrt(norm2 + norm3 + norm5 + norm6), 1e-10) ;
  837. factor2 = 1.0 / VL_MAX(sqrt(norm4 + norm5 + norm7 + norm8), 1e-10) ;
  838. factor4 = 1.0 / VL_MAX(sqrt(norm5 + norm6 + norm8 + norm9), 1e-10) ;
  839. } else {
  840. factor1 = 1.0 / VL_MAX(sqrt(norm1 + norm2 + norm4 + norm5), 1e-10) ;
  841. factor2 = 1.0 / VL_MAX(sqrt(norm2 + norm3 + norm5 + norm6), 1e-10) ;
  842. factor3 = 1.0 / VL_MAX(sqrt(norm4 + norm5 + norm7 + norm8), 1e-10) ;
  843. factor4 = 1.0 / VL_MAX(sqrt(norm5 + norm6 + norm8 + norm9), 1e-10) ;
  844. }
  845. #else
  846. /* as implemented in UOCTTI code */
  847. if (self->transposed) {
  848. /* if the image is transposed, y and x are swapped */
  849. factor1 = 1.0 / sqrt(norm1 + norm2 + norm4 + norm5 + 1e-4) ;
  850. factor3 = 1.0 / sqrt(norm2 + norm3 + norm5 + norm6 + 1e-4) ;
  851. factor2 = 1.0 / sqrt(norm4 + norm5 + norm7 + norm8 + 1e-4) ;
  852. factor4 = 1.0 / sqrt(norm5 + norm6 + norm8 + norm9 + 1e-4) ;
  853. } else {
  854. factor1 = 1.0 / sqrt(norm1 + norm2 + norm4 + norm5 + 1e-4) ;
  855. factor2 = 1.0 / sqrt(norm2 + norm3 + norm5 + norm6 + 1e-4) ;
  856. factor3 = 1.0 / sqrt(norm4 + norm5 + norm7 + norm8 + 1e-4) ;
  857. factor4 = 1.0 / sqrt(norm5 + norm6 + norm8 + norm9 + 1e-4) ;
  858. }
  859. #endif
  860. for (k = 0 ; k < self->numOrientations ; ++k) {
  861. double ha = iter[hogStride * k] ;
  862. double hb = iter[hogStride * (k + self->numOrientations)] ;
  863. double hc ;
  864. double ha1 = factor1 * ha ;
  865. double ha2 = factor2 * ha ;
  866. double ha3 = factor3 * ha ;
  867. double ha4 = factor4 * ha ;
  868. double hb1 = factor1 * hb ;
  869. double hb2 = factor2 * hb ;
  870. double hb3 = factor3 * hb ;
  871. double hb4 = factor4 * hb ;
  872. double hc1 = ha1 + hb1 ;
  873. double hc2 = ha2 + hb2 ;
  874. double hc3 = ha3 + hb3 ;
  875. double hc4 = ha4 + hb4 ;
  876. ha1 = VL_MIN(0.2, ha1) ;
  877. ha2 = VL_MIN(0.2, ha2) ;
  878. ha3 = VL_MIN(0.2, ha3) ;
  879. ha4 = VL_MIN(0.2, ha4) ;
  880. hb1 = VL_MIN(0.2, hb1) ;
  881. hb2 = VL_MIN(0.2, hb2) ;
  882. hb3 = VL_MIN(0.2, hb3) ;
  883. hb4 = VL_MIN(0.2, hb4) ;
  884. hc1 = VL_MIN(0.2, hc1) ;
  885. hc2 = VL_MIN(0.2, hc2) ;
  886. hc3 = VL_MIN(0.2, hc3) ;
  887. hc4 = VL_MIN(0.2, hc4) ;
  888. t1 += hc1 ;
  889. t2 += hc2 ;
  890. t3 += hc3 ;
  891. t4 += hc4 ;
  892. switch (self->variant) {
  893. case VlHogVariantUoctti :
  894. ha = 0.5 * (ha1 + ha2 + ha3 + ha4) ;
  895. hb = 0.5 * (hb1 + hb2 + hb3 + hb4) ;
  896. hc = 0.5 * (hc1 + hc2 + hc3 + hc4) ;
  897. *oiter = ha ;
  898. *(oiter + hogStride * self->numOrientations) = hb ;
  899. *(oiter + 2 * hogStride * self->numOrientations) = hc ;
  900. break ;
  901. case VlHogVariantDalalTriggs :
  902. *oiter = hc1 ;
  903. *(oiter + hogStride * self->numOrientations) = hc2 ;
  904. *(oiter + 2 * hogStride * self->numOrientations) = hc3 ;
  905. *(oiter + 3 * hogStride * self->numOrientations) = hc4 ;
  906. break ;
  907. }
  908. oiter += hogStride ;
  909. } /* next orientation */
  910. switch (self->variant) {
  911. case VlHogVariantUoctti :
  912. oiter += 2 * hogStride * self->numOrientations ;
  913. *oiter = (1.0f/sqrtf(18.0f)) * t1 ; oiter += hogStride ;
  914. *oiter = (1.0f/sqrtf(18.0f)) * t2 ; oiter += hogStride ;
  915. *oiter = (1.0f/sqrtf(18.0f)) * t3 ; oiter += hogStride ;
  916. *oiter = (1.0f/sqrtf(18.0f)) * t4 ; oiter += hogStride ;
  917. break ;
  918. case VlHogVariantDalalTriggs :
  919. break ;
  920. }
  921. ++iter ;
  922. } /* next x */
  923. } /* next y */
  924. } /* block normalization */
  925. }