This file is indexed.

/usr/include/vigra/slic.hxx is in libvigraimpex-dev 1.10.0+dfsg-11ubuntu2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/************************************************************************/
/*                                                                      */
/*    Copyright 2012-2013 by Ullrich Koethe and Thorsten Beier          */
/*                                                                      */
/*    This file is part of the VIGRA computer vision library.           */
/*    The VIGRA Website is                                              */
/*        http://hci.iwr.uni-heidelberg.de/vigra/                       */
/*    Please direct questions, bug reports, and contributions to        */
/*        ullrich.koethe@iwr.uni-heidelberg.de    or                    */
/*        vigra@informatik.uni-hamburg.de                               */
/*                                                                      */
/*    Permission is hereby granted, free of charge, to any person       */
/*    obtaining a copy of this software and associated documentation    */
/*    files (the "Software"), to deal in the Software without           */
/*    restriction, including without limitation the rights to use,      */
/*    copy, modify, merge, publish, distribute, sublicense, and/or      */
/*    sell copies of the Software, and to permit persons to whom the    */
/*    Software is furnished to do so, subject to the following          */
/*    conditions:                                                       */
/*                                                                      */
/*    The above copyright notice and this permission notice shall be    */
/*    included in all copies or substantial portions of the             */
/*    Software.                                                         */
/*                                                                      */
/*    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND    */
/*    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES   */
/*    OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND          */
/*    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT       */
/*    HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,      */
/*    WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING      */
/*    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR     */
/*    OTHER DEALINGS IN THE SOFTWARE.                                   */
/*                                                                      */
/************************************************************************/

#ifndef VIGRA_SLIC_HXX
#define VIGRA_SLIC_HXX

#include "multi_array.hxx"
#include "multi_convolution.hxx"
#include "multi_labeling.hxx"
#include "numerictraits.hxx"
#include "accumulator.hxx"
#include "array_vector.hxx"

namespace vigra {

/** \addtogroup SeededRegionGrowing
*/
//@{

/********************************************************/
/*                                                      */
/*                  generateSlicSeeds                   */
/*                                                      */
/********************************************************/

/** \brief Generate seeds for SLIC superpixel computation in arbitrary dimensions.

    The source array \a src must be a scalar boundary indicator such as the gradient 
    magnitude. Seeds are initially placed on a regular Cartesian grid with spacing
    \a seedDist und then moved to the point with smallest boundary indicator within
    a search region of radius \a searchRadius around the initial position. The resulting
    points are then marked in the output array \a seeds by consecutive labels.
    
    The function returns the number of selected seeds, which equals the largest seed label 
    because labeling starts at 1.

    <b> Declaration:</b>

    use arbitrary-dimensional arrays:
    \code
    namespace vigra {
        template <unsigned int N, class T, class S1,
                                  class Label, class S2>
        unsigned int 
        generateSlicSeeds(MultiArrayView<N, T, S1> const & src,
                          MultiArrayView<N, Label, S2>     seeds,
                          unsigned int                     seedDist,
                          unsigned int                     searchRadius = 1);
    }
    \endcode

    <b> Usage:</b>

    <b>\#include</b> \<vigra/slic.hxx\><br>
    Namespace: vigra

    \code
    MultiArray<2, RGBValue<float> > src(Shape2(w, h));
    ... // fill src image
    
    // transform image to Lab color space
    transformImage(srcImageRange(src), destImage(src), RGBPrime2LabFunctor<float>());
    
    // compute image gradient magnitude at scale 1.0 as a boundary indicator
    MultiArray<2, float> grad(src.shape());
    gaussianGradientMagnitude(srcImageRange(src), destImage(grad), 1.0);
    
    MultiArray<2, unsigned int>  seeds(src.shape());
    int seedDistance = 15;
    
    // place seeds on a grid with distance 15, but then move it to the lowest gradient
    // poistion in a 3x3 window
    generateSlicSeeds(grad, seeds, seedDistance);
    \endcode

    For more details and examples see slicSuperpixels().
*/
doxygen_overloaded_function(template <...> unsigned int generateSlicSeeds)

template <unsigned int N, class T, class S1,
                          class Label, class S2>
unsigned int 
generateSlicSeeds(MultiArrayView<N, T, S1> const & boundaryIndicatorImage,
                  MultiArrayView<N, Label, S2>     seeds,
                  unsigned int                     seedDist,
                  unsigned int                     searchRadius = 1)
{
    typedef typename MultiArrayShape<N>::type   Shape;

    seeds.init(0);
    Shape shape(boundaryIndicatorImage.shape()),
          seedShape(floor(shape / double(seedDist))),
          offset((shape - (seedShape - Shape(1))*seedDist) / 2);
    
    unsigned int label = 0;
    MultiCoordinateIterator<N> iter(seedShape),
                               end = iter.getEndIterator();
    for(; iter != end; ++iter)
    {
        // define search window around current seed center
        Shape center = (*iter)*seedDist + offset;
        Shape startCoord = max(Shape(0), center-Shape(searchRadius));
        Shape endCoord   = min(center+Shape(searchRadius+1), shape);
        
        // find the coordinate of minimum boundary indicator in window
        using namespace acc;
        AccumulatorChain<CoupledArrays<N, T>,
                         Select<WeightArg<1>, Coord<ArgMinWeight> > > a;
        extractFeatures(boundaryIndicatorImage.subarray(startCoord, endCoord), a);

        // add seed at minimum position, if not already occupied
        Shape minCoord = get<Coord<ArgMinWeight> >(a) + startCoord;
        if(seeds[minCoord] == 0)
            seeds[minCoord] = ++label;
    }
    return label;
}

/** \brief Options object for slicSuperpixels().

    <b> Usage:</b>

    see slicSuperpixels() for detailed examples.
*/
struct SlicOptions
{
        /** \brief Create options object with default settings.

            Defaults are: perform 10 iterations, determine a size limit for superpixels automatically.
        */
    SlicOptions()
    : iter(10),
      sizeLimit(0)
    {}
    
