This file is indexed.

/usr/include/vigra/multi_watersheds.hxx is in libvigraimpex-dev 1.10.0+dfsg-3ubuntu2.

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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
/************************************************************************/
/*                                                                      */
/*               Copyright 1998-2013 by Ullrich Koethe                  */
/*                                                                      */
/*    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_MULTI_WATERSHEDS_HXX
#define VIGRA_MULTI_WATERSHEDS_HXX

#include <functional>
#include "mathutil.hxx"
#include "multi_array.hxx"
#include "multi_math.hxx"
#include "multi_gridgraph.hxx"
#include "multi_localminmax.hxx"
#include "multi_labeling.hxx"
#include "watersheds.hxx"
#include "bucket_queue.hxx"
#include "union_find.hxx"

namespace vigra {

/** \addtogroup SeededRegionGrowing
*/
//@{
namespace lemon_graph {

namespace graph_detail {

template <class Graph, class T1Map, class T2Map>
void
prepareWatersheds(Graph const & g, 
                  T1Map const & data,
                  T2Map & lowestNeighborIndex)
{
    typedef typename Graph::NodeIt    graph_scanner;
    typedef typename Graph::OutArcIt  neighbor_iterator;

    for (graph_scanner node(g); node != INVALID; ++node) 
    {
        typename T1Map::value_type lowestValue  = data[*node];
        typename T2Map::value_type lowestIndex  = -1;

        for(neighbor_iterator arc(g, node); arc != INVALID; ++arc) 
        {
            if(data[g.target(*arc)] <= lowestValue)
            {
                lowestValue = data[g.target(*arc)];
                lowestIndex = arc.neighborIndex();
            }
        }
        lowestNeighborIndex[*node] = lowestIndex;
    }
}

template <class Graph, class T1Map, class T2Map, class T3Map>
typename T2Map::value_type
unionFindWatersheds(Graph const & g,
                    T1Map const & data, 
                    T2Map const & lowestNeighborIndex,
                    T3Map & labels)
{
    typedef typename Graph::NodeIt        graph_scanner;
    typedef typename Graph::OutBackArcIt  neighbor_iterator;
    typedef typename T3Map::value_type    LabelType;

    vigra::detail::UnionFindArray<LabelType>  regions;

    // pass 1: find connected components
    for (graph_scanner node(g); node != INVALID; ++node) 
    {
        // define tentative label for current node
        LabelType currentLabel = regions.nextFreeLabel();
        bool hasPlateauNeighbor = false;
        
        for (neighbor_iterator arc(g, node); arc != INVALID; ++arc)
        {
            // merge regions if current target is center's lowest neighbor or vice versa
            if(lowestNeighborIndex[*node] == arc.neighborIndex() || 
               lowestNeighborIndex[g.target(*arc)] == g.oppositeIndex(arc.neighborIndex()))
            {
                if(data[*node] == data[g.target(*arc)])
                    hasPlateauNeighbor = true;
                LabelType neighborLabel = regions[labels[g.target(*arc)]];
                currentLabel = regions.makeUnion(neighborLabel, currentLabel);
            }
        }
        
        if(hasPlateauNeighbor)
        {
            // we are on a plateau => link all plateau points
            for (neighbor_iterator arc(g, node); arc != INVALID; ++arc)
            {
                if(data[*node] == data[g.target(*arc)])
                {
                    LabelType neighborLabel = regions[labels[g.target(*arc)]];
                    currentLabel = regions.makeUnion(neighborLabel, currentLabel);
                }
            }
        }
        
        // set label of current node
        labels[*node] = regions.finalizeLabel(currentLabel);
    }
    
    LabelType count = regions.makeContiguous();

    // pass 2: make component labels contiguous
    for (graph_scanner node(g); node != INVALID; ++node) 
    {
        labels[*node] = regions[labels[*node]];
    }
    return count;
}

template <class Graph, class T1Map, class T2Map>
typename T2Map::value_type
generateWatershedSeeds(Graph const & g, 
                       T1Map const & data,
                       T2Map & seeds,
                       SeedOptions const & options = SeedOptions())
{
    typedef typename T1Map::value_type DataType;
    typedef unsigned char MarkerType;
    
    typename Graph::template NodeMap<MarkerType>  minima(g);
    
    if(options.mini == SeedOptions::LevelSets)
    {
        vigra_precondition(options.thresholdIsValid<DataType>(),
            "generateWatershedSeeds(): SeedOptions.levelSets() must be specified with threshold.");
    
        using namespace multi_math;
        minima = data <= DataType(options.thresh);
    }
    else
    {
        DataType threshold = options.thresholdIsValid<DataType>()
                                ? options.thresh
                                : NumericTraits<DataType>::max();
        
        if(options.mini == SeedOptions::ExtendedMinima)
            extendedLocalMinMaxGraph(g, data, minima, MarkerType(1), threshold, 
                                     std::less<DataType>(), std::equal_to<DataType>(), true);
        else
            localMinMaxGraph(g, data, minima, MarkerType(1), threshold, 
                             std::less<DataType>(), true);
    }
    return labelGraphWithBackground(g, minima, seeds, MarkerType(0), std::equal_to<MarkerType>());
}


template <class Graph, class T1Map, class T2Map>
typename T2Map::value_type 
seededWatersheds(Graph const & g, 
                 T1Map const & data,
                 T2Map & labels,
                 WatershedOptions const & options)
{
    typedef typename Graph::Node        Node;
    typedef typename Graph::NodeIt      graph_scanner;
    typedef typename Graph::OutArcIt    neighbor_iterator;
    typedef typename T1Map::value_type  CostType;
    typedef typename T2Map::value_type  LabelType;

    PriorityQueue<Node, CostType, true> pqueue;
    
    bool keepContours = ((options.terminate & KeepContours) != 0);
    LabelType maxRegionLabel = 0;
    
    for (graph_scanner node(g); node != INVALID; ++node) 
    {
        LabelType label = labels[*node];
        if(label != 0)
        {
            if(maxRegionLabel < label)
                maxRegionLabel = label;
                
            for (neighbor_iterator arc(g, node); arc != INVALID; ++arc)
            {
                if(labels[g.target(*arc)] == 0)
                {
                    // register all seeds that have an unlabeled neighbor
                    if(label == options.biased_label)
                        pqueue.push(*node, data[*node] * options.bias);
                    else
                        pqueue.push(*node, data[*node]);
                    break;
                }
            }
        }
    }
    
    LabelType contourLabel = maxRegionLabel + 1;  // temporary contour label
    
    // perform region growing
    while(!pqueue.empty())
    {
        Node node = pqueue.top();
        CostType cost = pqueue.topPriority();
        pqueue.pop();
        
        if((options.terminate & StopAtThreshold) && (cost > options.max_cost))
            break;

        LabelType label = labels[node];
        
        if(label == contourLabel)
            continue;

        // Put the unlabeled neighbors in the priority queue.
        for (neighbor_iterator arc(g, node); arc != INVALID; ++arc)
        {
            LabelType neighborLabel = labels[g.target(*arc)];
            if(neighborLabel == 0)
            {
                labels[g.target(*arc)] = label;
                CostType priority = (label == options.biased_label)
                                       ? data[g.target(*arc)] * options.bias
                                       : data[g.target(*arc)];
                if(priority < cost)
                    priority = cost;
                pqueue.push(g.target(*arc), priority);
            }
            else if(keepContours && (label != neighborLabel) && (neighborLabel != contourLabel))
            {
                // The present neighbor is adjacent to more than one region
                // => mark it as contour.
                CostType priority = (neighborLabel == options.biased_label)
                                       ? data[g.target(*arc)] * options.bias
                                       : data[g.target(*arc)];
                if(cost < priority) // neighbor not yet processed
                    labels[g.target(*arc)] = contourLabel;
            }
        }
    }
    
    if(keepContours)
    {
        // Replace the temporary contour label with label 0.
        typename T2Map::iterator k   = labels.begin(),
                                 end = labels.end();
        for(; k != end; ++k)
            if(*k == contourLabel)
                *k = 0;
    }
    
    return maxRegionLabel;
}

} // namespace graph_detail

template <class Graph, class T1Map, class T2Map>
typename T2Map::value_type 
watershedsGraph(Graph const & g, 
                T1Map const & data,
                T2Map & labels,
                WatershedOptions const & options)
{
    if(options.method == WatershedOptions::UnionFind)
    {
        vigra_precondition(g.maxDegree() <= NumericTraits<unsigned short>::max(),
            "watershedsGraph(): cannot handle nodes with degree > 65535.");
            
        typename Graph::template NodeMap<unsigned short>  lowestNeighborIndex(g);
        
        graph_detail::prepareWatersheds(g, data, lowestNeighborIndex);
        return graph_detail::unionFindWatersheds(g, data, lowestNeighborIndex, labels);
    }
    else if(options.method == WatershedOptions::RegionGrowing)
    {
        SeedOptions seed_options;
        
        // check if the user has explicitly requested seed computation
        if(options.seed_options.mini != SeedOptions::Unspecified)
        {
            seed_options = options.seed_options;
        }
        else
        {
            // otherwise, don't compute seeds if 'labels' already contains them 
            if(labels.any())
                seed_options.mini = SeedOptions::Unspecified;
        }

        if(seed_options.mini != SeedOptions::Unspecified)
        {
            graph_detail::generateWatershedSeeds(g, data, labels, seed_options);
        }
        
        return graph_detail::seededWatersheds(g, data, labels, options);
    }
    else
    {
        vigra_precondition(false,
           "watershedsGraph(): invalid method in watershed options.");
        return 0;
    }
}


} // namespace lemon_graph

    // documentation is in watersheds.hxx
