/usr/include/vigra/watersheds.hxx is in libvigraimpex-dev 1.7.1+dfsg1-2ubuntu4.
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 | /************************************************************************/
/* */
/* Copyright 1998-2005 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_WATERSHEDS_HXX
#define VIGRA_WATERSHEDS_HXX
#include <functional>
#include "mathutil.hxx"
#include "stdimage.hxx"
#include "pixelneighborhood.hxx"
#include "union_find.hxx"
namespace vigra {
template <class SrcIterator, class SrcAccessor,
class DestIterator, class DestAccessor,
class Neighborhood>
unsigned int watershedLabeling(SrcIterator upperlefts,
SrcIterator lowerrights, SrcAccessor sa,
DestIterator upperleftd, DestAccessor da,
Neighborhood)
{
typedef typename DestAccessor::value_type LabelType;
int w = lowerrights.x - upperlefts.x;
int h = lowerrights.y - upperlefts.y;
int x,y;
SrcIterator ys(upperlefts);
SrcIterator xs(ys);
DestIterator yd(upperleftd);
DestIterator xd(yd);
// temporary image to store region labels
detail::UnionFindArray<LabelType> labels;
// initialize the neighborhood circulators
NeighborOffsetCirculator<Neighborhood> ncstart(Neighborhood::CausalFirst);
NeighborOffsetCirculator<Neighborhood> ncstartBorder(Neighborhood::North);
NeighborOffsetCirculator<Neighborhood> ncend(Neighborhood::CausalLast);
++ncend;
NeighborOffsetCirculator<Neighborhood> ncendBorder(Neighborhood::North);
++ncendBorder;
// 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
da.set(labels.finalizeLabel(labels.nextFreeLabel()), xd);
++xs.x;
++xd.x;
for(x = 1; x != w; ++x, ++xs.x, ++xd.x)
{
if((sa(xs) & Neighborhood::directionBit(Neighborhood::West)) ||
(sa(xs, Neighborhood::west()) & Neighborhood::directionBit(Neighborhood::East)))
{
da.set(da(xd, Neighborhood::west()), xd);
}
else
{
da.set(labels.finalizeLabel(labels.nextFreeLabel()), xd);
}
}
++ys.y;
++yd.y;
for(y = 1; y != h; ++y, ++ys.y, ++yd.y)
{
xs = ys;
xd = yd;
for(x = 0; x != w; ++x, ++xs.x, ++xd.x)
{
NeighborOffsetCirculator<Neighborhood> nc(x == w-1
? ncstartBorder
: ncstart);
NeighborOffsetCirculator<Neighborhood> nce(x == 0
? ncendBorder
: ncend);
LabelType currentLabel = labels.nextFreeLabel();
for(; nc != nce; ++nc)
{
if((sa(xs) & nc.directionBit()) || (sa(xs, *nc) & nc.oppositeDirectionBit()))
{
currentLabel = labels.makeUnion(da(xd,*nc), currentLabel);
}
}
da.set(labels.finalizeLabel(currentLabel), xd);
}
}
unsigned int count = labels.makeContiguous();
// pass 2: assign one label to each region (tree)
// so that labels form a consecutive sequence 1, 2, ...
yd = upperleftd;
for(y=0; y != h; ++y, ++yd.y)
{
DestIterator xd(yd);
for(x = 0; x != w; ++x, ++xd.x)
{
da.set(labels[da(xd)], xd);
}
}
return count;
}
template <class SrcIterator, class SrcAccessor,
class DestIterator, class DestAccessor,
class Neighborhood>
unsigned int watershedLabeling(triple<SrcIterator, SrcIterator, SrcAccessor> src,
pair<DestIterator, DestAccessor> dest,
Neighborhood neighborhood)
{
return watershedLabeling(src.first, src.second, src.third,
dest.first, dest.second, neighborhood);
}
template <class SrcIterator, class SrcAccessor,
class DestIterator, class DestAccessor>
void prepareWatersheds(SrcIterator upperlefts, SrcIterator lowerrights, SrcAccessor sa,
DestIterator upperleftd, DestAccessor da,
FourNeighborCode)
{
int w = lowerrights.x - upperlefts.x;
int h = lowerrights.y - upperlefts.y;
int x,y;
SrcIterator ys(upperlefts);
SrcIterator xs(ys);
DestIterator yd = upperleftd;
for(y = 0; y != h; ++y, ++ys.y, ++yd.y)
{
xs = ys;
DestIterator xd = yd;
for(x = 0; x != w; ++x, ++xs.x, ++xd.x)
{
AtImageBorder atBorder = isAtImageBorder(x,y,w,h);
typename SrcAccessor::value_type v = sa(xs);
// the following choice causes minima to point
// to their lowest neighbor -- would this be better???
// typename SrcAccessor::value_type v = NumericTraits<typename SrcAccessor::value_type>::max();
int o = 0; // means center is minimum
if(atBorder == NotAtBorder)
{
NeighborhoodCirculator<SrcIterator, FourNeighborCode> c(xs), cend(c);
do {
if(sa(c) <= v)
{
v = sa(c);
o = c.directionBit();
}
}
while(++c != cend);
}
else
{
RestrictedNeighborhoodCirculator<SrcIterator, FourNeighborCode> c(xs, atBorder), cend(c);
do {
if(sa(c) <= v)
{
v = sa(c);
o = c.directionBit();
}
}
while(++c != cend);
}
da.set(o, xd);
}
}
}
template <class SrcIterator, class SrcAccessor,
class DestIterator, class DestAccessor>
void prepareWatersheds(SrcIterator upperlefts, SrcIterator lowerrights, SrcAccessor sa,
DestIterator upperleftd, DestAccessor da,
EightNeighborCode)
{
int w = lowerrights.x - upperlefts.x;
int h = lowerrights.y - upperlefts.y;
int x,y;
SrcIterator ys(upperlefts);
SrcIterator xs(ys);
DestIterator yd = upperleftd;
for(y = 0; y != h; ++y, ++ys.y, ++yd.y)
{
xs = ys;
DestIterator xd = yd;
for(x = 0; x != w; ++x, ++xs.x, ++xd.x)
{
AtImageBorder atBorder = isAtImageBorder(x,y,w,h);
typename SrcAccessor::value_type v = sa(xs);
// the following choice causes minima to point
// to their lowest neighbor -- would this be better???
// typename SrcAccessor::value_type v = NumericTraits<typename SrcAccessor::value_type>::max();
int o = 0; // means center is minimum
if(atBorder == NotAtBorder)
{
// handle diagonal and principal neighbors separately
// so that principal neighbors are preferred when there are
// candidates with equal strength
NeighborhoodCirculator<SrcIterator, EightNeighborCode>
c(xs, EightNeighborCode::NorthEast);
for(int i = 0; i < 4; ++i, c += 2)
{
if(sa(c) <= v)
{
v = sa(c);
o = c.directionBit();
}
}
--c;
for(int i = 0; i < 4; ++i, c += 2)
{
if(sa(c) <= v)
{
v = sa(c);
o = c.directionBit();
}
}
}
else
{
RestrictedNeighborhoodCirculator<SrcIterator, EightNeighborCode>
c(xs, atBorder), cend(c);
do
{
if(!c.isDiagonal())
continue;
if(sa(c) <= v)
{
v = sa(c);
o = c.directionBit();
}
}
while(++c != cend);
do
{
if(c.isDiagonal())
continue;
if(sa(c) <= v)
{
v = sa(c);
o = c.directionBit();
}
}
while(++c != cend);
}
da.set(o, xd);
}
}
}
/** \addtogroup SeededRegionGrowing Region Segmentation Algorithms
Region growing, watersheds, and voronoi tesselation
*/
//@{
/********************************************************/
/* */
/* watersheds */
/* */
/********************************************************/
/** \brief Region Segmentation by means of the watershed algorithm.
This function implements the union-find version of the watershed algorithms
as described in
J. Roerdink, R. Meijster: "<em>The watershed transform: definitions, algorithms,
and parallelization stretegies</em>", Fundamenta Informaticae, 41:187-228, 2000
The source image is a boundary indicator such as the gradient magnitude
of the trace of the \ref boundaryTensor(). Local minima of the boundary indicator
are used as region seeds, and all other pixels are recursively assigned to the same
region as their lowest neighbor. Pass \ref vigra::EightNeighborCode or
\ref vigra::FourNeighborCode to determine the neighborhood where pixel values
are compared. The pixel type of the input image must be <tt>LessThanComparable</tt>.
The function uses accessors.
Note that VIGRA provides an alternative implementaion of the watershed transform via
\ref seededRegionGrowing(). It is slower, but handles plateaus better
and allows to keep a one pixel wide boundary between regions.
<b> Declarations:</b>
pass arguments explicitly:
\code
namespace vigra {
template <class SrcIterator, class SrcAccessor,
class DestIterator, class DestAccessor,
class Neighborhood = EightNeighborCode>
unsigned int
watersheds(SrcIterator upperlefts, SrcIterator lowerrights, SrcAccessor sa,
DestIterator upperleftd, DestAccessor da,
Neighborhood neighborhood = EightNeighborCode())
}
\endcode
use argument objects in conjunction with \ref ArgumentObjectFactories :
\code
namespace vigra {
template <class SrcIterator, class SrcAccessor,
class DestIterator, class DestAccessor,
class Neighborhood = EightNeighborCode>
unsigned int
watersheds(triple<SrcIterator, SrcIterator, SrcAccessor> src,
pair<DestIterator, DestAccessor> dest,
Neighborhood neighborhood = EightNeighborCode())
}
\endcode
<b> Usage:</b>
<b>\#include</b> \<<a href="watersheds_8hxx-source.html">vigra/watersheds.hxx</a>\><br>
Namespace: vigra
Example: watersheds of the gradient magnitude.
\code
vigra::BImage in(w,h);
... // read input data
vigra::FImage gradx(x,y), grady(x,y), gradMag(x,y);
gaussianGradient(srcImageRange(src), destImage(gradx), destImage(grady), 3.0);
combineTwoImages(srcImageRange(gradx), srcImage(grady), destImage(gradMag),
vigra::MagnitudeFunctor<float>());
// the pixel type of the destination image must be large enough to hold
// numbers up to 'max_region_label' to prevent overflow
vigra::IImage labeling(x,y);
int max_region_label = watersheds(srcImageRange(gradMag), destImage(labeling));
\endcode
<b> Required Interface:</b>
\code
SrcImageIterator src_upperleft, src_lowerright;
DestImageIterator dest_upperleft;
SrcAccessor src_accessor;
DestAccessor dest_accessor;
// compare src values
src_accessor(src_upperleft) <= src_accessor(src_upperleft)
// set result
int label;
dest_accessor.set(label, dest_upperleft);
\endcode
*/
doxygen_overloaded_function(template <...> unsigned int watersheds)
template <class SrcIterator, class SrcAccessor,
class DestIterator, class DestAccessor,
class Neighborhood>
unsigned int
watersheds(SrcIterator upperlefts, SrcIterator lowerrights, SrcAccessor sa,
DestIterator upperleftd, DestAccessor da, Neighborhood neighborhood)
{
SImage orientationImage(lowerrights - upperlefts);
SImage::traverser yo = orientationImage.upperLeft();
prepareWatersheds(upperlefts, lowerrights, sa,
orientationImage.upperLeft(), orientationImage.accessor(), neighborhood);
return watershedLabeling(orientationImage.upperLeft(), orientationImage.lowerRight(), orientationImage.accessor(),
upperleftd, da, neighborhood);
}
template <class SrcIterator, class SrcAccessor,
class DestIterator, class DestAccessor>
inline unsigned int
watersheds(SrcIterator upperlefts, SrcIterator lowerrights, SrcAccessor sa,
DestIterator upperleftd, DestAccessor da)
{
return watersheds(upperlefts, lowerrights, sa, upperleftd, da, EightNeighborCode());
}
template <class SrcIterator, class SrcAccessor,
class DestIterator, class DestAccessor,
class Neighborhood>
inline unsigned int
watersheds(triple<SrcIterator, SrcIterator, SrcAccessor> src,
pair<DestIterator, DestAccessor> dest, Neighborhood neighborhood)
{
return watersheds(src.first, src.second, src.third, dest.first, dest.second, neighborhood);
}
template <class SrcIterator, class SrcAccessor,
class DestIterator, class DestAccessor>
inline unsigned int
watersheds(triple<SrcIterator, SrcIterator, SrcAccessor> src,
pair<DestIterator, DestAccessor> dest)
{
return watersheds(src.first, src.second, src.third, dest.first, dest.second);
}
//@}
} // namespace vigra
#endif // VIGRA_WATERSHEDS_HXX
|