/usr/include/vigra/labelimage.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 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 | /************************************************************************/
/* */
/* Copyright 1998-2002 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_LABELIMAGE_HXX
#define VIGRA_LABELIMAGE_HXX
#include <vector>
#include <functional>
#include "utilities.hxx"
#include "stdimage.hxx"
#include "union_find.hxx"
#include "sized_int.hxx"
#include "multi_shape.hxx"
namespace vigra {
/** \addtogroup Labeling Connected Components Labeling
The 2-dimensional connected components algorithms may use either 4 or 8 connectivity.
By means of a functor the merge criterion can be defined arbitrarily.
*/
//@{
/********************************************************/
/* */
/* labelImage */
/* */
/********************************************************/
/** \brief Find the connected components of a segmented image.
Connected components are defined as regions with uniform pixel
values. Thus, <TT>T1</TT> either must be
equality comparable, or a suitable EqualityFunctor must be
provided that realizes the desired predicate. The
destination's value type <tt>T2</tt> should be large enough to hold the labels
without overflow. Region numbers will be a consecutive sequence
starting with one and ending with the region number returned by
the function (inclusive). The parameter '<TT>eight_neighbors</TT>'
determines whether the regions should be 4-connected (false) or
8-connected (true).
Return: the number of regions found (= largest region label)
See \ref labelMultiArray() for a dimension-independent implementation of
connected components labelling.
<b> Declarations:</b>
pass 2D array views:
\code
namespace vigra {
template <class T1, class S1,
class T2, class S2,
class EqualityFunctor = std::equal_to<T1> >
unsigned int
labelImage(MultiArrayView<2, T1, S1> const & src,
MultiArrayView<2, T2, S2> dest,
bool eight_neighbors, EqualityFunctor equal = EqualityFunctor());
}
\endcode
\deprecatedAPI{labelImage}
pass \ref ImageIterators and \ref DataAccessors :
\code
namespace vigra {
template <class SrcIterator, class SrcAccessor,
class DestIterator, class DestAccessor>
unsigned int labelImage(SrcIterator upperlefts,
SrcIterator lowerrights, SrcAccessor sa,
DestIterator upperleftd, DestAccessor da,
bool eight_neighbors);
template <class SrcIterator, class SrcAccessor,
class DestIterator, class DestAccessor,
class EqualityFunctor>
unsigned int labelImage(SrcIterator upperlefts,
SrcIterator lowerrights, SrcAccessor sa,
DestIterator upperleftd, DestAccessor da,
bool eight_neighbors, EqualityFunctor equal);
}
\endcode
use argument objects in conjunction with \ref ArgumentObjectFactories :
\code
namespace vigra {
template <class SrcIterator, class SrcAccessor,
class DestIterator, class DestAccessor>
unsigned int labelImage(triple<SrcIterator, SrcIterator, SrcAccessor> src,
pair<DestIterator, DestAccessor> dest,
bool eight_neighbors);
template <class SrcIterator, class SrcAccessor,
class DestIterator, class DestAccessor,
class EqualityFunctor>
unsigned int labelImage(triple<SrcIterator, SrcIterator, SrcAccessor> src,
pair<DestIterator, DestAccessor> dest,
bool eight_neighbors, EqualityFunctor equal)
}
\endcode
\deprecatedEnd
<b> Usage:</b>
<b>\#include</b> \<vigra/labelimage.hxx\><br>
Namespace: vigra
\code
MultiArray<2, unsigned char> src(w,h);
MultiArray<2, unsigned int> labels(w,h);
// threshold at 128
transformImage(src, src, Threshold<int, int>(128, 256, 0, 255));
// find 4-connected regions
labelImage(src, labels, false);
\endcode
\deprecatedUsage{labelImage}
\code
vigra::BImage src(w,h);
vigra::IImage labels(w,h);
// threshold at 128
vigra::transformImage(srcImageRange(src), destImage(src),
vigra::Threshold<vigra::BImage::PixelType, vigra::BImage::PixelType>(
128, 256, 0, 255));
// find 4-connected regions
vigra::labelImage(srcImageRange(src), destImage(labels), false);
\endcode
<b> Required Interface:</b>
\code
SrcImageIterator src_upperleft, src_lowerright;
DestImageIterator dest_upperleft;
SrcAccessor src_accessor;
DestAccessor dest_accessor;
SrcAccessor::value_type u = src_accessor(src_upperleft);
u == u // first form
EqualityFunctor equal; // second form
equal(u, u) // second form
int i;
dest_accessor.set(i, dest_upperleft);
\endcode
\deprecatedEnd
*/
doxygen_overloaded_function(template <...> unsigned int labelImage)
template <class SrcIterator, class SrcAccessor,
class DestIterator, class DestAccessor,
class EqualityFunctor>
unsigned int labelImage(SrcIterator upperlefts,
SrcIterator lowerrights, SrcAccessor sa,
DestIterator upperleftd, DestAccessor da,
bool eight_neighbors, EqualityFunctor equal)
{
typedef typename DestAccessor::value_type LabelType;
int w = lowerrights.x - upperlefts.x;
int h = lowerrights.y - upperlefts.y;
int x,y,i;
const Diff2D neighbor[] = {
Diff2D(-1,0), // left
Diff2D(-1,-1), // topleft
Diff2D(0,-1), // top
Diff2D(1,-1) // topright
};
const int left = 0, /* unused: topleft = 1, */ top = 2, topright = 3;
int step = eight_neighbors ? 1 : 2;
SrcIterator ys = upperlefts;
DestIterator yd = upperleftd;
detail::UnionFindArray<LabelType> label;
// pass 1: scan image from upper left to lower right
// to find connected components
// Each component will be represented by a tree of pixels. Each
// pixel contains the scan order address of its parent in the
// tree. In order for pass 2 to work correctly, the parent must
// always have a smaller scan order address than the child.
// Therefore, we can merge trees only at their roots, because the
// root of the combined tree must have the smallest scan order
// address among all the tree's pixels/ nodes. The root of each
// tree is distinguished by pointing to itself (it contains its
// own scan order address). This condition is enforced whenever a
// new region is found or two regions are merged
for(y = 0; y != h; ++y, ++ys.y, ++yd.y)
{
SrcIterator xs = ys;
DestIterator xd = yd;
int endNeighbor = (y == 0) ? left : (eight_neighbors ? topright : top);
for(x = 0; x != w; ++x, ++xs.x, ++xd.x)
{
int beginNeighbor = (x == 0) ? top : left;
if(x == w-1 && endNeighbor == topright) endNeighbor = top;
for(i=beginNeighbor; i<=endNeighbor; i+=step)
{
if(equal(sa(xs), sa(xs, neighbor[i])))
{
LabelType neighborLabel = label.find(da(xd,neighbor[i]));
for(int j=i+2; j<=endNeighbor; j+=step)
{
if(equal(sa(xs), sa(xs, neighbor[j])))
{
neighborLabel = label.makeUnion(da(xd, neighbor[j]), neighborLabel);
break;
}
}
da.set(neighborLabel, xd);
break;
}
}
if(i > endNeighbor)
{
da.set(label.makeNewLabel(), xd);
}
}
}
// pass 2: assign one label to each region (tree)
// so that labels form a consecutive sequence 1, 2, ...
unsigned int count = label.makeContiguous();
yd = upperleftd;
for(y=0; y != h; ++y, ++yd.y)
{
typename DestIterator::row_iterator xd = yd.rowIterator();
for(x = 0; x != w; ++x, ++xd)
{
da.set(label[da(xd)], xd);
}
}
return count;
}
template <class SrcIterator, class SrcAccessor,
class DestIterator, class DestAccessor>
inline
unsigned int labelImage(SrcIterator upperlefts,
SrcIterator lowerrights, SrcAccessor sa,
DestIterator upperleftd, DestAccessor da,
bool eight_neighbors)
{
return labelImage(upperlefts, lowerrights, sa,
upperleftd, da, eight_neighbors,
std::equal_to<typename SrcAccessor::value_type>());
}
template <class SrcIterator, class SrcAccessor,
class DestIterator, class DestAccessor,
class EqualityFunctor>
inline unsigned int
labelImage(triple<SrcIterator, SrcIterator, SrcAccessor> src,
pair<DestIterator, DestAccessor> dest,
bool eight_neighbors, EqualityFunctor equal)
{
return labelImage(src.first, src.second, src.third,
dest.first, dest.second, eight_neighbors, equal);
}
template <class SrcIterator, class SrcAccessor,
class DestIterator, class DestAccessor>
inline unsigned int
labelImage(triple<SrcIterator, SrcIterator, SrcAccessor> src,
pair<DestIterator, DestAccessor> dest,
bool eight_neighbors)
{
return labelImage(src.first, src.second, src.third,
dest.first, dest.second, eight_neighbors,
std::equal_to<typename SrcAccessor::value_type>());
}
template <class T1, class S1,
class T2, class S2,
class EqualityFunctor>
inline unsigned int
labelImage(MultiArrayView<2, T1, S1> const & src,
MultiArrayView<2, T2, S2> dest,
bool eight_neighbors, EqualityFunctor equal)
{
vigra_precondition(src.shape() == dest.shape(),
"labelImage(): shape mismatch between input and output.");
return labelImage(srcImageRange(src),
destImage(dest), eight_neighbors, equal);
}
template <class T1, class S1,
class T2, class S2>
inline unsigned int
labelImage(MultiArrayView<2, T1, S1> const & src,
MultiArrayView<2, T2, S2> dest,
bool eight_neighbors)
{
return labelImage(srcImageRange(src),
destImage(dest), eight_neighbors,
std::equal_to<T1>());
}
/********************************************************/
/* */
/* labelImageWithBackground */
/* */
/********************************************************/
/** \brief Find the connected components of a segmented image,
excluding the background from labeling.
This function works like \ref labelImage(), but considers all background pixels
(i.e. pixels having the given '<TT>background_value</TT>') as a single region that
is ignored when determining connected components and remains untouched in the
destination image. Usually, you will zero-initialize the output image, so that
the background gets label 0 (remember that actual region labels start at one).
Return: the number of non-background regions found (= largest region label)
See \ref labelMultiArrayWithBackground() for a dimension-independent implementation
if this algorithm.
<b> Declarations:</b>
pass 2D array views:
\code
namespace vigra {
template <class T1, class S1,
class T2, class S2,
class ValueType,
class EqualityFunctor = std::equal_to<T1> >
unsigned int
labelImageWithBackground(MultiArrayView<2, T1, S1> const & src,
MultiArrayView<2, T2, S2> dest,
bool eight_neighbors,
ValueType background_value,
EqualityFunctor equal = EqualityFunctor());
}
\endcode
\deprecatedAPI{labelImageWithBackground}
pass \ref ImageIterators and \ref DataAccessors :
\code
namespace vigra {
template <class SrcIterator, class SrcAccessor,
class DestIterator, class DestAccessor,
class ValueType>
int labelImageWithBackground(SrcIterator upperlefts,
SrcIterator lowerrights, SrcAccessor sa,
DestIterator upperleftd, DestAccessor da,
bool eight_neighbors,
ValueType background_value );
template <class SrcIterator, class SrcAccessor,
class DestIterator, class DestAccessor,
class ValueType, class EqualityFunctor>
int labelImageWithBackground(SrcIterator upperlefts,
SrcIterator lowerrights, SrcAccessor sa,
DestIterator upperleftd, DestAccessor da,
bool eight_neighbors,
ValueType background_value, EqualityFunctor equal);
}
\endcode
use argument objects in conjunction with \ref ArgumentObjectFactories :
\code
namespace vigra {
template <class SrcIterator, class SrcAccessor,
class DestIterator, class DestAccessor,
class ValueType>
int labelImageWithBackground(triple<SrcIterator, SrcIterator, SrcAccessor> src,
pair<DestIterator, DestAccessor> dest,
bool eight_neighbors,
ValueType background_value);
template <class SrcIterator, class SrcAccessor,
class DestIterator, class DestAccessor,
class ValueType, class EqualityFunctor>
int labelImageWithBackground(triple<SrcIterator, SrcIterator, SrcAccessor> src,
pair<DestIterator, DestAccessor> dest,
bool eight_neighbors,
ValueType background_value, EqualityFunctor equal);
}
\endcode
\deprecatedEnd
<b> Usage:</b>
<b>\#include</b> \<vigra/labelimage.hxx\><br>
Namespace: vigra
\code
MultiArray<2, unsigned char> src(w,h);
MultiArray<2, unsigned int> labels(w,h);
// threshold at 128
transformImage(src, src, Threshold<int, int>(128, 256, 0, 255));
// find 4-connected regions of foreground (= white pixels) only
labelImageWithBackground(src, labels, false, 0);
\endcode
\deprecatedUsage{labelImageWithBackground}
\code
vigra::BImage src(w,h);
vigra::IImage labels(w,h);
// threshold at 128
vigra::transformImage(srcImageRange(src), destImage(src),
vigra::Threshold<vigra::BImage::PixelType, vigra::BImage::PixelType>(
128, 256, 0, 255));
// find 4-connected regions of foreground (= white pixels) only
vigra::labelImageWithBackground(srcImageRange(src), destImage(labels),
false, 0);
\endcode
<b> Required Interface:</b>
\code
SrcImageIterator src_upperleft, src_lowerright;
DestImageIterator dest_upperleft;
SrcAccessor src_accessor;
DestAccessor dest_accessor;
SrcAccessor::value_type u = src_accessor(src_upperleft);
ValueType background_value;
u == u // first form
u == background_value // first form
EqualityFunctor equal; // second form
equal(u, u) // second form
equal(u, background_value) // second form
int i;
dest_accessor.set(i, dest_upperleft);
\endcode
\deprecatedEnd
*/
doxygen_overloaded_function(template <...> unsigned int labelImageWithBackground)
template <class SrcIterator, class SrcAccessor,
class DestIterator, class DestAccessor,
class ValueType, class EqualityFunctor>
unsigned int labelImageWithBackground(
SrcIterator upperlefts,
SrcIterator lowerrights, SrcAccessor sa,
DestIterator upperleftd, DestAccessor da,
bool eight_neighbors,
ValueType background_value, EqualityFunctor equal)
{
int w = lowerrights.x - upperlefts.x;
int h = lowerrights.y - upperlefts.y;
int x,y,i;
const Diff2D neighbor[] = {
Diff2D(-1,0), // left
Diff2D(-1,-1), // topleft
Diff2D(0,-1), // top
Diff2D(1,-1) // topright
};
const int left = 0, /* unused: topleft = 1,*/ top = 2, topright = 3;
int step = eight_neighbors ? 1 : 2;
SrcIterator ys(upperlefts);
SrcIterator xs(ys);
// temporary image to store region labels
typedef BasicImage<IntBiggest> TmpImage;
TmpImage labelimage(w, h);
TmpImage::ScanOrderIterator label = labelimage.begin();
TmpImage::Iterator yt = labelimage.upperLeft();
TmpImage::Iterator xt(yt);
// pass 1: scan image from upper left to lower right
// find connected components
for(y = 0; y != h; ++y, ++ys.y, ++yt.y)
{
xs = ys;
xt = yt;
int endNeighbor = (y == 0) ? left : (eight_neighbors ? topright : top);
for(x = 0; x != w; ++x, ++xs.x, ++xt.x)
{
if(equal(sa(xs), background_value))
{
*xt = -1;
}
else
{
int beginNeighbor = (x == 0) ? top : left;
if(x == w-1 && endNeighbor == topright) endNeighbor = top;
for(i=beginNeighbor; i<=endNeighbor; i+=step)
{
if(equal(sa(xs), sa(xs, neighbor[i])))
{
IntBiggest neighborLabel = xt[neighbor[i]];
for(int j=i+2; j<=endNeighbor; j+=step)
{
if(equal(sa(xs), sa(xs, neighbor[j])))
{
IntBiggest neighborLabel1 = xt[neighbor[j]];
if(neighborLabel != neighborLabel1)
{
// find roots of the region trees
while(neighborLabel != label[neighborLabel])
{
neighborLabel = label[neighborLabel];
}
while(neighborLabel1 != label[neighborLabel1])
{
neighborLabel1 = label[neighborLabel1];
}
// merge the trees
if(neighborLabel1 < neighborLabel)
{
label[neighborLabel] = neighborLabel1;
neighborLabel = neighborLabel1;
}
else if(neighborLabel < neighborLabel1)
{
label[neighborLabel1] = neighborLabel;
}
}
break;
}
}
*xt = neighborLabel;
break;
}
}
if(i > endNeighbor)
{
// new region
// The initial label of a new region equals the
// scan order address of it's first pixel.
// This is essential for correct operation of the algorithm.
*xt = x + y*w;
}
}
}
}
// pass 2: assign contiguous labels to the regions
DestIterator yd(upperleftd);
int count = 0;
i = 0;
for(y=0; y != h; ++y, ++yd.y)
{
DestIterator xd(yd);
for(x = 0; x != w; ++x, ++xd.x, ++i)
{
if(label[i] == -1) continue;
if(label[i] == i)
{
label[i] = count++;
}
else
{
label[i] = label[label[i]];
}
da.set(label[i]+1, xd);
}
}
return count;
}
template <class SrcIterator, class SrcAccessor,
class DestIterator, class DestAccessor,
class ValueType>
inline
unsigned int labelImageWithBackground(
SrcIterator upperlefts,
SrcIterator lowerrights, SrcAccessor sa,
DestIterator upperleftd, DestAccessor da,
bool eight_neighbors,
ValueType background_value)
{
return labelImageWithBackground(upperlefts, lowerrights, sa,
upperleftd, da,
eight_neighbors, background_value,
std::equal_to<typename SrcAccessor::value_type>());
}
template <class SrcIterator, class SrcAccessor,
class DestIterator, class DestAccessor,
class ValueType, class EqualityFunctor>
inline unsigned int
labelImageWithBackground(triple<SrcIterator, SrcIterator, SrcAccessor> src,
pair<DestIterator, DestAccessor> dest,
bool eight_neighbors,
ValueType background_value, EqualityFunctor equal)
{
return labelImageWithBackground(src.first, src.second, src.third,
dest.first, dest.second,
eight_neighbors, background_value, equal);
}
template <class SrcIterator, class SrcAccessor,
class DestIterator, class DestAccessor,
class ValueType>
inline unsigned int
labelImageWithBackground(triple<SrcIterator, SrcIterator, SrcAccessor> src,
pair<DestIterator, DestAccessor> dest,
bool eight_neighbors,
ValueType background_value)
{
return labelImageWithBackground(src.first, src.second, src.third,
dest.first, dest.second,
eight_neighbors, background_value,
std::equal_to<typename SrcAccessor::value_type>());
}
template <class T1, class S1,
class T2, class S2,
class ValueType, class EqualityFunctor>
inline unsigned int
labelImageWithBackground(MultiArrayView<2, T1, S1> const & src,
MultiArrayView<2, T2, S2> dest,
bool eight_neighbors,
ValueType background_value, EqualityFunctor equal)
{
vigra_precondition(src.shape() == dest.shape(),
"labelImageWithBackground(): shape mismatch between input and output.");
return labelImageWithBackground(srcImageRange(src),
destImage(dest),
eight_neighbors, background_value, equal);
}
template <class T1, class S1,
class T2, class S2,
class ValueType>
inline unsigned int
labelImageWithBackground(MultiArrayView<2, T1, S1> const & src,
MultiArrayView<2, T2, S2> dest,
bool eight_neighbors,
ValueType background_value)
{
vigra_precondition(src.shape() == dest.shape(),
"labelImageWithBackground(): shape mismatch between input and output.");
return labelImageWithBackground(srcImageRange(src),
destImage(dest),
eight_neighbors, background_value,
std::equal_to<T1>());
}
/********************************************************/
/* */
/* regionImageToCrackEdgeImage */
/* */
/********************************************************/
/** \brief Transform a labeled image into a crack edge (interpixel edge) image.
<b> Declarations:</b>
pass 2D array views:
\code
namespace vigra {
template <class T1, class S1,
class T2, class S2, class DestValue>
void
regionImageToCrackEdgeImage(MultiArrayView<2, T1, S1> const & src,
MultiArrayView<2, T2, S2> dest,
DestValue edge_marker);
}
\endcode
\deprecatedAPI{regionImageToCrackEdgeImage}
pass \ref ImageIterators and \ref DataAccessors :
\code
namespace vigra {
template <class SrcIterator, class SrcAccessor,
class DestIterator, class DestAccessor, class DestValue>
void regionImageToCrackEdgeImage(
SrcIterator sul, SrcIterator slr, SrcAccessor sa,
DestIterator dul, DestAccessor da,
DestValue edge_marker)
}
\endcode
use argument objects in conjunction with \ref ArgumentObjectFactories :
\code
namespace vigra {
template <class SrcIterator, class SrcAccessor,
class DestIterator, class DestAccessor, class DestValue>
void regionImageToCrackEdgeImage(
triple<SrcIterator, SrcIterator, SrcAccessor> src,
pair<DestIterator, DestAccessor> dest,
DestValue edge_marker)
}
\endcode
\deprecatedEnd
This algorithm inserts border pixels (so called "crack edges" or "interpixel edges")
between regions in a labeled image like this (<TT>a</TT> and
<TT>c</TT> are the original labels, and <TT>0</TT> is the value of
<TT>edge_marker</TT> and denotes the inserted edges):
\code
original image insert zero- and one-cells
a 0 c c c
a c c a 0 0 0 c
a a c => a a a 0 c
a a a a a a 0 0
a a a a a
\endcode
The algorithm assumes that the original labeled image contains
no background. Therefore, it is suitable as a post-processing
operation of \ref labelImage() or \ref seededRegionGrowing().
The destination image must be twice the size of the original
(precisely, <TT>(2*w-1)</TT> by <TT>(2*h-1)</TT> pixels). The
source value type (<TT>SrcAccessor::value-type</TT>) must be
equality-comparable.
<b> Usage:</b>
<b>\#include</b> \<vigra/labelimage.hxx\><br>
Namespace: vigra
\code
MultiArray<2, unsigned char> src(w,h);
MultiArray<2, unsigned int> labels(w,h),
cellgrid(2*w-1, 2*h-1);
// threshold at 128
transformImage(src, src, Threshold<int, int>(128, 256, 0, 255));
// find 4-connected regions
labelImage(src, labels, false);
// create cell grid image, mark edges with 0
regionImageToCrackEdgeImage(labels, cellgrid, 0);
\endcode
\deprecatedUsage{regionImageToCrackEdgeImage}
\code
vigra::BImage src(w,h);
vigra::IImage labels(w,h);
vigra::IImage cellgrid(2*w-1, 2*h-1);
// threshold at 128
vigra::transformImage(srcImageRange(src), destImage(src),
vigra::Threshold<vigra::BImage::PixelType, vigra::BImage::PixelType>(
128, 256, 0, 255));
// find 4-connected regions
vigra::labelImage(srcImageRange(src), destImage(labels), false);
// create cell grid image, mark edges with 0
vigra::regionImageToCrackEdgeImage(srcImageRange(labels), destImage(cellgrid), 0);
\endcode
<b> Required Interface:</b>
\code
ImageIterator src_upperleft, src_lowerright;
ImageIterator dest_upperleft;
SrcAccessor src_accessor;
DestAccessor dest_accessor;
SrcAccessor::value_type u = src_accessor(src_upperleft);
u != u
DestValue edge_marker;
dest_accessor.set(edge_marker, dest_upperleft);
\endcode
\deprecatedEnd
<b> Preconditions:</b>
The destination image must have twice the size of the source:
\code
w_dest = 2 * w_src - 1
h_dest = 2 * h_src - 1
\endcode
*/
doxygen_overloaded_function(template <...> void regionImageToCrackEdgeImage)
template <class SrcIterator, class SrcAccessor,
class DestIterator, class DestAccessor, class DestValue>
void regionImageToCrackEdgeImage(
SrcIterator sul, SrcIterator slr, SrcAccessor sa,
DestIterator dul, DestAccessor da,
DestValue edge_marker)
{
int w = slr.x - sul.x;
int h = slr.y - sul.y;
int x,y;
const Diff2D right(1,0);
const Diff2D left(-1,0);
const Diff2D bottomright(1,1);
const Diff2D bottom(0,1);
const Diff2D top(0,-1);
SrcIterator iy = sul;
DestIterator dy = dul;
for(y=0; y<h-1; ++y, ++iy.y, dy.y+=2)
{
SrcIterator ix = iy;
DestIterator dx = dy;
for(x=0; x<w-1; ++x, ++ix.x, dx.x+=2)
{
da.set(sa(ix), dx);
da.set(sa(ix), dx, bottomright);
if(sa(ix, right) != sa(ix))
{
da.set(edge_marker, dx, right);
}
else
{
da.set(sa(ix), dx, right);
}
if(sa(ix, bottom) != sa(ix))
{
da.set(edge_marker, dx, bottom);
}
else
{
da.set(sa(ix), dx, bottom);
}
}
da.set(sa(ix), dx);
if(sa(ix, bottom) != sa(ix))
{
da.set(edge_marker, dx, bottom);
}
else
{
da.set(sa(ix), dx, bottom);
}
}
SrcIterator ix = iy;
DestIterator dx = dy;
for(x=0; x<w-1; ++x, ++ix.x, dx.x+=2)
{
da.set(sa(ix), dx);
if(sa(ix, right) != sa(ix))
{
da.set(edge_marker, dx, right);
}
else
{
da.set(sa(ix), dx, right);
}
}
da.set(sa(ix), dx);
dy = dul + Diff2D(1,1);
const Diff2D dist[] = {right, top, left, bottom };
// find missing 0-cells
for(y=0; y<h-1; ++y, dy.y+=2)
{
DestIterator dx = dy;
for(x=0; x<w-1; ++x, dx.x+=2)
{
int i;
for(i=0; i<4; ++i)
{
if(da(dx, dist[i]) == edge_marker) break;
}
if(i < 4) da.set(edge_marker, dx);
}
}
}
template <class SrcIterator, class SrcAccessor,
class DestIterator, class DestAccessor, class DestValue>
inline void
regionImageToCrackEdgeImage(triple<SrcIterator, SrcIterator, SrcAccessor> src,
pair<DestIterator, DestAccessor> dest,
DestValue edge_marker)
{
regionImageToCrackEdgeImage(src.first, src.second, src.third,
dest.first, dest.second,
edge_marker);
}
template <class T1, class S1,
class T2, class S2, class DestValue>
inline void
regionImageToCrackEdgeImage(MultiArrayView<2, T1, S1> const & src,
MultiArrayView<2, T2, S2> dest,
DestValue edge_marker)
{
vigra_precondition(2*src.shape()-Shape2(1) == dest.shape(),
"regionImageToCrackEdgeImage(): shape mismatch between input and output.");
regionImageToCrackEdgeImage(srcImageRange(src),
destImage(dest),
edge_marker);
}
/********************************************************/
/* */
/* regionImageToEdgeImage */
/* */
/********************************************************/
/** \brief Transform a labeled image into an edge image.
<b> Declarations:</b>
pass 2D array views:
\code
namespace vigra {
template <class T1, class S1,
class T2, class S2, class DestValue>
void
regionImageToEdgeImage(MultiArrayView<2, T1, S1> const & src,
MultiArrayView<2, T2, S2> dest,
DestValue edge_marker);
}
\endcode
\deprecatedAPI{regionImageToEdgeImage}
pass \ref ImageIterators and \ref DataAccessors :
\code
namespace vigra {
template <class SrcIterator, class SrcAccessor,
class DestIterator, class DestAccessor, class DestValue>
void regionImageToEdgeImage(
SrcIterator sul, SrcIterator slr, SrcAccessor sa,
DestIterator dul, DestAccessor da,
DestValue edge_marker)
}
\endcode
use argument objects in conjunction with \ref ArgumentObjectFactories :
\code
namespace vigra {
template <class SrcIterator, class SrcAccessor,
class DestIterator, class DestAccessor, class DestValue>
void regionImageToEdgeImage(
triple<SrcIterator, SrcIterator, SrcAccessor> src,
pair<DestIterator, DestAccessor> dest,
DestValue edge_marker)
}
\endcode
\deprecatedEnd
This algorithm marks all pixels with the given <TT>edge_marker</TT>
which belong to a different region (label) than their right or lower
neighbors:
\code
original image edges
(assuming edge_marker == 1)
a c c 1 1 *
a a c => * 1 1
a a a * * *
\endcode
The non-edge pixels of the destination image will not be touched.
The source value type <TT>T1</TT> must be
equality-comparable.
<b> Usage:</b>
<b>\#include</b> \<vigra/labelimage.hxx\><br>
Namespace: vigra
\code
MultiArray<2, unsigned char> src(w,h),
edges(w,h);
MultiArray<2, unsigned int> labels(w,h);
edges = 255; // init background (non-edge) to 255
// threshold at 128
transformImage(src, src, Threshold<int, int>(128, 256, 0, 255));
// find 4-connected regions
labelImage(src, labels, false);
// create edge image, mark edges with 0
regionImageToEdgeImage(labels, edges, 0);
\endcode
\deprecatedUsage{regionImageToEdgeImage}
\code
vigra::BImage src(w,h);
vigra::IImage labels(w,h);
vigra::IImage edges(w, h);
edges = 255; // init background (non-edge) to 255
// threshold at 128
vigra::transformImage(srcImageRange(src), destImage(src),
vigra::Threshold<vigra::BImage::PixelType, vigra::BImage::PixelType>(
128, 256, 0, 255));
// find 4-connected regions
vigra::labelImage(srcImageRange(src), destImage(labels), false);
// create edge image, mark edges with 0
vigra::regionImageToEdgeImage(srcImageRange(labels), destImage(edges), 0);
\endcode
<b> Required Interface:</b>
\code
ImageIterator src_upperleft, src_lowerright;
ImageIterator dest_upperleft;
SrcAccessor src_accessor;
DestAccessor dest_accessor;
SrcAccessor::value_type u = src_accessor(src_upperleft);
u != u
DestValue edge_marker;
dest_accessor.set(edge_marker, dest_upperleft);
\endcode
\deprecatedEnd
*/
doxygen_overloaded_function(template <...> void regionImageToEdgeImage)
template <class SrcIterator, class SrcAccessor,
class DestIterator, class DestAccessor, class DestValue>
void regionImageToEdgeImage(
SrcIterator sul, SrcIterator slr, SrcAccessor sa,
DestIterator dul, DestAccessor da,
DestValue edge_marker)
{
int w = slr.x - sul.x;
int h = slr.y - sul.y;
int x,y;
const Diff2D right(1,0);
const Diff2D left(-1,0);
const Diff2D bottomright(1,1);
const Diff2D bottom(0,1);
const Diff2D top(0,-1);
SrcIterator iy = sul;
DestIterator dy = dul;
for(y=0; y<h-1; ++y, ++iy.y, ++dy.y)
{
SrcIterator ix = iy;
DestIterator dx = dy;
for(x=0; x<w-1; ++x, ++ix.x, ++dx.x)
{
if(sa(ix, right) != sa(ix))
{
da.set(edge_marker, dx);
}
if(sa(ix, bottom) != sa(ix))
{
da.set(edge_marker, dx);
}
}
if(sa(ix, bottom) != sa(ix))
{
da.set(edge_marker, dx);
}
}
SrcIterator ix = iy;
DestIterator dx = dy;
for(x=0; x<w-1; ++x, ++ix.x, ++dx.x)
{
if(sa(ix, right) != sa(ix))
{
da.set(edge_marker, dx);
}
}
}
template <class SrcIterator, class SrcAccessor,
class DestIterator, class DestAccessor, class DestValue>
inline void
regionImageToEdgeImage(triple<SrcIterator, SrcIterator, SrcAccessor> src,
pair<DestIterator, DestAccessor> dest,
DestValue edge_marker)
{
regionImageToEdgeImage(src.first, src.second, src.third,
dest.first, dest.second,
edge_marker);
}
template <class T1, class S1,
class T2, class S2, class DestValue>
inline void
regionImageToEdgeImage(MultiArrayView<2, T1, S1> const & src,
MultiArrayView<2, T2, S2> dest,
DestValue edge_marker)
{
vigra_precondition(src.shape() == dest.shape(),
"regionImageToEdgeImage(): shape mismatch between input and output.");
regionImageToEdgeImage(srcImageRange(src),
destImage(dest),
edge_marker);
}
//@}
} // namespace vigra
#endif // VIGRA_LABELIMAGE_HXX
|