| 1 |
/** @file slic.c |
| 2 |
** @brief SLIC superpixels - Definition |
| 3 |
** @author Andrea Vedaldi |
| 4 |
**/ |
| 5 |
|
| 6 |
/* |
| 7 |
Copyright (C) 2007-12 Andrea Vedaldi and Brian Fulkerson. |
| 8 |
All rights reserved. |
| 9 |
|
| 10 |
This file is part of the VLFeat library and is made available under |
| 11 |
the terms of the BSD license (see the COPYING file). |
| 12 |
*/ |
| 13 |
|
| 14 |
/** |
| 15 |
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --> |
| 16 |
@page slic Simple Linear Iterative Clustering (SLIC) |
| 17 |
@author Andrea Vedaldi |
| 18 |
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --> |
| 19 |
|
| 20 |
@ref slic.h implements the *Simple Linear Iterative Clustering* (SLIC) |
| 21 |
algorithm, an image segmentation method described in @cite{achanta10slic}. |
| 22 |
|
| 23 |
- @ref slic-overview |
| 24 |
- @ref slic-usage |
| 25 |
- @ref slic-tech |
| 26 |
|
| 27 |
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --> |
| 28 |
@section slic-overview Overview |
| 29 |
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --> |
| 30 |
|
| 31 |
SLIC @cite{achanta10slic} is a simple and efficient method to decompose |
| 32 |
an image in visually homogeneous regions. It is based on a spatially |
| 33 |
localized version of k-means clustering. Similar to mean shift or |
| 34 |
quick shift (@ref quickshift.h), each pixel is associated to a feature |
| 35 |
vector |
| 36 |
|
| 37 |
@f[ |
| 38 |
\Psi(x,y) = |
| 39 |
\left[ |
| 40 |
\begin{array}{c} |
| 41 |
\lambda x \\ |
| 42 |
\lambda y \\ |
| 43 |
I(x,y) |
| 44 |
\end{array} |
| 45 |
\right] |
| 46 |
@f] |
| 47 |
|
| 48 |
and then k-means clustering is run on those. As discussed below, the |
| 49 |
coefficient @f$ \lambda @f$ balances the spatial and appearance |
| 50 |
components of the feature vectors, imposing a degree of spatial |
| 51 |
regularization to the extracted regions. |
| 52 |
|
| 53 |
SLIC takes two parameters: the nominal size of the regions |
| 54 |
(superpixels) @c regionSize and the strength of the spatial |
| 55 |
regularization @c regularizer. The image is first divided into a grid |
| 56 |
with step @c regionSize. The center of each grid tile is then used to |
| 57 |
initialize a corresponding k-means (up to a small shift to avoid |
| 58 |
image edges). Finally, the k-means centers and clusters are refined by |
| 59 |
using the Lloyd algorithm, yielding segmenting the image. As a |
| 60 |
further restriction and simplification, during the k-means iterations |
| 61 |
each pixel can be assigned to only the <em>2 x 2</em> centers |
| 62 |
corresponding to grid tiles adjacent to the pixel. |
| 63 |
|
| 64 |
The parameter @c regularizer sets the trade-off between clustering |
| 65 |
appearance and spatial regularization. This is obtained by setting |
| 66 |
|
| 67 |
@f[ |
| 68 |
\lambda = \frac{\mathtt{regularizer}}{\mathtt{regionSize}} |
| 69 |
@f] |
| 70 |
|
| 71 |
in the definition of the feature @f$ \psi(x,y) @f$. |
| 72 |
|
| 73 |
After the k-means step, SLIC optionally |
| 74 |
removes any segment whose area is smaller than a threshld @c minRegionSize |
| 75 |
by merging them into larger ones. |
| 76 |
|
| 77 |
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --> |
| 78 |
@section slic-usage Usage from the C library |
| 79 |
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --> |
| 80 |
|
| 81 |
To compute the SLIC superpixels of an image use the function |
| 82 |
::vl_slic_segment. |
| 83 |
|
| 84 |
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --> |
| 85 |
@section slic-tech Technical details |
| 86 |
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --> |
| 87 |
|
| 88 |
SLIC starts by dividing the image domain into a regular grid with @f$ |
| 89 |
M \times N @f$ tiles, where |
| 90 |
|
| 91 |
@f[ |
| 92 |
M = \lceil \frac{\mathtt{imageWidth}}{\mathtt{regionSize}} \rceil, |
| 93 |
\quad |
| 94 |
N = \lceil \frac{\mathtt{imageHeight}}{\mathtt{regionSize}} \rceil. |
| 95 |
@f] |
| 96 |
|
| 97 |
A region (superpixel or k-means cluster) is initialized from each grid |
| 98 |
center |
| 99 |
|
| 100 |
@f[ |
| 101 |
x_i = \operatorname{round} i \frac{\mathtt{imageWidth}}{\mathtt{regionSize}} |
| 102 |
\quad |
| 103 |
y_j = \operatorname{round} j \frac{\mathtt{imageWidth}}{\mathtt{regionSize}}. |
| 104 |
@f] |
| 105 |
|
| 106 |
In order to avoid placing these centers on top of image |
| 107 |
discontinuities, the centers are then moved in a 3 x 3 |
| 108 |
neighbourohood to minimize the edge strength |
| 109 |
|
| 110 |
@f[ |
| 111 |
\operatorname{edge}(x,y) = |
| 112 |
\| I(x+1,y) - I(x-1,y) \|_2^2 + |
| 113 |
\| I(x,y+1) - I(x,y-1) \|_2^2. |
| 114 |
@f] |
| 115 |
|
| 116 |
Then the regions are obtained by running k-means clustering, started |
| 117 |
from the centers |
| 118 |
|
| 119 |
@f[ |
| 120 |
C = \{ \Psi(x_i,y_j), i=0,1,\dots,M-1\ j=0,1,\dots,N-1 \} |
| 121 |
@f] |
| 122 |
|
| 123 |
thus obtained. K-means uses the standard LLoyd algorithm alternating |
| 124 |
assigning pixels to the clostest centers a re-estiamting the centers |
| 125 |
as the average of the corresponding feature vectors of the pixel |
| 126 |
assigned to them. The only difference compared to standard k-means is |
| 127 |
that each pixel can be assigned only to the center originated from the |
| 128 |
neighbour tiles. This guarantees that there are exactly four |
| 129 |
pixel-to-center comparisons at each round of minimization, which |
| 130 |
threfore cost @f$ O(n) @f$, where @f$ n @f$ is the number of |
| 131 |
superpixels. |
| 132 |
|
| 133 |
After k-means has converged, SLIC eliminates any connected region whose |
| 134 |
area is less than @c minRegionSize pixels. This is done by greedily |
| 135 |
merging regions to neighbour ones: the pixels @f$ p @f$ are scanned in |
| 136 |
lexicographical order and the corresponding connected components |
| 137 |
are visited. If a region has already been visited, it is skipped; if not, |
| 138 |
its area is computed and if this is less than @c minRegionSize its label |
| 139 |
is changed to the one of a neighbour |
| 140 |
region at @f$ p @f$ that has already been vistied (there is always one |
| 141 |
except for the very first pixel). |
| 142 |
|
| 143 |
*/ |
| 144 |
|
| 145 |
#include "slic.h" |
| 146 |
#include "mathop.h" |
| 147 |
#include <math.h> |
| 148 |
#include <string.h> |
| 149 |
|
| 150 |
/** @brief SLIC superpixel segmentation |
| 151 |
** @param segmentation segmentation. |
| 152 |
** @param image image to segment. |
| 153 |
** @param width image width. |
| 154 |
** @param height image height. |
| 155 |
** @param numChannels number of image channels (depth). |
| 156 |
** @param regionSize nominal size of the regions. |
| 157 |
** @param regularization trade-off between appearance and spatial terms. |
| 158 |
** @param minRegionSize minimum size of a segment. |
| 159 |
** |
| 160 |
** The function computes the SLIC superpixels of the specified image @a image. |
| 161 |
** @a image is a pointer to an @c width by @c height by @c by numChannles array of @c float. |
| 162 |
** @a segmentation is a pointer to a @c width by @c height array of @c vl_uint32. |
| 163 |
** @a segmentation contain the labels of each image pixels, from 0 to |
| 164 |
** the number of regions minus one. |
| 165 |
** |
| 166 |
** @sa @ref slic-overview, @ref slic-tech |
| 167 |
**/ |
| 168 |
|
| 169 |
void |
| 170 |
vl_slic_segment (vl_uint32 * segmentation, |
| 171 |
float const * image, |
| 172 |
vl_size width, |
| 173 |
vl_size height, |
| 174 |
vl_size numChannels, |
| 175 |
vl_size regionSize, |
| 176 |
float regularization, |
| 177 |
vl_size minRegionSize) |
| 178 |
{ |
| 179 |
vl_index i, x, y, u, v, k, region ; |
| 180 |
vl_uindex iter ; |
| 181 |
vl_size const numRegionsX = (vl_size) ceil((double) width / regionSize) ; |
| 182 |
vl_size const numRegionsY = (vl_size) ceil((double) height / regionSize) ; |
| 183 |
vl_size const numRegions = numRegionsX * numRegionsY ; |
| 184 |
vl_size const numPixels = width * height ; |
| 185 |
float * centers ; |
| 186 |
float * edgeMap ; |
| 187 |
float previousEnergy = VL_INFINITY_F ; |
| 188 |
float startingEnergy ; |
| 189 |
vl_uint32 * masses ; |
| 190 |
vl_size const maxNumIterations = 100 ; |
| 191 |
|
| 192 |
assert(segmentation) ; |
| 193 |
assert(image) ; |
| 194 |
assert(width >= 1) ; |
| 195 |
assert(height >= 1) ; |
| 196 |
assert(numChannels >= 1) ; |
| 197 |
assert(regionSize >= 1) ; |
| 198 |
assert(regularization >= 0) ; |
| 199 |
|
| 200 |
#define atimage(x,y,k) image[(x)+(y)*width+(k)*width*height] |
| 201 |
#define atEdgeMap(x,y) edgeMap[(x)+(y)*width] |
| 202 |
|
| 203 |
edgeMap = vl_calloc(numPixels, sizeof(float)) ; |
| 204 |
masses = vl_malloc(sizeof(vl_uint32) * numPixels) ; |
| 205 |
centers = vl_malloc(sizeof(float) * (2 + numChannels) * numRegions) ; |
| 206 |
|
| 207 |
/* compute edge map (gradient strength) */ |
| 208 |
for (k = 0 ; k < (signed)numChannels ; ++k) { |
| 209 |
for (y = 1 ; y < (signed)height-1 ; ++y) { |
| 210 |
for (x = 1 ; x < (signed)width-1 ; ++x) { |
| 211 |
float a = atimage(x-1,y,k) ; |
| 212 |
float b = atimage(x+1,y,k) ; |
| 213 |
float c = atimage(x,y+1,k) ; |
| 214 |
float d = atimage(x,y-1,k) ; |
| 215 |
atEdgeMap(x,y) += (a - b) * (a - b) + (c - d) * (c - d) ; |
| 216 |
} |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
/* initialize K-means centers */ |
| 221 |
i = 0 ; |
| 222 |
for (v = 0 ; v < (signed)numRegionsY ; ++v) { |
| 223 |
for (u = 0 ; u < (signed)numRegionsX ; ++u) { |
| 224 |
vl_index xp ; |
| 225 |
vl_index yp ; |
| 226 |
vl_index centerx = 0 ; |
| 227 |
vl_index centery = 0 ; |
| 228 |
float minEdgeValue = VL_INFINITY_F ; |
| 229 |
|
| 230 |
x = (vl_index) vl_round_d(regionSize * (u + 0.5)) ; |
| 231 |
y = (vl_index) vl_round_d(regionSize * (v + 0.5)) ; |
| 232 |
|
| 233 |
x = VL_MAX(VL_MIN(x, (signed)width-1),0) ; |
| 234 |
y = VL_MAX(VL_MIN(y, (signed)height-1),0) ; |
| 235 |
|
| 236 |
/* search in a 3x3 neighbourhood the smallest edge response */ |
| 237 |
for (yp = VL_MAX(0, y-1) ; yp <= VL_MIN((signed)height-1, y+1) ; ++ yp) { |
| 238 |
for (xp = VL_MAX(0, x-1) ; xp <= VL_MIN((signed)width-1, x+1) ; ++ xp) { |
| 239 |
float thisEdgeValue = atEdgeMap(xp,yp) ; |
| 240 |
if (thisEdgeValue < minEdgeValue) { |
| 241 |
minEdgeValue = thisEdgeValue ; |
| 242 |
centerx = xp ; |
| 243 |
centery = yp ; |
| 244 |
} |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
/* initialize the new center at this location */ |
| 249 |
centers[i++] = (float) centerx ; |
| 250 |
centers[i++] = (float) centery ; |
| 251 |
for (k = 0 ; k < (signed)numChannels ; ++k) { |
| 252 |
centers[i++] = atimage(centerx,centery,k) ; |
| 253 |
} |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
/* run k-means iterations */ |
| 258 |
for (iter = 0 ; iter < maxNumIterations ; ++iter) { |
| 259 |
float factor = regularization / (regionSize * regionSize) ; |
| 260 |
float energy = 0 ; |
| 261 |
|
| 262 |
/* assign pixels to centers */ |
| 263 |
for (y = 0 ; y < (signed)height ; ++y) { |
| 264 |
for (x = 0 ; x < (signed)width ; ++x) { |
| 265 |
vl_index u = floor((double)x / regionSize - 0.5) ; |
| 266 |
vl_index v = floor((double)y / regionSize - 0.5) ; |
| 267 |
vl_index up, vp ; |
| 268 |
float minDistance = VL_INFINITY_F ; |
| 269 |
|
| 270 |
for (vp = VL_MAX(0, v) ; vp <= VL_MIN((signed)numRegionsY-1, v+1) ; ++vp) { |
| 271 |
for (up = VL_MAX(0, u) ; up <= VL_MIN((signed)numRegionsX-1, u+1) ; ++up) { |
| 272 |
vl_index region = up + vp * numRegionsX ; |
| 273 |
float centerx = centers[(2 + numChannels) * region + 0] ; |
| 274 |
float centery = centers[(2 + numChannels) * region + 1] ; |
| 275 |
float spatial = (x - centerx) * (x - centerx) + (y - centery) * (y - centery) ; |
| 276 |
float appearance = 0 ; |
| 277 |
float distance ; |
| 278 |
for (k = 0 ; k < (signed)numChannels ; ++k) { |
| 279 |
float centerz = centers[(2 + numChannels) * region + k + 2] ; |
| 280 |
float z = atimage(x,y,k) ; |
| 281 |
appearance += (z - centerz) * (z - centerz) ; |
| 282 |
} |
| 283 |
distance = appearance + factor * spatial ; |
| 284 |
if (minDistance > distance) { |
| 285 |
minDistance = distance ; |
| 286 |
segmentation[x + y * width] = (vl_uint32)region ; |
| 287 |
} |
| 288 |
} |
| 289 |
} |
| 290 |
energy += minDistance ; |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
/* |
| 295 |
VL_PRINTF("vl:slic: iter %d: energy: %g\n", iter, energy) ; |
| 296 |
*/ |
| 297 |
|
| 298 |
/* check energy termination conditions */ |
| 299 |
if (iter == 0) { |
| 300 |
startingEnergy = energy ; |
| 301 |
} else { |
| 302 |
if ((previousEnergy - energy) < 1e-5 * (startingEnergy - energy)) { |
| 303 |
break ; |
| 304 |
} |
| 305 |
} |
| 306 |
previousEnergy = energy ; |
| 307 |
|
| 308 |
/* recompute centers */ |
| 309 |
memset(masses, 0, sizeof(vl_uint32) * width * height) ; |
| 310 |
memset(centers, 0, sizeof(float) * (2 + numChannels) * numRegions) ; |
| 311 |
|
| 312 |
for (y = 0 ; y < (signed)height ; ++y) { |
| 313 |
for (x = 0 ; x < (signed)width ; ++x) { |
| 314 |
vl_index pixel = x + y * width ; |
| 315 |
vl_index region = segmentation[pixel] ; |
| 316 |
masses[region] ++ ; |
| 317 |
centers[region * (2 + numChannels) + 0] += x ; |
| 318 |
centers[region * (2 + numChannels) + 1] += y ; |
| 319 |
for (k = 0 ; k < (signed)numChannels ; ++k) { |
| 320 |
centers[region * (2 + numChannels) + k + 2] += atimage(x,y,k) ; |
| 321 |
} |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
for (region = 0 ; region < (signed)numRegions ; ++region) { |
| 326 |
float mass = VL_MAX(masses[region], 1e-8) ; |
| 327 |
for (i = (2 + numChannels) * region ; |
| 328 |
i < (signed)(2 + numChannels) * (region + 1) ; |
| 329 |
++i) { |
| 330 |
centers[i] /= mass ; |
| 331 |
} |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
vl_free(masses) ; |
| 336 |
vl_free(centers) ; |
| 337 |
vl_free(edgeMap) ; |
| 338 |
|
| 339 |
/* elimiate small regions */ |
| 340 |
{ |
| 341 |
vl_uint32 * cleaned = vl_calloc(numPixels, sizeof(vl_uint32)) ; |
| 342 |
vl_uindex * segment = vl_malloc(sizeof(vl_uindex) * numPixels) ; |
| 343 |
vl_size segmentSize ; |
| 344 |
vl_uint32 label ; |
| 345 |
vl_uint32 cleanedLabel ; |
| 346 |
vl_size numExpanded ; |
| 347 |
vl_index const dx [] = {+1, -1, 0, 0} ; |
| 348 |
vl_index const dy [] = { 0, 0, +1, -1} ; |
| 349 |
vl_index direction ; |
| 350 |
vl_index pixel ; |
| 351 |
|
| 352 |
for (pixel = 0 ; pixel < (signed)numPixels ; ++pixel) { |
| 353 |
if (cleaned[pixel]) continue ; |
| 354 |
label = segmentation[pixel] ; |
| 355 |
numExpanded = 0 ; |
| 356 |
segmentSize = 0 ; |
| 357 |
segment[segmentSize++] = pixel ; |
| 358 |
|
| 359 |
/* |
| 360 |
find cleanedLabel as the label of an already cleaned |
| 361 |
region neihbour of this pixel |
| 362 |
*/ |
| 363 |
cleanedLabel = label + 1 ; |
| 364 |
cleaned[pixel] = label + 1 ; |
| 365 |
x = pixel % width ; |
| 366 |
y = pixel / width ; |
| 367 |
for (direction = 0 ; direction < 4 ; ++direction) { |
| 368 |
vl_index xp = x + dx[direction] ; |
| 369 |
vl_index yp = y + dy[direction] ; |
| 370 |
vl_index neighbor = xp + yp * width ; |
| 371 |
if (0 <= xp && xp < (signed)width && |
| 372 |
0 <= yp && yp < (signed)height && |
| 373 |
cleaned[neighbor]) { |
| 374 |
cleanedLabel = cleaned[neighbor] ; |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
/* expand the segment */ |
| 379 |
while (numExpanded < segmentSize) { |
| 380 |
vl_index open = segment[numExpanded++] ; |
| 381 |
x = open % width ; |
| 382 |
y = open / width ; |
| 383 |
for (direction = 0 ; direction < 4 ; ++direction) { |
| 384 |
vl_index xp = x + dx[direction] ; |
| 385 |
vl_index yp = y + dy[direction] ; |
| 386 |
vl_index neighbor = xp + yp * width ; |
| 387 |
if (0 <= xp && xp < (signed)width && |
| 388 |
0 <= yp && yp < (signed)height && |
| 389 |
cleaned[neighbor] == 0 && |
| 390 |
segmentation[neighbor] == label) { |
| 391 |
cleaned[neighbor] = label + 1 ; |
| 392 |
segment[segmentSize++] = neighbor ; |
| 393 |
} |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
/* change label to cleanedLabel if the semgent is too small */ |
| 398 |
if (segmentSize < minRegionSize) { |
| 399 |
while (segmentSize > 0) { |
| 400 |
cleaned[segment[--segmentSize]] = cleanedLabel ; |
| 401 |
} |
| 402 |
} |
| 403 |
} |
| 404 |
/* restore base 0 indexing of the regions */ |
| 405 |
for (pixel = 0 ; pixel < (signed)numPixels ; ++pixel) cleaned[pixel] -- ; |
| 406 |
|
| 407 |
memcpy(segmentation, cleaned, numPixels * sizeof(vl_uint32)) ; |
| 408 |
vl_free(cleaned) ; |
| 409 |
vl_free(segment) ; |
| 410 |
} |
| 411 |
} |