        /** \brief Number of iterations.

            Default: 10
        */
    SlicOptions & iterations(unsigned int i)
    {
        iter = i;
        return *this;
    }
    
        /** \brief Minimum superpixel size.
        
            If you set this to 1, no size filtering will be performed.

            Default: 0 (determine size limit automatically as <tt>average size / 4</tt>)
        */
    SlicOptions & minSize(unsigned int s)
    {
        sizeLimit = s;
        return *this;
    }
    
    unsigned int iter;
    unsigned int sizeLimit;
};

namespace detail {

template <unsigned int N, class T, class Label>
class Slic
{
  public: 
    // 
    typedef MultiArrayView<N, T>                    DataImageType;
    typedef MultiArrayView<N, Label>                LabelImageType; 
    typedef typename DataImageType::difference_type ShapeType;
    typedef typename PromoteTraits<
                   typename NormTraits<T>::NormType,
                   typename NormTraits<MultiArrayIndex>::NormType
             >::Promote                             DistanceType;

    Slic(DataImageType dataImage, 
         LabelImageType labelImage, 
         DistanceType intensityScaling, 
         int maxRadius, 
         SlicOptions const & options = SlicOptions());
    
    unsigned int execute();

  private:
    void updateAssigments();
    unsigned int postProcessing();
    
    typedef MultiArray<N,DistanceType>  DistanceImageType;

    ShapeType                       shape_;
    DataImageType                   dataImage_;
    LabelImageType                  labelImage_;
    DistanceImageType               distance_;
    int                             max_radius_;
    DistanceType                    normalization_;
    SlicOptions                     options_;
    
