[8f0f49]: src / foreign / vigra / seededregiongrowing.hxx  Maximize  Restore  History

Download this file

585 lines (479 with data), 20.8 kB

  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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
/************************************************************************/
/* */
/* Copyright 1998-2003 by Ullrich Koethe, Hans Meine */
/* Cognitive Systems Group, University of Hamburg, Germany */
/* */
/* This file is part of the VIGRA computer vision library. */
/* ( Version 1.4.0, Dec 21 2005 ) */
/* The VIGRA Website is */
/* http://kogs-www.informatik.uni-hamburg.de/~koethe/vigra/ */
/* Please direct questions, bug reports, and contributions to */
/* koethe@informatik.uni-hamburg.de or */
/* vigra@kogs1.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_SEEDEDREGIONGROWING_HXX
#define VIGRA_SEEDEDREGIONGROWING_HXX
#include <vector>
#include <stack>
#include <queue>
#include <vigra/utilities.hxx>
#include <vigra/stdimage.hxx>
#include <vigra/stdimagefunctions.hxx>
namespace vigra {
namespace detail {
template <class COST>
class SeedRgPixel
{
public:
Point2D location_, nearest_;
COST cost_;
int count_;
int label_;
int dist_;
SeedRgPixel()
: location_(0,0), nearest_(0,0), cost_(0), count_(0), label_(0)
{}
SeedRgPixel(Point2D const & location, Point2D const & nearest,
COST const & cost, int const & count, int const & label)
: location_(location), nearest_(nearest),
cost_(cost), count_(count), label_(label)
{
int dx = location_.x - nearest_.x;
int dy = location_.y - nearest_.y;
dist_ = dx * dx + dy * dy;
}
void set(Point2D const & location, Point2D const & nearest,
COST const & cost, int const & count, int const & label)
{
location_ = location;
nearest_ = nearest;
cost_ = cost;
count_ = count;
label_ = label;
int dx = location_.x - nearest_.x;
int dy = location_.y - nearest_.y;
dist_ = dx * dx + dy * dy;
}
struct Compare
{
// must implement > since priority_queue looks for largest element
bool operator()(SeedRgPixel const & l,
SeedRgPixel const & r) const
{
if(r.cost_ == l.cost_)
{
if(r.dist_ == l.dist_) return r.count_ < l.count_;
return r.dist_ < l.dist_;
}
return r.cost_ < l.cost_;
}
bool operator()(SeedRgPixel const * l,
SeedRgPixel const * r) const
{
if(r->cost_ == l->cost_)
{
if(r->dist_ == l->dist_) return r->count_ < l->count_;
return r->dist_ < l->dist_;
}
return r->cost_ < l->cost_;
}
};
struct Allocator
{
~Allocator()
{
while(!freelist_.empty())
{
delete freelist_.top();
freelist_.pop();
}
}
SeedRgPixel *
create(Point2D const & location, Point2D const & nearest,
COST const & cost, int const & count, int const & label)
{
if(!freelist_.empty())
{
SeedRgPixel * res = freelist_.top();
freelist_.pop();
res->set(location, nearest, cost, count, label);
return res;
}
return new SeedRgPixel(location, nearest, cost, count, label);
}
void dismiss(SeedRgPixel * p)
{
freelist_.push(p);
}
std::stack<SeedRgPixel<COST> *> freelist_;
};
};
struct UnlabelWatersheds
{
int operator()(int label) const
{
return label < 0 ? 0 : label;
}
};
} // namespace detail
enum SRGType { KeepContours, CompleteGrow, SRGWatershedLabel = -1 };
/** \addtogroup SeededRegionGrowing Region Segmentation Algorithms
Region growing, watersheds, and voronoi tesselation
*/
//@{
/********************************************************/
/* */
/* seededRegionGrowing */
/* */
/********************************************************/
/** \brief Region Segmentation by means of Seeded Region Growing.
This algorithm implements seeded region growing as described in
R. Adams, L. Bischof: "<em> Seeded Region Growing</em>", IEEE Trans. on Pattern
Analysis and Maschine Intelligence, vol 16, no 6, 1994, and
Ullrich K&ouml;the:
<em> "<a href="http://kogs-www.informatik.uni-hamburg.de/~koethe/papers/#primary">Primary Image Segmentation</a>"</em>,
in: G. Sagerer, S.
Posch, F. Kummert (eds.): Mustererkennung 1995, Proc. 17. DAGM-Symposium,
Springer 1995
The seed image is a partly segmented image which contains uniquely
labeled regions (the seeds) and unlabeled pixels (the candidates, label 0).
Seed regions can be as large as you wish and as small as one pixel. If
there are no candidates, the algorithm will simply copy the seed image
into the output image. Otherwise it will aggregate the candidates into
the existing regions so that a cost function is minimized. This
works as follows:
<ol>
<li> Find all candidate pixels that are 4-adjacent to a seed region.
Calculate the cost for aggregating each candidate into its adajacent region
and put the candidates into a priority queue.
<li> While( priority queue is not empty)
<ol>
<li> Take the candidate with least cost from the queue. If it has not
already been merged, merge it with it's adjacent region.
<li> Put all candidates that are 4-adjacent to the pixel just processed
into the priority queue.
</ol>
</ol>
If <tt>SRGType == CompleteGrow</tt> (the default), this algorithm
will produce a complete 4-connected tesselation of the image.
If <tt>SRGType == KeepContours</tt>, a one-pixel-wide border will be left
between the regions. The border pixels get label 0 (zero).
The cost is determined jointly by the source image and the
region statistics functor. The source image contains feature values for each
pixel which will be used by the region statistics functor to calculate and
update statistics for each region and to calculate the cost for each
candidate. The <TT>RegionStatisticsArray</TT> must be compatible to the
\ref ArrayOfRegionStatistics functor and contains an <em> array</em> of
statistics objects for each region. The indices must correspond to the
labels of the seed regions. The statistics for the initial regions must have
been calculated prior to calling <TT>seededRegionGrowing()</TT> (for example by
means of \ref inspectTwoImagesIf()).
For each candidate
<TT>x</TT> that is adjacent to region <TT>i</TT>, the algorithm will call
<TT>stats[i].cost(as(x))</TT> to get the cost (where <TT>x</TT> is a <TT>SrcImageIterator</TT>
and <TT>as</TT> is
the SrcAccessor). When a candidate has been merged with a region, the
statistics are updated by calling <TT>stats[i].operator()(as(x))</TT>. Since
the <TT>RegionStatisticsArray</TT> is passed by reference, this will overwrite
the original statistics.
If a candidate could be merged into more than one regions with identical
cost, the algorithm will favour the nearest region.
In some cases, the cost only depends on the feature value of the current
pixel. Then the update operation will simply be a no-op, and the <TT>cost()</TT>
function returns its argument. This behavior is implemented by the
\ref SeedRgDirectValueFunctor. With <tt>SRGType == KeepContours</tt>,
this is equivalent to the watershed algorithm.
<b> Declarations:</b>
pass arguments explicitly:
\code
namespace vigra {
template <class SrcImageIterator, class SrcAccessor,
class SeedImageIterator, class SeedAccessor,
class DestImageIterator, class DestAccessor,
class RegionStatisticsArray>
void seededRegionGrowing(SrcImageIterator srcul,
SrcImageIterator srclr, SrcAccessor as,
SeedImageIterator seedsul, SeedAccessor aseeds,
DestImageIterator destul, DestAccessor ad,
RegionStatisticsArray & stats,
SRGType srgType = CompleteGrow);
}
\endcode
use argument objects in conjunction with \ref ArgumentObjectFactories:
\code
namespace vigra {
template <class SrcImageIterator, class SrcAccessor,
class SeedImageIterator, class SeedAccessor,
class DestImageIterator, class DestAccessor,
class RegionStatisticsArray>
inline void
seededRegionGrowing(triple<SrcImageIterator, SrcImageIterator, SrcAccessor> img1,
pair<SeedImageIterator, SeedAccessor> img3,
pair<DestImageIterator, DestAccessor> img4,
RegionStatisticsArray & stats,
SRGType srgType = CompleteGrow);
}
\endcode
<b> Usage:</b>
<b>\#include</b> "<a href="seededregiongrowing_8hxx-source.html">vigra/seededregiongrowing.hxx</a>"<br>
Namespace: vigra
Example: implementation of the voronoi tesselation
\code
vigra::BImage points(w,h);
vigra::FImage dist(x,y);
// empty edge image
points = 0;
dist = 0;
int max_region_label = 100;
// throw in some random points:
for(int i = 1; i <= max_region_label; ++i)
points(w * rand() / RAND_MAX , h * rand() / RAND_MAX) = i;
// calculate Euclidean distance transform
vigra::distanceTransform(srcImageRange(points), destImage(dist), 2);
// init statistics functor
vigra::ArrayOfRegionStatistics<vigra::SeedRgDirectValueFunctor<float> >
stats(max_region_label);
// find voronoi region of each point
vigra:: seededRegionGrowing(srcImageRange(dist), srcImage(points),
destImage(points), stats);
\endcode
<b> Required Interface:</b>
\code
SrcImageIterator src_upperleft, src_lowerright;
SeedImageIterator seed_upperleft;
DestImageIterator dest_upperleft;
SrcAccessor src_accessor;
SeedAccessor seed_accessor;
DestAccessor dest_accessor;
RegionStatisticsArray stats;
// calculate costs
RegionStatisticsArray::value_type::cost_type cost =
stats[seed_accessor(seed_upperleft)].cost(src_accessor(src_upperleft));
// compare costs
cost < cost;
// update statistics
stats[seed_accessor(seed_upperleft)](src_accessor(src_upperleft));
// set result
dest_accessor.set(seed_accessor(seed_upperleft), dest_upperleft);
\endcode
Further requirements are determined by the <TT>RegionStatisticsArray</TT>.
*/
template <class SrcImageIterator, class SrcAccessor,
class SeedImageIterator, class SeedAccessor,
class DestImageIterator, class DestAccessor,
class RegionStatisticsArray>
void seededRegionGrowing(SrcImageIterator srcul,
SrcImageIterator srclr, SrcAccessor as,
SeedImageIterator seedsul, SeedAccessor aseeds,
DestImageIterator destul, DestAccessor ad,
RegionStatisticsArray & stats,
const SRGType srgType)
{
int w = srclr.x - srcul.x;
int h = srclr.y - srcul.y;
int count = 0;
SrcImageIterator isy = srcul, isx = srcul; // iterators for the src image
typedef typename RegionStatisticsArray::value_type RegionStatistics;
typedef typename RegionStatistics::cost_type CostType;
typedef detail::SeedRgPixel<CostType> Pixel;
typename Pixel::Allocator allocator;
typedef std::priority_queue<Pixel *, std::vector<Pixel *>,
typename Pixel::Compare> SeedRgPixelHeap;
// copy seed image in an image with border
IImage regions(w+2, h+2);
IImage::Iterator ir = regions.upperLeft() + Diff2D(1,1);
IImage::Iterator iry, irx;
initImageBorder(destImageRange(regions), 1, SRGWatershedLabel);
copyImage(seedsul, seedsul+Diff2D(w,h), aseeds, ir, regions.accessor());
// allocate and init memory for the results
SeedRgPixelHeap pheap;
int cneighbor;
static const Diff2D dist[] = { Diff2D(-1,0), Diff2D(0,-1),
Diff2D(1,0), Diff2D(0,1) };
Point2D pos(0,0);
for(isy=srcul, iry=ir, pos.y=0; pos.y<h;
++pos.y, ++isy.y, ++iry.y)
{
for(isx=isy, irx=iry, pos.x=0; pos.x<w;
++pos.x, ++isx.x, ++irx.x)
{
if(*irx == 0)
{
// find candidate pixels for growing and fill heap
for(int i=0; i<4; i++)
{
cneighbor = irx[dist[i]];
if(cneighbor > 0)
{
CostType cost = stats[cneighbor].cost(as(isx));
Pixel * pixel =
allocator.create(pos, pos+dist[i], cost, count++, cneighbor);
pheap.push(pixel);
}
}
}
}
}
// perform region growing
while(pheap.size() != 0)
{
Pixel * pixel = pheap.top();
pheap.pop();
Point2D pos = pixel->location_;
Point2D nearest = pixel->nearest_;
int lab = pixel->label_;
allocator.dismiss(pixel);
irx = ir + pos;
isx = srcul + pos;
if(*irx) // already labelled region / watershed?
continue;
if(srgType == KeepContours)
{
for(int i=0; i<4; i++)
{
cneighbor = irx[dist[i]];
if((cneighbor>0) && (cneighbor != lab))
{
lab = SRGWatershedLabel;
break;
}
}
}
*irx = lab;
if((srgType != KeepContours) || (lab > 0))
{
// update statistics
stats[*irx](as(isx));
// search neighborhood
// second pass: find new candidate pixels
for(int i=0; i<4; i++)
{
if(irx[dist[i]] == 0)
{
CostType cost = stats[lab].cost(as(isx, dist[i]));
Pixel * new_pixel =
allocator.create(pos+dist[i], nearest, cost, count++, lab);
pheap.push(new_pixel);
}
}
}
}
// write result
transformImage(ir, ir+Point2D(w,h), regions.accessor(), destul, ad,
detail::UnlabelWatersheds());
}
template <class SrcImageIterator, class SrcAccessor,
class SeedImageIterator, class SeedAccessor,
class DestImageIterator, class DestAccessor,
class RegionStatisticsArray>
inline void
seededRegionGrowing(SrcImageIterator srcul,
SrcImageIterator srclr, SrcAccessor as,
SeedImageIterator seedsul, SeedAccessor aseeds,
DestImageIterator destul, DestAccessor ad,
RegionStatisticsArray & stats)
{
seededRegionGrowing(srcul, srclr, as,
seedsul, aseeds,
destul, ad,
stats, CompleteGrow);
}
template <class SrcImageIterator, class SrcAccessor,
class SeedImageIterator, class SeedAccessor,
class DestImageIterator, class DestAccessor,
class RegionStatisticsArray>
inline void
seededRegionGrowing(triple<SrcImageIterator, SrcImageIterator, SrcAccessor> img1,
pair<SeedImageIterator, SeedAccessor> img3,
pair<DestImageIterator, DestAccessor> img4,
RegionStatisticsArray & stats,
SRGType srgType)
{
seededRegionGrowing(img1.first, img1.second, img1.third,
img3.first, img3.second,
img4.first, img4.second,
stats, srgType);
}
template <class SrcImageIterator, class SrcAccessor,
class SeedImageIterator, class SeedAccessor,
class DestImageIterator, class DestAccessor,
class RegionStatisticsArray>
inline void
seededRegionGrowing(triple<SrcImageIterator, SrcImageIterator, SrcAccessor> img1,
pair<SeedImageIterator, SeedAccessor> img3,
pair<DestImageIterator, DestAccessor> img4,
RegionStatisticsArray & stats)
{
seededRegionGrowing(img1.first, img1.second, img1.third,
img3.first, img3.second,
img4.first, img4.second,
stats, CompleteGrow);
}
/********************************************************/
/* */
/* SeedRgDirectValueFunctor */
/* */
/********************************************************/
/** \brief Statistics functor to be used for seeded region growing.
This functor can be used if the cost of a candidate during
\ref seededRegionGrowing() is equal to the feature value of that
candidate and does not depend on properties of the region it is going to
be merged with.
<b>\#include</b> "<a href="seededregiongrowing_8hxx-source.html">vigra/seededregiongrowing.hxx</a>"<br>
Namespace: vigra
<b> Required Interface:</b>
no requirements
*/
template <class Value>
class SeedRgDirectValueFunctor
{
public:
/** the functor's argument type
*/
typedef Value argument_type;
/** the functor's result type (unused, only necessary for
use of SeedRgDirectValueFunctor in \ref vigra::ArrayOfRegionStatistics
*/
typedef Value result_type;
/** \deprecated use argument_type
*/
typedef Value value_type;
/** the return type of the cost() function
*/
typedef Value cost_type;
/** Do nothing (since we need not update region statistics).
*/
void operator()(argument_type const &) const {}
/** Return argument (since cost is identical to feature value)
*/
cost_type const & cost(argument_type const & v) const
{
return v;
}
};
//@}
} // namespace vigra
#endif // VIGRA_SEEDEDREGIONGROWING_HXX

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks