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.

465 lines
14 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /** @file quickshift.c
  2. ** @brief Quick shift - Definition
  3. ** @author Brian Fulkerson
  4. ** @author Andrea Vedaldi
  5. **/
  6. /*
  7. Copyright (C) 2007-12 Andrea Vedaldi and Brian Fulkerson.
  8. All rights reserved.
  9. This file is part of the VLFeat library and is made available under
  10. the terms of the BSD license (see the COPYING file).
  11. */
  12. /**
  13. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  14. @page quickshift Quick shift image segmentation
  15. @author Brian Fulkerson
  16. @author Andrea Vedaldi
  17. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  18. @ref quickshift.h implements an image segmentation algorithm based on
  19. the quick shift clustering algorithm @cite{vedaldi08quick}.
  20. - @ref quickshift-intro
  21. - @ref quickshift-usage
  22. - @ref quickshift-tech
  23. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  24. @section quickshift-intro Overview
  25. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  26. Quick shift @cite{vedaldi08quick} is a fast mode seeking algorithm,
  27. similar to mean shift. The algorithm segments an RGB image (or any
  28. image with more than one channel) by identifying clusters of pixels in
  29. the joint spatial and color dimensions. Segments are local
  30. (superpixels) and can be used as a basis for further processing.
  31. Given an image, the algorithm calculates a forest of pixels whose
  32. branches are labeled with a distance value
  33. (::vl_quickshift_get_parents, ::vl_quickshift_get_dists). This
  34. specifies a hierarchical segmentation of the image, with segments
  35. corresponding to subtrees. Useful superpixels can be identified by
  36. cutting the branches whose distance label is above a given threshold
  37. (the threshold can be either fixed by hand, or determined by cross
  38. validation).
  39. Parameter influencing the algorithm are:
  40. - <b>Kernel size.</b> The pixel density and its modes are estimated by
  41. using a Parzen window estimator with a Gaussian kernel of the
  42. specified size (::vl_quickshift_set_kernel_size). The larger the size,
  43. the larger the neighborhoods of pixels considered.
  44. - <b>Maximum distance.</b> This (::vl_quickshift_set_max_dist) is the
  45. maximum distance between two pixels that the algorithm considers when
  46. building the forest. In principle, it can be infinity (so that a tree
  47. is returned), but in practice it is much faster to consider only
  48. relatively small distances (the maximum distance can be set to a small
  49. multiple of the kernel size).
  50. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  51. @section quickshift-usage Usage
  52. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  53. - Create a new quick shift object (::vl_quickshift_new). The object
  54. can be reused for multiple images of the same size.
  55. - Configure quick shift by setting the kernel size
  56. (::vl_quickshift_set_kernel_size) and the maximum gap
  57. (::vl_quickshift_set_max_dist). The latter is in principle not
  58. necessary, but useful to speedup processing.
  59. - Process an image (::vl_quickshift_process).
  60. - Retrieve the parents (::vl_quickshift_get_parents) and the distances
  61. (::vl_quickshift_get_dists). These can be used to segment
  62. the image in superpixels.
  63. - Delete the quick shift object (::vl_quickshift_delete).
  64. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  65. @section quickshift-tech Technical details
  66. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  67. For each pixel <em>(x,y)</em>, quick shift regards @f$ (x,y,I(x,y))
  68. @f$ as a sample from a <em>d + 2</em> dimensional vector space. It
  69. then calculates the Parzen density estimate (with a Gaussian kernel of
  70. standard deviation @f$ \sigma @f$)
  71. @f[
  72. E(x,y) = P(x,y,I(x,y)) = \sum_{x'y'}
  73. \frac{1}{(2\pi\sigma)^{d+2}}
  74. \exp
  75. \left(
  76. -\frac{1}{2\sigma^2}
  77. \left[
  78. \begin{array}{c}
  79. x - x' \\
  80. y - y' \\
  81. I(x,y) - I(x',y') \\
  82. \end{array}
  83. \right]
  84. \right)
  85. @f]
  86. Then quick shift construct a tree connecting each image pixel to its
  87. nearest neighbor which has greater density value. Formally, write @f$
  88. (x',y') >_P (x,y) @f$ if, and only if,
  89. @f[
  90. P(x',y',I(x',y')) > P(x,y,I(x,y))}.
  91. @f]
  92. Each pixel <em>(x, y)</em> is connected to the closest higher density
  93. pixel <em>parent(x, y)</em> that achieves the minimum distance in
  94. @f[
  95. \mathrm{dist}(x,y) =
  96. \mathrm{min}_{(x',y') > P(x,y)}
  97. \left(
  98. (x - x')^2 +
  99. (y - y')^2 +
  100. \| I(x,y) - I(x',y') \|_2^2
  101. \right).
  102. @f]
  103. **/
  104. #include "quickshift.h"
  105. #include "mathop.h"
  106. #include <string.h>
  107. #include <math.h>
  108. #include <stdio.h>
  109. /** -----------------------------------------------------------------
  110. ** @internal
  111. ** @brief Computes the accumulated channel L2 distance between
  112. ** i,j + the distance between i,j
  113. **
  114. ** @param I input image buffer
  115. ** @param N1 size of the first dimension of the image
  116. ** @param N2 size of the second dimension of the image
  117. ** @param K number of channels
  118. ** @param i1 first dimension index of the first pixel to compare
  119. ** @param i2 second dimension of the first pixel
  120. ** @param j1 index of the second pixel to compare
  121. ** @param j2 second dimension of the second pixel
  122. **
  123. ** Takes the L2 distance between the values in I at pixel i and j,
  124. ** accumulating along K channels and adding in the distance
  125. ** between i,j in the image.
  126. **
  127. ** @return the distance as described above
  128. **/
  129. VL_INLINE
  130. vl_qs_type
  131. vl_quickshift_distance(vl_qs_type const * I,
  132. int N1, int N2, int K,
  133. int i1, int i2,
  134. int j1, int j2)
  135. {
  136. vl_qs_type dist = 0 ;
  137. int d1 = j1 - i1 ;
  138. int d2 = j2 - i2 ;
  139. int k ;
  140. dist += d1*d1 + d2*d2 ;
  141. /* For k = 0...K-1, d+= L2 distance between I(i1,i2,k) and
  142. * I(j1,j2,k) */
  143. for (k = 0 ; k < K ; ++k) {
  144. vl_qs_type d =
  145. I [i1 + N1 * i2 + (N1*N2) * k] -
  146. I [j1 + N1 * j2 + (N1*N2) * k] ;
  147. dist += d*d ;
  148. }
  149. return dist ;
  150. }
  151. /** -----------------------------------------------------------------
  152. ** @internal
  153. ** @brief Computes the accumulated channel inner product between i,j + the
  154. ** distance between i,j
  155. **
  156. ** @param I input image buffer
  157. ** @param N1 size of the first dimension of the image
  158. ** @param N2 size of the second dimension of the image
  159. ** @param K number of channels
  160. ** @param i1 first dimension index of the first pixel to compare
  161. ** @param i2 second dimension of the first pixel
  162. ** @param j1 index of the second pixel to compare
  163. ** @param j2 second dimension of the second pixel
  164. **
  165. ** Takes the channel-wise inner product between the values in I at
  166. ** pixel i and j, accumulating along K channels and adding in the
  167. ** inner product between i,j in the image.
  168. **
  169. ** @return the inner product as described above
  170. **/
  171. VL_INLINE
  172. vl_qs_type
  173. vl_quickshift_inner(vl_qs_type const * I,
  174. int N1, int N2, int K,
  175. int i1, int i2,
  176. int j1, int j2)
  177. {
  178. vl_qs_type ker = 0 ;
  179. int k ;
  180. ker += i1*j1 + i2*j2 ;
  181. for (k = 0 ; k < K ; ++k) {
  182. ker +=
  183. I [i1 + N1 * i2 + (N1*N2) * k] *
  184. I [j1 + N1 * j2 + (N1*N2) * k] ;
  185. }
  186. return ker ;
  187. }
  188. /** -----------------------------------------------------------------
  189. ** @brief Create a quick shift object
  190. ** @param image the image.
  191. ** @param height the height (number of rows) of the image.
  192. ** @param width the width (number of columns) of the image.
  193. ** @param channels the number of channels of the image.
  194. ** @return new quick shift object.
  195. **
  196. ** The @c image is an array of ::vl_qs_type values with three
  197. ** dimensions (respectively @c widht, @c height, and @c
  198. ** channels). Typically, a color (e.g, RGB) image has three
  199. ** channels. The linear index of a pixel is computed with:
  200. ** @c channels * @c width* @c height + @c row + @c height * @c col.
  201. **/
  202. VL_EXPORT
  203. VlQS *
  204. vl_quickshift_new(vl_qs_type const * image, int height, int width,
  205. int channels)
  206. {
  207. VlQS * q = (VlQS*)vl_malloc(sizeof(VlQS));
  208. q->image = (vl_qs_type *)image;
  209. q->height = height;
  210. q->width = width;
  211. q->channels = channels;
  212. q->medoid = VL_FALSE;
  213. q->tau = VL_MAX(height,width)/50;
  214. q->sigma = VL_MAX(2, q->tau/3);
  215. q->dists = (vl_qs_type*)vl_calloc(height*width, sizeof(vl_qs_type));
  216. q->parents = (int*)vl_calloc(height*width, sizeof(int));
  217. q->density = (vl_qs_type*)vl_calloc(height*width, sizeof(vl_qs_type)) ;
  218. return q;
  219. }
  220. /** -----------------------------------------------------------------
  221. ** @brief Create a quick shift objet
  222. ** @param q quick shift object.
  223. **/
  224. VL_EXPORT
  225. void vl_quickshift_process(VlQS * q)
  226. {
  227. vl_qs_type const *I = q->image;
  228. int *parents = q->parents;
  229. vl_qs_type *E = q->density;
  230. vl_qs_type *dists = q->dists;
  231. vl_qs_type *M = 0, *n = 0 ;
  232. vl_qs_type sigma = q->sigma ;
  233. vl_qs_type tau = q->tau;
  234. vl_qs_type tau2 = tau*tau;
  235. int K = q->channels, d;
  236. int N1 = q->height, N2 = q->width;
  237. int i1,i2, j1,j2, R, tR;
  238. d = 2 + K ; /* Total dimensions include spatial component (x,y) */
  239. if (q->medoid) { /* n and M are only used in mediod shift */
  240. M = (vl_qs_type *) vl_calloc(N1*N2*d, sizeof(vl_qs_type)) ;
  241. n = (vl_qs_type *) vl_calloc(N1*N2, sizeof(vl_qs_type)) ;
  242. }
  243. R = (int) ceil (3 * sigma) ;
  244. tR = (int) ceil (tau) ;
  245. /* -----------------------------------------------------------------
  246. * n
  247. * -------------------------------------------------------------- */
  248. /* If we are doing medoid shift, initialize n to the inner product of the
  249. * image with itself
  250. */
  251. if (n) {
  252. for (i2 = 0 ; i2 < N2 ; ++ i2) {
  253. for (i1 = 0 ; i1 < N1 ; ++ i1) {
  254. n [i1 + N1 * i2] = vl_quickshift_inner(I,N1,N2,K,
  255. i1,i2,
  256. i1,i2) ;
  257. }
  258. }
  259. }
  260. /* -----------------------------------------------------------------
  261. * E = - [oN'*F]', M
  262. * -------------------------------------------------------------- */
  263. /*
  264. D_ij = d(x_i,x_j)
  265. E_ij = exp(- .5 * D_ij / sigma^2) ;
  266. F_ij = - E_ij
  267. E_i = sum_j E_ij
  268. M_di = sum_j X_j F_ij
  269. E is the parzen window estimate of the density
  270. 0 = dissimilar to everything, windowsize = identical
  271. */
  272. for (i2 = 0 ; i2 < N2 ; ++ i2) {
  273. for (i1 = 0 ; i1 < N1 ; ++ i1) {
  274. int j1min = VL_MAX(i1 - R, 0 ) ;
  275. int j1max = VL_MIN(i1 + R, N1-1) ;
  276. int j2min = VL_MAX(i2 - R, 0 ) ;
  277. int j2max = VL_MIN(i2 + R, N2-1) ;
  278. /* For each pixel in the window compute the distance between it and the
  279. * source pixel */
  280. for (j2 = j2min ; j2 <= j2max ; ++ j2) {
  281. for (j1 = j1min ; j1 <= j1max ; ++ j1) {
  282. vl_qs_type Dij = vl_quickshift_distance(I,N1,N2,K, i1,i2, j1,j2) ;
  283. /* Make distance a similarity */
  284. vl_qs_type Fij = - exp(- Dij / (2*sigma*sigma)) ;
  285. /* E is E_i above */
  286. E [i1 + N1 * i2] -= Fij ;
  287. if (M) {
  288. /* Accumulate votes for the median */
  289. int k ;
  290. M [i1 + N1*i2 + (N1*N2) * 0] += j1 * Fij ;
  291. M [i1 + N1*i2 + (N1*N2) * 1] += j2 * Fij ;
  292. for (k = 0 ; k < K ; ++k) {
  293. M [i1 + N1*i2 + (N1*N2) * (k+2)] +=
  294. I [j1 + N1*j2 + (N1*N2) * k] * Fij ;
  295. }
  296. }
  297. } /* j1 */
  298. } /* j2 */
  299. } /* i1 */
  300. } /* i2 */
  301. /* -----------------------------------------------------------------
  302. * Find best neighbors
  303. * -------------------------------------------------------------- */
  304. if (q->medoid) {
  305. /*
  306. Qij = - nj Ei - 2 sum_k Gjk Mik
  307. n is I.^2
  308. */
  309. /* medoid shift */
  310. for (i2 = 0 ; i2 < N2 ; ++i2) {
  311. for (i1 = 0 ; i1 < N1 ; ++i1) {
  312. vl_qs_type sc_best = 0 ;
  313. /* j1/j2 best are the best indicies for each i */
  314. vl_qs_type j1_best = i1 ;
  315. vl_qs_type j2_best = i2 ;
  316. int j1min = VL_MAX(i1 - R, 0 ) ;
  317. int j1max = VL_MIN(i1 + R, N1-1) ;
  318. int j2min = VL_MAX(i2 - R, 0 ) ;
  319. int j2max = VL_MIN(i2 + R, N2-1) ;
  320. for (j2 = j2min ; j2 <= j2max ; ++ j2) {
  321. for (j1 = j1min ; j1 <= j1max ; ++ j1) {
  322. vl_qs_type Qij = - n [j1 + j2 * N1] * E [i1 + i2 * N1] ;
  323. int k ;
  324. Qij -= 2 * j1 * M [i1 + i2 * N1 + (N1*N2) * 0] ;
  325. Qij -= 2 * j2 * M [i1 + i2 * N1 + (N1*N2) * 1] ;
  326. for (k = 0 ; k < K ; ++k) {
  327. Qij -= 2 *
  328. I [j1 + j2 * N1 + (N1*N2) * k] *
  329. M [i1 + i2 * N1 + (N1*N2) * (k + 2)] ;
  330. }
  331. if (Qij > sc_best) {
  332. sc_best = Qij ;
  333. j1_best = j1 ;
  334. j2_best = j2 ;
  335. }
  336. }
  337. }
  338. /* parents_i is the linear index of j which is the best pair
  339. * dists_i is the score of the best match
  340. */
  341. parents [i1 + N1 * i2] = j1_best + N1 * j2_best ;
  342. dists[i1 + N1 * i2] = sc_best ;
  343. }
  344. }
  345. } else {
  346. /* Quickshift assigns each i to the closest j which has an increase in the
  347. * density (E). If there is no j s.t. Ej > Ei, then dists_i == inf (a root
  348. * node in one of the trees of merges).
  349. */
  350. for (i2 = 0 ; i2 < N2 ; ++i2) {
  351. for (i1 = 0 ; i1 < N1 ; ++i1) {
  352. vl_qs_type E0 = E [i1 + N1 * i2] ;
  353. vl_qs_type d_best = VL_QS_INF ;
  354. vl_qs_type j1_best = i1 ;
  355. vl_qs_type j2_best = i2 ;
  356. int j1min = VL_MAX(i1 - tR, 0 ) ;
  357. int j1max = VL_MIN(i1 + tR, N1-1) ;
  358. int j2min = VL_MAX(i2 - tR, 0 ) ;
  359. int j2max = VL_MIN(i2 + tR, N2-1) ;
  360. for (j2 = j2min ; j2 <= j2max ; ++ j2) {
  361. for (j1 = j1min ; j1 <= j1max ; ++ j1) {
  362. if (E [j1 + N1 * j2] > E0) {
  363. vl_qs_type Dij = vl_quickshift_distance(I,N1,N2,K, i1,i2, j1,j2) ;
  364. if (Dij <= tau2 && Dij < d_best) {
  365. d_best = Dij ;
  366. j1_best = j1 ;
  367. j2_best = j2 ;
  368. }
  369. }
  370. }
  371. }
  372. /* parents is the index of the best pair */
  373. /* dists_i is the minimal distance, inf implies no Ej > Ei within
  374. * distance tau from the point */
  375. parents [i1 + N1 * i2] = j1_best + N1 * j2_best ;
  376. dists[i1 + N1 * i2] = sqrt(d_best) ;
  377. }
  378. }
  379. }
  380. if (M) vl_free(M) ;
  381. if (n) vl_free(n) ;
  382. }
  383. /** -----------------------------------------------------------------
  384. ** @brief Delete quick shift object
  385. ** @param q quick shift object.
  386. **/
  387. void vl_quickshift_delete(VlQS * q)
  388. {
  389. if (q) {
  390. if (q->parents) vl_free(q->parents);
  391. if (q->dists) vl_free(q->dists);
  392. if (q->density) vl_free(q->density);
  393. vl_free(q);
  394. }
  395. }