template <unsigned int N, class T, class S1,
                          class Label, class S2>
inline Label
generateWatershedSeeds(MultiArrayView<N, T, S1> const & data,
                       MultiArrayView<N, Label, S2> seeds,
                       NeighborhoodType neighborhood = IndirectNeighborhood,
                       SeedOptions const & options = SeedOptions())
{
    vigra_precondition(data.shape() == seeds.shape(),
        "generateWatershedSeeds(): Shape mismatch between input and output.");
    
    GridGraph<N, undirected_tag> graph(data.shape(), neighborhood);
    return lemon_graph::graph_detail::generateWatershedSeeds(graph, data, seeds, options);
}


/** \brief Watershed segmentation of an arbitrary-dimensional array.

    This function implements variants of the watershed algorithms
    described in

    [1] L. Vincent and P. Soille: <em>"Watersheds in digital spaces: An efficient algorithm
    based on immersion simulations"</em>, IEEE Trans. Patt. Analysis Mach. Intell. 13(6):583-598, 1991

    [2] J. Roerdink, R. Meijster: <em>"The watershed transform: definitions, algorithms,
    and parallelization strategies"</em>, Fundamenta Informaticae, 41:187-228, 2000

    The source array \a data is a boundary indicator such as the gaussianGradientMagnitude()
    or the trace of the \ref boundaryTensor(), and the destination \a labels is a label array
    designating membership of each point in one of the regions found. Plateaus in the boundary
    indicator are handled via simple tie breaking strategies. Argument \a neighborhood 
    specifies the connectivity between points and can be <tt>DirectNeighborhood</tt> (meaning 
    4-neighborhood in 2D and 6-neighborhood in 3D, default) or <tt>IndirectNeighborhood</tt> 
    (meaning 8-neighborhood in 2D and 26-neighborhood in 3D).
    
    The watershed variant to be applied can be selected in the \ref WatershedOptions
    object: When you call <tt>WatershedOptions::regionGrowing()</tt> (default), the flooding
    algorithm from [1] is used. Alternatively, <tt>WatershedOptions::unionFind()</tt> uses
    the scan-line algorithm 4.7 from [2]. The latter is faster, but does not support any options 
    (if you pass options nonetheless, they are silently ignored).
    
    The region growing algorithm needs a seed for each region. Seeds can either be provided in
    the destination array \a labels (which will then be overwritten with the result) or computed
    automatically by an internal call to generateWatershedSeeds(). In the former case you have 
    full control over seed placement, while the latter is more convenient. Automatic seed 
    computation is performed when you provide seeding options via <tt>WatershedOptions::seedOptions()</tt> 
    or when the array \a labels is empty (all zeros), in which case default seeding options 
    are chosen. The destination image should be zero-initialized for automatic seed computation.
    
    Further options to be specified via \ref WatershedOptions are:
    
    <ul>
    <li> <tt>keepContours()</tt>: Whether to keep a 1-pixel-wide contour (with label 0) between 
         regions (otherwise, a complete grow is performed, i.e. all pixels are assigned to a region).
    <li> <tt>stopAtThreshold()</tt>: Whether to stop growing when the boundaryness exceeds a threshold 
         (remaining pixels keep label 0).
    <li> <tt>biasLabel()</tt>: Whether one region (label) is to be preferred or discouraged by biasing its cost 
         with a given factor (smaller than 1 for preference, larger than 1 for discouragement).
    </ul>
    
    The option <tt>turboAlgorithm()</tt> is implied by method <tt>regionGrowing()</tt> (this is
    in contrast to watershedsRegionGrowing(), which supports an additional algorithm in 2D only).

    watershedsMultiArray() returns the number of regions found (= the highest region label, because 
    labels start at 1). 

    <b> Declaration:</b>

    \code
    namespace vigra {
        template <unsigned int N, class T, class S1,
                                  class Label, class S2>
        Label
        watershedsMultiArray(MultiArrayView<N, T, S1> const & data,
                             MultiArrayView<N, Label, S2> labels,  // may also hold input seeds
                             NeighborhoodType neighborhood = DirectNeighborhood,
                             WatershedOptions const & options = WatershedOptions());
    }
    \endcode

    <b> Usage:</b>

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

    Example: watersheds of the gradient magnitude (the example works likewise for higher dimensions).

    \code
    MultiArray<2, unsigned char> src(Shape2(w, h));
    ... // read input data
    
    // compute gradient magnitude at scale 1.0 as a boundary indicator
    MultiArray<2, float> gradMag(src.shape());
    gaussianGradientMagnitude(srcImageRange(src), destImage(gradMag), 1.0);

    // example 1
    {
        // the pixel type of the destination image must be large enough to hold
        // numbers up to 'max_region_label' to prevent overflow
        MultiArray<2, unsigned int> labeling(src.shape());
        
        // call region-growing algorithm for 4-neighborhood, leave a 1-pixel boundary between 
        // regions, and autogenerate seeds from all gradient minima where the magnitude is 
        // less than 2.0.
        unsigned int max_region_label = 
              watershedsMultiArray(gradMag, labeling, DirectNeighborhood,
                                   WatershedOptions().keepContours()
                                      .seedOptions(SeedOptions().minima().threshold(2.0)));
    }
    
    // example 2
    {
        MultiArray<2, unsigned int> labeling(src.shape());
        
        // compute seeds beforehand (use connected components of all pixels 
        // where the gradient is below 4.0)
        unsigned int max_region_label = generateWatershedSeeds(gradMag, labeling,
                                                       SeedOptions().levelSets(4.0));
        
        // quantize the gradient image to 256 gray levels
        float m, M;
        gradMag.minmax(&m, &M);
        
        using namespace multi_math;
        MultiArray<2, unsigned char> gradMag256(255.0 / (M - m) * (gradMag - m));
        
        // call region-growing algorithm with 8-neighborhood,
        // since the data are 8-bit, a faster priority queue will be used
        watershedsMultiArray(gradMag256, labeling, IndirectNeighborhood);
    }
    
    // example 3
    {
        MultiArray<2, unsigned int> labeling(src.shape());
        
        .. // put seeds in 'labeling', e.g. from an interactive labeling program,
           // make sure that label 1 corresponds to the background
        
        // bias the watershed algorithm so that the background is preferred
        // by reducing the cost for label 1 to 90%
        watershedsMultiArray(gradMag, labeling, 
                             WatershedOptions().biasLabel(1, 0.9));
    }
    
    // example 4
    {
        MultiArray<2, unsigned int> labeling(src.shape());
        
        // use the fast union-find algorithm with 4-neighborhood
        watershedsMultiArray(gradMag, labeling, WatershedOptions().unionFind());
    }
    \endcode
*/
doxygen_overloaded_function(template <...> Label watershedsMultiArray)

template <unsigned int N, class T, class S1,
                          class Label, class S2>
inline Label
watershedsMultiArray(MultiArrayView<N, T, S1> const & data,
                     MultiArrayView<N, Label, S2> labels,  // may also hold input seeds
                     NeighborhoodType neighborhood = DirectNeighborhood,
                     WatershedOptions const & options = WatershedOptions())
{
    vigra_precondition(data.shape() == labels.shape(),
        "watershedsMultiArray(): Shape mismatch between input and output.");
    
    GridGraph<N, undirected_tag> graph(data.shape(), neighborhood);
    return lemon_graph::watershedsGraph(graph, data, labels, options);
}

//@}

} // namespace vigra

#endif // VIGRA_MULTI_WATERSHEDS_HXX