/usr/include/vigra/seededregiongrowing.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 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 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 | /************************************************************************/
/* */
/* Copyright 1998-2010 by Ullrich Koethe, Hans Meine */
/* */
/* 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_SEEDEDREGIONGROWING_HXX
#define VIGRA_SEEDEDREGIONGROWING_HXX
#include <vector>
#include <stack>
#include <queue>
#include "utilities.hxx"
#include "stdimage.hxx"
#include "stdimagefunctions.hxx"
#include "pixelneighborhood.hxx"
#include "bucket_queue.hxx"
#include "multi_shape.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
/** \addtogroup SeededRegionGrowing Region Segmentation Algorithms
Region growing, watersheds, and voronoi tesselation
*/
//@{
/********************************************************/
/* */
/* seededRegionGrowing */
/* */
/********************************************************/
/** Choose between different types of Region Growing */
enum SRGType {
CompleteGrow = 0,
KeepContours = 1,
StopAtThreshold = 2,
SRGWatershedLabel = -1
};
/** \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öthe:
<em><a href="http://hci.iwr.uni-heidelberg.de/people/ukoethe/papers/index.php#cite_primary_segmentation">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).
The highest seed label found in the seed image is returned by the algorithm.
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.
Candidates are taken from the neighborhood of the already assigned pixels,
where the type of neighborhood is determined by parameter <tt>neighborhood</tt>
which can take the values <tt>FourNeighborCode()</tt> (the default)
or <tt>EightNeighborCode()</tt>. The algorithm basically works as follows
(illustrated for 4-neighborhood, but 8-neighborhood works in the same way):
<ol>
<li> Find all candidate pixels that are 4-adjacent to a seed region.
Calculate the cost for aggregating each candidate into its adjacent region
and put the candidates into a priority queue.
<li> While( priority queue is not empty and termination criterion is not fulfilled)
<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>
<tt>SRGType</tt> can take the following values:
<DL>
<DT><tt>CompleteGrow</tt> <DD> produce a complete tesselation of the volume (default).
<DT><tt>KeepContours</tt> <DD> keep a 1-voxel wide unlabeled contour between all regions.
<DT><tt>StopAtThreshold</tt> <DD> stop when the boundary indicator values exceed the
threshold given by parameter <tt>max_cost</tt>.
<DT><tt>KeepContours | StopAtThreshold</tt> <DD> keep 1-voxel wide contour and stop at given <tt>max_cost</tt>.
</DL>
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>SrcIterator</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. If <tt>StopAtThreshold</tt> is active,
and the cost of the current candidate at any point in the algorithm exceeds the optional
<tt>max_cost</tt> value (which defaults to <tt>NumericTraits<double>::max()</tt>),
region growing is aborted, and all voxels not yet assigned to a region remain unlabeled.
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 2D array views:
\code
namespace vigra {
template <class T1, class S1,
class TS, class AS,
class T2, class S2,
class RegionStatisticsArray, class Neighborhood>
TS
seededRegionGrowing(MultiArrayView<2, T1, S1> const & src,
MultiArrayView<2, TS, AS> const & seeds,
MultiArrayView<2, T2, S2> labels,
RegionStatisticsArray & stats,
SRGType srgType = CompleteGrow,
Neighborhood n = FourNeighborCode(),
double max_cost = NumericTraits<double>::max());
}
\endcode
\deprecatedAPI{seededRegionGrowing}
pass \ref ImageIterators and \ref DataAccessors :
\code
namespace vigra {
template <class SrcIterator, class SrcAccessor,
class SeedImageIterator, class SeedAccessor,
class DestIterator, class DestAccessor,
class RegionStatisticsArray, class Neighborhood>
typename SeedAccessor::value_type
seededRegionGrowing(SrcIterator srcul, SrcIterator srclr, SrcAccessor as,
SeedImageIterator seedsul, SeedAccessor aseeds,
DestIterator destul, DestAccessor ad,
RegionStatisticsArray & stats,
SRGType srgType = CompleteGrow,
Neighborhood neighborhood = FourNeighborCode(),
double max_cost = NumericTraits<double>::max());
}
\endcode
use argument objects in conjunction with \ref ArgumentObjectFactories :
\code
namespace vigra {
template <class SrcIterator, class SrcAccessor,
class SeedImageIterator, class SeedAccessor,
class DestIterator, class DestAccessor,
class RegionStatisticsArray, class Neighborhood>
typename SeedAccessor::value_type
seededRegionGrowing(triple<SrcIterator, SrcIterator, SrcAccessor> src,
pair<SeedImageIterator, SeedAccessor> seeds,
pair<DestIterator, DestAccessor> dest,
RegionStatisticsArray & stats,
SRGType srgType = CompleteGrow,
Neighborhood neighborhood = FourNeighborCode(),
double max_cost = NumericTraits<double>::max());
}
\endcode
\deprecatedEnd
<b> Usage:</b>
<b>\#include</b> \<vigra/seededregiongrowing.hxx\><br>
Namespace: vigra
Example: implementation of the voronoi tesselation
\code
MultiArray<2, int> points(w,h);
MultiArray<2, float> dist(x,y);
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
distanceTransform(points, dist, 2);
// init statistics functor
ArrayOfRegionStatistics<SeedRgDirectValueFunctor<float> > stats(max_region_label);
// find voronoi region of each point (the point image is overwritten with the
// voronoi region labels)
seededRegionGrowing(dist, points, points, stats);
\endcode
\deprecatedUsage{seededRegionGrowing}
\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
SrcIterator src_upperleft, src_lowerright;
SeedImageIterator seed_upperleft;
DestIterator 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
\deprecatedEnd
Further requirements are determined by the <TT>RegionStatisticsArray</TT>.
*/
doxygen_overloaded_function(template <...> void seededRegionGrowing)
template <class SrcIterator, class SrcAccessor,
class SeedImageIterator, class SeedAccessor,
class DestIterator, class DestAccessor,
class RegionStatisticsArray, class Neighborhood>
typename SeedAccessor::value_type
seededRegionGrowing(SrcIterator srcul,
SrcIterator srclr, SrcAccessor as,
SeedImageIterator seedsul, SeedAccessor aseeds,
DestIterator destul, DestAccessor ad,
RegionStatisticsArray & stats,
SRGType srgType,
Neighborhood,
double max_cost)
{
int w = srclr.x - srcul.x;
int h = srclr.y - srcul.y;
int count = 0;
SrcIterator isy = srcul, isx = srcul; // iterators for the src image
typedef typename SeedAccessor::value_type LabelType;
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, maxRegionLabel = 0;
typedef typename Neighborhood::Direction Direction;
int directionCount = Neighborhood::DirectionCount;
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<directionCount; i++)
{
// cneighbor = irx[dist[i]];
cneighbor = irx[Neighborhood::diff((Direction)i)];
if(cneighbor > 0)
{
CostType cost = stats[cneighbor].cost(as(isx));
Pixel * pixel =
allocator.create(pos, pos+Neighborhood::diff((Direction)i), cost, count++, cneighbor);
pheap.push(pixel);
}
}
}
else
{
vigra_precondition((LabelType)*irx <= stats.maxRegionLabel(),
"seededRegionGrowing(): Largest label exceeds size of RegionStatisticsArray.");
if(maxRegionLabel < *irx)
maxRegionLabel = *irx;
}
}
}
// 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_;
CostType cost = pixel->cost_;
allocator.dismiss(pixel);
if((srgType & StopAtThreshold) != 0 && cost > max_cost)
break;
irx = ir + pos;
isx = srcul + pos;
if(*irx) // already labelled region / watershed?
continue;
if((srgType & KeepContours) != 0)
{
for(int i=0; i<directionCount; i++)
{
cneighbor = irx[Neighborhood::diff((Direction)i)];
if((cneighbor>0) && (cneighbor != lab))
{
lab = SRGWatershedLabel;
break;
}
}
}
*irx = lab;
if((srgType & KeepContours) == 0 || lab > 0)
{
// update statistics
stats[*irx](as(isx));
// search neighborhood
// second pass: find new candidate pixels
for(int i=0; i<directionCount; i++)
{
if(irx[Neighborhood::diff((Direction)i)] == 0)
{
CostType cost = stats[lab].cost(as(isx, Neighborhood::diff((Direction)i)));
Pixel * new_pixel =
allocator.create(pos+Neighborhood::diff((Direction)i), nearest, cost, count++, lab);
pheap.push(new_pixel);
}
}
}
}
// free temporary memory
while(pheap.size() != 0)
{
allocator.dismiss(pheap.top());
pheap.pop();
}
// write result
transformImage(ir, ir+Point2D(w,h), regions.accessor(), destul, ad,
detail::UnlabelWatersheds());
return (LabelType)maxRegionLabel;
}
template <class SrcIterator, class SrcAccessor,
class SeedImageIterator, class SeedAccessor,
class DestIterator, class DestAccessor,
class RegionStatisticsArray, class Neighborhood>
inline typename SeedAccessor::value_type
seededRegionGrowing(SrcIterator srcul,
SrcIterator srclr, SrcAccessor as,
SeedImageIterator seedsul, SeedAccessor aseeds,
DestIterator destul, DestAccessor ad,
RegionStatisticsArray & stats,
SRGType srgType,
Neighborhood n)
{
return seededRegionGrowing(srcul, srclr, as,
seedsul, aseeds,
destul, ad,
stats, srgType, n, NumericTraits<double>::max());
}
template <class SrcIterator, class SrcAccessor,
class SeedImageIterator, class SeedAccessor,
class DestIterator, class DestAccessor,
class RegionStatisticsArray>
inline typename SeedAccessor::value_type
seededRegionGrowing(SrcIterator srcul,
SrcIterator srclr, SrcAccessor as,
SeedImageIterator seedsul, SeedAccessor aseeds,
DestIterator destul, DestAccessor ad,
RegionStatisticsArray & stats,
SRGType srgType)
{
return seededRegionGrowing(srcul, srclr, as,
seedsul, aseeds,
destul, ad,
stats, srgType, FourNeighborCode());
}
template <class SrcIterator, class SrcAccessor,
class SeedImageIterator, class SeedAccessor,
class DestIterator, class DestAccessor,
class RegionStatisticsArray>
inline typename SeedAccessor::value_type
seededRegionGrowing(SrcIterator srcul,
SrcIterator srclr, SrcAccessor as,
SeedImageIterator seedsul, SeedAccessor aseeds,
DestIterator destul, DestAccessor ad,
RegionStatisticsArray & stats)
{
return seededRegionGrowing(srcul, srclr, as,
seedsul, aseeds,
destul, ad,
stats, CompleteGrow);
}
template <class SrcIterator, class SrcAccessor,
class SeedImageIterator, class SeedAccessor,
class DestIterator, class DestAccessor,
class RegionStatisticsArray, class Neighborhood>
inline typename SeedAccessor::value_type
seededRegionGrowing(triple<SrcIterator, SrcIterator, SrcAccessor> img1,
pair<SeedImageIterator, SeedAccessor> img3,
pair<DestIterator, DestAccessor> img4,
RegionStatisticsArray & stats,
SRGType srgType,
Neighborhood n,
double max_cost = NumericTraits<double>::max())
{
return seededRegionGrowing(img1.first, img1.second, img1.third,
img3.first, img3.second,
img4.first, img4.second,
stats, srgType, n, max_cost);
}
template <class SrcIterator, class SrcAccessor,
class SeedImageIterator, class SeedAccessor,
class DestIterator, class DestAccessor,
class RegionStatisticsArray>
inline typename SeedAccessor::value_type
seededRegionGrowing(triple<SrcIterator, SrcIterator, SrcAccessor> img1,
pair<SeedImageIterator, SeedAccessor> img3,
pair<DestIterator, DestAccessor> img4,
RegionStatisticsArray & stats,
SRGType srgType)
{
return seededRegionGrowing(img1.first, img1.second, img1.third,
img3.first, img3.second,
img4.first, img4.second,
stats, srgType, FourNeighborCode());
}
template <class SrcIterator, class SrcAccessor,
class SeedImageIterator, class SeedAccessor,
class DestIterator, class DestAccessor,
class RegionStatisticsArray>
inline typename SeedAccessor::value_type
seededRegionGrowing(triple<SrcIterator, SrcIterator, SrcAccessor> img1,
pair<SeedImageIterator, SeedAccessor> img3,
pair<DestIterator, DestAccessor> img4,
RegionStatisticsArray & stats)
{
return seededRegionGrowing(img1.first, img1.second, img1.third,
img3.first, img3.second,
img4.first, img4.second,
stats, CompleteGrow);
}
template <class T1, class S1,
class TS, class AS,
class T2, class S2,
class RegionStatisticsArray, class Neighborhood>
inline TS
seededRegionGrowing(MultiArrayView<2, T1, S1> const & img1,
MultiArrayView<2, TS, AS> const & img3,
MultiArrayView<2, T2, S2> img4,
RegionStatisticsArray & stats,
SRGType srgType,
Neighborhood n,
double max_cost = NumericTraits<double>::max())
{
vigra_precondition(img1.shape() == img3.shape(),
"seededRegionGrowing(): shape mismatch between input and output.");
return seededRegionGrowing(srcImageRange(img1),
srcImage(img3),
destImage(img4),
stats, srgType, n, max_cost);
}
template <class T1, class S1,
class TS, class AS,
class T2, class S2,
class RegionStatisticsArray>
inline TS
seededRegionGrowing(MultiArrayView<2, T1, S1> const & img1,
MultiArrayView<2, TS, AS> const & img3,
MultiArrayView<2, T2, S2> img4,
RegionStatisticsArray & stats,
SRGType srgType)
{
vigra_precondition(img1.shape() == img3.shape(),
"seededRegionGrowing(): shape mismatch between input and output.");
return seededRegionGrowing(srcImageRange(img1),
srcImage(img3),
destImage(img4),
stats, srgType, FourNeighborCode());
}
template <class T1, class S1,
class TS, class AS,
class T2, class S2,
class RegionStatisticsArray>
inline TS
seededRegionGrowing(MultiArrayView<2, T1, S1> const & img1,
MultiArrayView<2, TS, AS> const & img3,
MultiArrayView<2, T2, S2> img4,
RegionStatisticsArray & stats)
{
vigra_precondition(img1.shape() == img3.shape(),
"seededRegionGrowing(): shape mismatch between input and output.");
return seededRegionGrowing(srcImageRange(img1),
srcImage(img3),
destImage(img4),
stats, CompleteGrow);
}
/********************************************************/
/* */
/* fastSeededRegionGrowing */
/* */
/********************************************************/
template <class SrcIterator, class SrcAccessor,
class DestIterator, class DestAccessor,
class RegionStatisticsArray, class Neighborhood>
typename DestAccessor::value_type
fastSeededRegionGrowing(SrcIterator srcul, SrcIterator srclr, SrcAccessor as,
DestIterator destul, DestAccessor ad,
RegionStatisticsArray & stats,
SRGType srgType,
Neighborhood,
double max_cost,
std::ptrdiff_t bucket_count = 256)
{
typedef typename DestAccessor::value_type LabelType;
vigra_precondition((srgType & KeepContours) == 0,
"fastSeededRegionGrowing(): the turbo algorithm doesn't support 'KeepContours', sorry.");
int w = srclr.x - srcul.x;
int h = srclr.y - srcul.y;
SrcIterator isy = srcul, isx = srcul; // iterators for the src image
DestIterator idy = destul, idx = destul; // iterators for the dest image
BucketQueue<Point2D, true> pqueue(bucket_count);
LabelType maxRegionLabel = 0;
Point2D pos(0,0);
for(isy=srcul, idy = destul, pos.y=0; pos.y<h; ++pos.y, ++isy.y, ++idy.y)
{
for(isx=isy, idx=idy, pos.x=0; pos.x<w; ++pos.x, ++isx.x, ++idx.x)
{
LabelType label = ad(idx);
if(label != 0)
{
vigra_precondition(label <= stats.maxRegionLabel(),
"fastSeededRegionGrowing(): Largest label exceeds size of RegionStatisticsArray.");
if(maxRegionLabel < label)
maxRegionLabel = label;
AtImageBorder atBorder = isAtImageBorder(pos.x, pos.y, w, h);
if(atBorder == NotAtBorder)
{
NeighborhoodCirculator<DestIterator, Neighborhood> c(idx), cend(c);
do
{
if(ad(c) == 0)
{
std::ptrdiff_t priority = (std::ptrdiff_t)stats[label].cost(as(isx));
pqueue.push(pos, priority);
break;
}
}
while(++c != cend);
}
else
{
RestrictedNeighborhoodCirculator<DestIterator, Neighborhood>
c(idx, atBorder), cend(c);
do
{
if(ad(c) == 0)
{
std::ptrdiff_t priority = (std::ptrdiff_t)stats[label].cost(as(isx));
pqueue.push(pos, priority);
break;
}
}
while(++c != cend);
}
}
}
}
// perform region growing
while(!pqueue.empty())
{
Point2D pos = pqueue.top();
std::ptrdiff_t cost = pqueue.topPriority();
pqueue.pop();
if((srgType & StopAtThreshold) != 0 && cost > max_cost)
break;
idx = destul + pos;
isx = srcul + pos;
std::ptrdiff_t label = ad(idx);
AtImageBorder atBorder = isAtImageBorder(pos.x, pos.y, w, h);
if(atBorder == NotAtBorder)
{
NeighborhoodCirculator<DestIterator, Neighborhood> c(idx), cend(c);
do
{
std::ptrdiff_t nlabel = ad(c);
if(nlabel == 0)
{
ad.set(label, idx, c.diff());
std::ptrdiff_t priority =
std::max((std::ptrdiff_t)stats[label].cost(as(isx, c.diff())), cost);
pqueue.push(pos+c.diff(), priority);
}
}
while(++c != cend);
}
else
{
RestrictedNeighborhoodCirculator<DestIterator, Neighborhood>
c(idx, atBorder), cend(c);
do
{
std::ptrdiff_t nlabel = ad(c);
if(nlabel == 0)
{
ad.set(label, idx, c.diff());
std::ptrdiff_t priority =
std::max((std::ptrdiff_t)stats[label].cost(as(isx, c.diff())), cost);
pqueue.push(pos+c.diff(), priority);
}
}
while(++c != cend);
}
}
return maxRegionLabel;
}
template <class SrcIterator, class SrcAccessor,
class DestIterator, class DestAccessor,
class RegionStatisticsArray, class Neighborhood>
inline typename DestAccessor::value_type
fastSeededRegionGrowing(SrcIterator srcul, SrcIterator srclr, SrcAccessor as,
DestIterator destul, DestAccessor ad,
RegionStatisticsArray & stats,
SRGType srgType,
Neighborhood n)
{
return fastSeededRegionGrowing(srcul, srclr, as,
destul, ad,
stats, srgType, n, NumericTraits<double>::max(), 256);
}
template <class SrcIterator, class SrcAccessor,
class DestIterator, class DestAccessor,
class RegionStatisticsArray>
inline typename DestAccessor::value_type
fastSeededRegionGrowing(SrcIterator srcul, SrcIterator srclr, SrcAccessor as,
DestIterator destul, DestAccessor ad,
RegionStatisticsArray & stats,
SRGType srgType)
{
return fastSeededRegionGrowing(srcul, srclr, as,
destul, ad,
stats, srgType, FourNeighborCode());
}
template <class SrcIterator, class SrcAccessor,
class DestIterator, class DestAccessor,
class RegionStatisticsArray>
inline typename DestAccessor::value_type
fastSeededRegionGrowing(SrcIterator srcul, SrcIterator srclr, SrcAccessor as,
DestIterator destul, DestAccessor ad,
RegionStatisticsArray & stats)
{
return fastSeededRegionGrowing(srcul, srclr, as,
destul, ad,
stats, CompleteGrow);
}
template <class SrcIterator, class SrcAccessor,
class DestIterator, class DestAccessor,
class RegionStatisticsArray, class Neighborhood>
inline typename DestAccessor::value_type
fastSeededRegionGrowing(triple<SrcIterator, SrcIterator, SrcAccessor> src,
pair<DestIterator, DestAccessor> dest,
RegionStatisticsArray & stats,
SRGType srgType,
Neighborhood n,
double max_cost,
std::ptrdiff_t bucket_count = 256)
{
return fastSeededRegionGrowing(src.first, src.second, src.third,
dest.first, dest.second,
stats, srgType, n, max_cost, bucket_count);
}
template <class T1, class S1,
class T2, class S2,
class RegionStatisticsArray, class Neighborhood>
inline T2
fastSeededRegionGrowing(MultiArrayView<2, T1, S1> const & src,
MultiArrayView<2, T2, S2> dest,
RegionStatisticsArray & stats,
SRGType srgType,
Neighborhood n,
double max_cost,
std::ptrdiff_t bucket_count = 256)
{
vigra_precondition(src.shape() == dest.shape(),
"fastSeededRegionGrowing(): shape mismatch between input and output.");
return fastSeededRegionGrowing(srcImageRange(src),
destImage(dest),
stats, srgType, n, max_cost, bucket_count);
}
/********************************************************/
/* */
/* 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> \<vigra/seededregiongrowing.hxx\><br>
Namespace: vigra
*/
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
|