Debian Codegraph @

src/thirdparty/VLFeat/quickshift.c

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