    typedef acc::Select<acc::DataArg<1>, acc::LabelArg<2>, acc::Mean, acc::RegionCenter> Statistics;
    typedef acc::AccumulatorChainArray<CoupledArrays<N, T, Label>, Statistics> RegionFeatures;
    RegionFeatures clusters_;
};



template <unsigned int N, class T, class Label>
Slic<N, T, Label>::Slic(
    DataImageType         dataImage, 
    LabelImageType        labelImage,
    DistanceType          intensityScaling,
    int                   maxRadius,
    SlicOptions const &   options)
:   shape_(dataImage.shape()),
    dataImage_(dataImage),
    labelImage_(labelImage),
    distance_(shape_),
    max_radius_(maxRadius),
    normalization_(sq(intensityScaling) / sq(max_radius_)),
    options_(options)
{
    clusters_.ignoreLabel(0);
}

template <unsigned int N, class T, class Label>
unsigned int Slic<N, T, Label>::execute()
{
    // Do SLIC
    for(size_t i=0; i<options_.iter; ++i)
    {
        // update mean for each cluster
        clusters_.reset();
        extractFeatures(dataImage_, labelImage_, clusters_);
        
        // update which pixels get assigned to which cluster
        updateAssigments();
    }

    return postProcessing();
}

template <unsigned int N, class T, class Label>
void
Slic<N, T, Label>::updateAssigments()
{
    using namespace acc;
    distance_.init(NumericTraits<DistanceType>::max());
    for(unsigned int c=1; c<=clusters_.maxRegionLabel(); ++c)
    {
        if(get<Count>(clusters_, c) == 0) // label doesn't exist
            continue;
            
        typedef typename LookupTag<RegionCenter, RegionFeatures>::value_type CenterType;
        CenterType center = get<RegionCenter>(clusters_, c);

        // get ROI limits around region center
        ShapeType pixelCenter(round(center)), 
                  startCoord(max(ShapeType(0), pixelCenter - ShapeType(max_radius_))), 
                  endCoord(min(shape_, pixelCenter + ShapeType(max_radius_+1)));
        center -= startCoord; // need center relative to ROI
        
        // setup iterators for ROI
        typedef typename CoupledArrays<N, T, Label, DistanceType>::IteratorType Iterator;
        Iterator iter = createCoupledIterator(dataImage_, labelImage_, distance_).
                            restrictToSubarray(startCoord, endCoord),
                 end = iter.getEndIterator();
        
        // only pixels within the ROI can be assigned to a cluster
        for(; iter != end; ++iter)
        {
            // compute distance between cluster center and pixel
            DistanceType spatialDist   = squaredNorm(center-iter.point());
            DistanceType colorDist     = squaredNorm(get<Mean>(clusters_, c)-iter.template get<1>());
            DistanceType dist =  colorDist + normalization_*spatialDist;
            // update label?
            if(dist < iter.template get<3>())
            {
                iter.template get<2>() = static_cast<Label>(c);
                iter.template get<3>() = dist;
            }
        }
    }
}

template <unsigned int N, class T, class Label>
unsigned int 
Slic<N, T, Label>::postProcessing()
{
    // get rid of regions below a size limit
    MultiArray<N,Label> tmpLabelImage(labelImage_);
    unsigned int maxLabel = labelMultiArray(tmpLabelImage, labelImage_, DirectNeighborhood);
    
    unsigned int sizeLimit = options_.sizeLimit == 0
                                 ? (unsigned int)(0.25 * labelImage_.size() / maxLabel)
                                 : options_.sizeLimit;
    if(sizeLimit == 1)
        return maxLabel;
        
    // determine region size
    using namespace acc;
    AccumulatorChainArray<CoupledArrays<N, Label>, Select<LabelArg<1>, Count> > sizes;
    extractFeatures(labelImage_, sizes);
        
    typedef GridGraph<N, undirected_tag> Graph;
    Graph graph(labelImage_.shape(), DirectNeighborhood);
    
    typedef typename Graph::NodeIt        graph_scanner;
    typedef typename Graph::OutBackArcIt  neighbor_iterator;

    ArrayVector<Label> regions(maxLabel+1);

    // make sure that all regions exceed the sizeLimit
    for (graph_scanner node(graph); node != lemon::INVALID; ++node) 
    {
        Label label = labelImage_[*node];
        
        if(regions[label] > 0)
            continue;   // already processed
            
        regions[label] = label;
        
        if(get<Count>(sizes, label) < sizeLimit)
        {
            // region is too small => merge into an existing neighbor
            for (neighbor_iterator arc(graph, node); arc != lemon::INVALID; ++arc)
            {
                regions[label] = regions[labelImage_[graph.target(*arc)]];
                break;
            }
        }
    }
    
    // make labels contiguous after possible merging
    maxLabel = 0; 
    for(unsigned int i=1; i<=maxLabel; ++i)
    {
        if(regions[i] == i)
        {
                regions[i] = (Label)++maxLabel;
        }
        else
        {
                regions[i] = regions[regions[i]]; 
        }
    }

    // update labels
    for (graph_scanner node(graph); node != lemon::INVALID; ++node) 
    {
        labelImage_[*node] = regions[labelImage_[*node]];
    }
    
    return maxLabel;
}

} // namespace detail


/** \brief Compute SLIC superpixels in arbitrary dimensions.

    This function implements the algorithm described in 
    
    R. Achanta et al.: <em>"SLIC Superpixels Compared to State-of-the-Art 
    Superpixel Methods"</em>, IEEE Trans. Patt. Analysis Mach. Intell. 34(11):2274-2281, 2012
    
    The value type <tt>T</tt> of the source array \a src must provide the necessary functionality 
    to compute averages and squared distances (i.e. it must fulfill the requirements of a 
    \ref LinearSpace and support squaredNorm(T const &)). This is true for all scalar types as well as
    \ref vigra::TinyVector and \ref vigra::RGBValue. The output array \a labels will be filled
    with labels designating membership of each point in one of the superpixel regions.
    
    The output array can optionally contain seeds (which will be overwritten by the output) 
    to give you full control over seed placement. If \a labels is empty, seeds will be created
    automatically by an internal call to generateSlicSeeds(). 
    
    The parameter \a seedDistance specifies the radius of the window around each seed (or, more
    precisely, around the present regions centers) where the algorithm looks for potential members 
    of the corresponding superpixel. It thus places an upper limit on the superpixel size. When seeds
    are computed automatically, this parameter also determines the grid spacing for seed placement.
    
    The parameter \a intensityScaling is used to normalize (i.e. divide) the color/intensity difference 
    before it is compared with the spatial distance. This corresponds to parameter <i>m</i> in equation
    (2) of the paper.
    
    The options object can be used to specify the number of iterations (<tt>SlicOptions::iterations()</tt>)
    and an explicit minimal superpixel size (<tt>SlicOptions::minSize()</tt>). By default, the algorithm 
    merges all regions that are smaller than 1/4 of the average superpixel size.
    
    The function returns the number of superpixels, which equals the largest label 
    because labeling starts at 1.

    <b> Declaration:</b>

    use arbitrary-dimensional arrays:
    \code
    namespace vigra {
        template <unsigned int N, class T, class S1,
                                  class Label, class S2,
                  class DistanceType>
        unsigned int 
        slicSuperpixels(MultiArrayView<N, T, S1> const &  src,
                        MultiArrayView<N, Label, S2>      labels,
                        DistanceType                      intensityScaling,
                        unsigned int                      seedDistance, 
                        SlicOptions const &               options = SlicOptions());
    }
    \endcode

    <b> Usage:</b>

    <b>\#include</b> \<vigra/slic.hxx\><br>
    Namespace: vigra

    \code
    MultiArray<2, RGBValue<float> > src(Shape2(w, h));
    ... // fill src image
    
    // transform image to Lab color space
    transformMultiArray(srcMultiArrayRange(src), destMultiArray(src), RGBPrime2LabFunctor<float>());    
    
    MultiArray<2, unsigned int>  labels(src.shape());
    int seedDistance = 15;
    double intensityScaling = 20.0;
    
    // compute seeds automatically, perform 40 iterations, and scale intensity differences
    // down to 1/20 before comparing with spatial distances
    slicSuperpixels(src, labels, intensityScaling, seedDistance, SlicOptions().iterations(40));
    \endcode
    
    This works for arbitrary-dimensional arrays.
*/
doxygen_overloaded_function(template <...> unsigned int slicSuperpixels)

template <unsigned int N, class T, class S1,
                          class Label, class S2,
          class DistanceType>
unsigned int 
slicSuperpixels(MultiArrayView<N, T, S1> const &  src,
                MultiArrayView<N, Label, S2>      labels,
                DistanceType                      intensityScaling,
                unsigned int                      seedDistance, 
                SlicOptions const &               options = SlicOptions())
{
    if(!labels.any())
    {
        typedef typename NormTraits<T>::NormType TmpType;
        MultiArray<N, TmpType> grad(src.shape());
        gaussianGradientMagnitude(src, grad, 1.0);
        generateSlicSeeds(grad, labels, seedDistance);
    }
    return detail::Slic<N, T, Label>(src, labels, intensityScaling, seedDistance, options).execute();
}

//@}

} // namespace vigra

#endif // VIGRA_SLIC_HXX