[ VIGRA Homepage | Function Index | Class Index | Namespaces | File List | Main Page ]

watersheds3d.hxx
1 /************************************************************************/
2 /* */
3 /* Copyright 2006-2007 by F. Heinrich, B. Seppke, Ullrich Koethe */
4 /* */
5 /* This file is part of the VIGRA computer vision library. */
6 /* The VIGRA Website is */
7 /* http://hci.iwr.uni-heidelberg.de/vigra/ */
8 /* Please direct questions, bug reports, and contributions to */
9 /* ullrich.koethe@iwr.uni-heidelberg.de or */
10 /* vigra@informatik.uni-hamburg.de */
11 /* */
12 /* Permission is hereby granted, free of charge, to any person */
13 /* obtaining a copy of this software and associated documentation */
14 /* files (the "Software"), to deal in the Software without */
15 /* restriction, including without limitation the rights to use, */
16 /* copy, modify, merge, publish, distribute, sublicense, and/or */
17 /* sell copies of the Software, and to permit persons to whom the */
18 /* Software is furnished to do so, subject to the following */
19 /* conditions: */
20 /* */
21 /* The above copyright notice and this permission notice shall be */
22 /* included in all copies or substantial portions of the */
23 /* Software. */
24 /* */
25 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND */
26 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES */
27 /* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */
28 /* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT */
29 /* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */
30 /* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING */
31 /* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR */
32 /* OTHER DEALINGS IN THE SOFTWARE. */
33 /* */
34 /************************************************************************/
35 
36 #ifndef VIGRA_watersheds3D_HXX
37 #define VIGRA_watersheds3D_HXX
38 
39 #include "voxelneighborhood.hxx"
40 #include "multi_array.hxx"
41 #include "union_find.hxx"
42 
43 namespace vigra
44 {
45 
46 template <class SrcIterator, class SrcAccessor, class SrcShape,
47  class DestIterator, class DestAccessor, class Neighborhood3D>
48 int preparewatersheds3D( SrcIterator s_Iter, SrcShape srcShape, SrcAccessor sa,
49  DestIterator d_Iter, DestAccessor da, Neighborhood3D)
50 {
51  //basically needed for iteration and border-checks
52  int w = srcShape[0], h = srcShape[1], d = srcShape[2];
53  int x,y,z, local_min_count=0;
54 
55  //declare and define Iterators for all three dims at src
56  SrcIterator zs = s_Iter;
57  SrcIterator ys(zs);
58  SrcIterator xs(ys);
59 
60  //Declare Iterators for all three dims at dest
61  DestIterator zd = d_Iter;
62 
63  for(z = 0; z != d; ++z, ++zs.dim2(), ++zd.dim2())
64  {
65  ys = zs;
66  DestIterator yd(zd);
67 
68  for(y = 0; y != h; ++y, ++ys.dim1(), ++yd.dim1())
69  {
70  xs = ys;
71  DestIterator xd(yd);
72 
73  for(x = 0; x != w; ++x, ++xs.dim0(), ++xd.dim0())
74  {
75  AtVolumeBorder atBorder = isAtVolumeBorder(x,y,z,w,h,d);
76  typename SrcAccessor::value_type v = sa(xs);
77  // the following choice causes minima to point
78  // to their lowest neighbor -- would this be better???
79  // typename SrcAccessor::value_type v = NumericTraits<typename SrcAccessor::value_type>::max();
80  int o = 0; // means center is minimum
81  typename SrcAccessor::value_type my_v = v;
82  if(atBorder == NotAtBorder)
83  {
84  NeighborhoodCirculator<SrcIterator, Neighborhood3D> c(xs), cend(c);
85 
86  do {
87  if(sa(c) < v)
88  {
89  v = sa(c);
90  o = c.directionBit();
91  }
92  else if(sa(c) == my_v && my_v == v)
93  {
94  o = o | c.directionBit();
95  }
96  }
97  while(++c != cend);
98  }
99  else
100  {
101  RestrictedNeighborhoodCirculator<SrcIterator, Neighborhood3D> c(xs, atBorder), cend(c);
102  do {
103  if(sa(c) < v)
104  {
105  v = sa(c);
106  o = c.directionBit();
107  }
108  else if(sa(c) == my_v && my_v == v)
109  {
110  o = o | c.directionBit();
111  }
112  }
113  while(++c != cend);
114  }
115  if (o==0) local_min_count++;
116  da.set(o, xd);
117  }//end x-iteration
118  }//end y-iteration
119  }//end z-iteration
120  return local_min_count;
121 }
122 
123 template <class SrcIterator, class SrcAccessor,class SrcShape,
124  class DestIterator, class DestAccessor,
125  class Neighborhood3D>
126 unsigned int watershedLabeling3D( SrcIterator s_Iter, SrcShape srcShape, SrcAccessor sa,
127  DestIterator d_Iter, DestAccessor da,
128  Neighborhood3D)
129 {
130  typedef typename DestAccessor::value_type LabelType;
131 
132  //basically needed for iteration and border-checks
133  int w = srcShape[0], h = srcShape[1], d = srcShape[2];
134  int x,y,z;
135 
136  //declare and define Iterators for all three dims at src
137  SrcIterator zs = s_Iter;
138  DestIterator zd = d_Iter;
139 
140  // temporary image to store region labels
141  detail::UnionFindArray<LabelType> labels;
142 
143  // initialize the neighborhood traversers
144  NeighborOffsetCirculator<Neighborhood3D> nc(Neighborhood3D::CausalFirst);
145  NeighborOffsetCirculator<Neighborhood3D> nce(Neighborhood3D::CausalLast);
146  ++nce;
147  // pass 1: scan image from upper left front to lower right back
148  // to find connected components
149 
150  // Each component will be represented by a tree of pixels. Each
151  // pixel contains the scan order address of its parent in the
152  // tree. In order for pass 2 to work correctly, the parent must
153  // always have a smaller scan order address than the child.
154  // Therefore, we can merge trees only at their roots, because the
155  // root of the combined tree must have the smallest scan order
156  // address among all the tree's pixels/ nodes. The root of each
157  // tree is distinguished by pointing to itself (it contains its
158  // own scan order address). This condition is enforced whenever a
159  // new region is found or two regions are merged
160  for(z = 0; z != d; ++z, ++zs.dim2(), ++zd.dim2())
161  {
162  SrcIterator ys = zs;
163  DestIterator yd = zd;
164 
165  for(y = 0; y != h; ++y, ++ys.dim1(), ++yd.dim1())
166  {
167  SrcIterator xs = ys;
168  DestIterator xd = yd;
169 
170  for(x = 0; x != w; ++x, ++xs.dim0(), ++xd.dim0())
171  {
172  LabelType currentLabel = labels.nextFreeLabel(); // default: new region
173 
174  //queck whether there is a special borde threatment to be used or not
175  AtVolumeBorder atBorder = isAtVolumeBorderCausal(x,y,z,w,h,d);
176 
177  //We are not at the border!
178  if(atBorder == NotAtBorder)
179  {
180 
181  nc = NeighborOffsetCirculator<Neighborhood3D>(Neighborhood3D::CausalFirst);
182 
183  do
184  {
185  // Direction of NTraversr Neighbor's direction bit is pointing
186  // = Direction of voxel towards us?
187  if((sa(xs) & nc.directionBit()) || (sa(xs,*nc) & nc.oppositeDirectionBit()))
188  {
189  currentLabel = labels.makeUnion(da(xd,*nc), currentLabel);
190  }
191  ++nc;
192  }while(nc!=nce);
193  }
194  //we are at a border - handle this!!
195  else
196  {
197  nc = NeighborOffsetCirculator<Neighborhood3D>(Neighborhood3D::nearBorderDirectionsCausal(atBorder,0));
198  int j=0;
199  while(nc.direction() != Neighborhood3D::Error)
200  {
201  // Direction of NTraversr Neighbor's direction bit is pointing
202  // = Direction of voxel towards us?
203  if((sa(xs) & nc.directionBit()) || (sa(xs,*nc) & nc.oppositeDirectionBit()))
204  {
205  currentLabel = labels.makeUnion(da(xd,*nc), currentLabel);
206  }
207  nc.turnTo(Neighborhood3D::nearBorderDirectionsCausal(atBorder,++j));
208  }
209  }
210  da.set(labels.finalizeLabel(currentLabel), xd);
211  }
212  }
213  }
214 
215  unsigned int count = labels.makeContiguous();
216 
217  // pass 2: assign one label to each region (tree)
218  // so that labels form a consecutive sequence 1, 2, ...
219  zd = d_Iter;
220  for(z=0; z != d; ++z, ++zd.dim2())
221  {
222  DestIterator yd(zd);
223 
224  for(y=0; y != h; ++y, ++yd.dim1())
225  {
226  DestIterator xd(yd);
227 
228  for(x = 0; x != w; ++x, ++xd.dim0())
229  {
230  da.set(labels[da(xd)], xd);
231  }
232  }
233  }
234  return count;
235 }
236 
237 
238 /** \addtogroup SeededRegionGrowing Region Segmentation Algorithms
239  Region growing, watersheds, and voronoi tesselation
240 */
241 //@{
242 
243 /********************************************************/
244 /* */
245 /* watersheds3D */
246 /* */
247 /********************************************************/
248 
249 /** \brief Region Segmentation by means of the watershed algorithm.
250 
251  <b> Declarations:</b>
252 
253  pass arguments explicitly:
254  \code
255  namespace vigra {
256  template <class SrcIterator, class SrcAccessor,class SrcShape,
257  class DestIterator, class DestAccessor,
258  class Neighborhood3D>
259  unsigned int watersheds3D(SrcIterator s_Iter, SrcShape srcShape, SrcAccessor sa,
260  DestIterator d_Iter, DestAccessor da,
261  Neighborhood3D neighborhood3D);
262  }
263  \endcode
264 
265  use argument objects in conjunction with \ref ArgumentObjectFactories :
266  \code
267  namespace vigra {
268  template <class SrcIterator, class SrcAccessor,class SrcShape,
269  class DestIterator, class DestAccessor,
270  class Neighborhood3D>
271  unsigned int watersheds3D(triple<SrcIterator, SrcShape, SrcAccessor> src,
272  pair<DestIterator, DestAccessor> dest,
273  Neighborhood3D neighborhood3D);
274  }
275  \endcode
276 
277  use with 3D-Six-Neighborhood:
278  \code
279  namespace vigra {
280 
281  template <class SrcIterator, class SrcAccessor,class SrcShape,
282  class DestIterator, class DestAccessor>
283  unsigned int watersheds3DSix(triple<SrcIterator, SrcShape, SrcAccessor> src,
284  pair<DestIterator, DestAccessor> dest);
285 
286  }
287  \endcode
288 
289  use with 3D-TwentySix-Neighborhood:
290  \code
291  namespace vigra {
292 
293  template <class SrcIterator, class SrcAccessor,class SrcShape,
294  class DestIterator, class DestAccessor>
295  unsigned int watersheds3DTwentySix(triple<SrcIterator, SrcShape, SrcAccessor> src,
296  pair<DestIterator, DestAccessor> dest);
297 
298  }
299  \endcode
300 
301  This function implements the union-find version of the watershed algorithms
302  as described in
303 
304  J. Roerdink, R. Meijster: "<em>The watershed transform: definitions, algorithms,
305  and parallelization stretegies</em>", Fundamenta Informaticae, 41:187-228, 2000
306 
307  The source volume is a boundary indicator such as the gradient magnitude
308  of the trace of the \ref boundaryTensor(). Local minima of the boundary indicator
309  are used as region seeds, and all other voxels are recursively assigned to the same
310  region as their lowest neighbor. Pass \ref vigra::NeighborCode3DSix or
311  \ref vigra::NeighborCode3DTwentySix to determine the neighborhood where voxel values
312  are compared. The voxel type of the input volume must be <tt>LessThanComparable</tt>.
313  The function uses accessors.
314 
315  ...probably soon in VIGRA:
316  Note that VIGRA provides an alternative implementaion of the watershed transform via
317  \ref seededRegionGrowing3D(). It is slower, but handles plateaus better
318  and allows to keep a one pixel wide boundary between regions.
319 
320  <b> Usage:</b>
321 
322  <b>\#include</b> <<a href="watersheds3D_8hxx-source.html">vigra/watersheds3D.hxx</a>><br>
323  Namespace: vigra
324 
325  Example: watersheds3D of the gradient magnitude.
326 
327  \code
328  typedef vigra::MultiArray<3,int> IntVolume;
329  typedef vigra::MultiArray<3,double> DVolume;
330  DVolume src(DVolume::difference_type(w,h,d));
331  IntVolume dest(IntVolume::difference_type(w,h,d));
332 
333  float gauss=1;
334 
335  vigra::MultiArray<3, vigra::TinyVector<float,3> > temp(IntVolume::difference_type(w,h,d));
336  vigra::gaussianGradientMultiArray(srcMultiArrayRange(vol),destMultiArray(temp),gauss);
337 
338  IntVolume::iterator temp_iter=temp.begin();
339  for(DVolume::iterator iter=src.begin(); iter!=src.end(); ++iter, ++temp_iter)
340  *iter = norm(*temp_iter);
341 
342  // find 6-connected regions
343  int max_region_label = vigra::watersheds3DSix(srcMultiArrayRange(src), destMultiArray(dest));
344 
345  // find 26-connected regions
346  max_region_label = vigra::watersheds3DTwentySix(srcMultiArrayRange(src), destMultiArray(dest));
347 
348  \endcode
349 
350  <b> Required Interface:</b>
351 
352  \code
353  SrcIterator src_begin;
354  SrcShape src_shape;
355  DestIterator dest_begin;
356 
357  SrcAccessor src_accessor;
358  DestAccessor dest_accessor;
359 
360  // compare src values
361  src_accessor(src_begin) <= src_accessor(src_begin)
362 
363  // set result
364  int label;
365  dest_accessor.set(label, dest_begin);
366  \endcode
367 */
368 doxygen_overloaded_function(template <...> unsigned int watersheds3D)
369 
370 template <class SrcIterator, class SrcAccessor, class SrcShape,
371  class DestIterator, class DestAccessor,
372  class Neighborhood3D>
373 unsigned int watersheds3D( SrcIterator s_Iter, SrcShape srcShape, SrcAccessor sa,
374  DestIterator d_Iter, DestAccessor da, Neighborhood3D neighborhood3D)
375 {
376  //create temporary volume to store the DAG of directions to minima
377  if ((int)Neighborhood3D::DirectionCount>7){ //If we have 3D-TwentySix Neighborhood
378 
379  vigra::MultiArray<3,int> orientationVolume(srcShape);
380 
381  preparewatersheds3D( s_Iter, srcShape, sa,
382  destMultiArray(orientationVolume).first, destMultiArray(orientationVolume).second,
383  neighborhood3D);
384 
385  return watershedLabeling3D( srcMultiArray(orientationVolume).first, srcShape, srcMultiArray(orientationVolume).second,
386  d_Iter, da,
387  neighborhood3D);
388  }
389  else{
390 
391  vigra::MultiArray<3,unsigned char> orientationVolume(srcShape);
392 
393  preparewatersheds3D( s_Iter, srcShape, sa,
394  destMultiArray(orientationVolume).first, destMultiArray(orientationVolume).second,
395  neighborhood3D);
396 
397  return watershedLabeling3D( srcMultiArray(orientationVolume).first, srcShape, srcMultiArray(orientationVolume).second,
398  d_Iter, da,
399  neighborhood3D);
400  }
401 }
402 
403 template <class SrcIterator, class SrcShape, class SrcAccessor,
404  class DestIterator, class DestAccessor>
405 inline unsigned int watersheds3DSix( vigra::triple<SrcIterator, SrcShape, SrcAccessor> src,
406  vigra::pair<DestIterator, DestAccessor> dest)
407 {
408  return watersheds3D(src.first, src.second, src.third, dest.first, dest.second, NeighborCode3DSix());
409 }
410 
411 template <class SrcIterator, class SrcShape, class SrcAccessor,
412  class DestIterator, class DestAccessor>
413 inline unsigned int watersheds3DTwentySix( vigra::triple<SrcIterator, SrcShape, SrcAccessor> src,
414  vigra::pair<DestIterator, DestAccessor> dest)
415 {
416  return watersheds3D(src.first, src.second, src.third, dest.first, dest.second, NeighborCode3DTwentySix());
417 }
418 
419 }//namespace vigra
420 
421 #endif //VIGRA_watersheds3D_HXX

© Ullrich Köthe (ullrich.koethe@iwr.uni-heidelberg.de)
Heidelberg Collaboratory for Image Processing, University of Heidelberg, Germany

html generated using doxygen and Python
vigra 1.7.1 (Wed Mar 12 2014)