aimstil  5.0.5
distanceTransform.h
Go to the documentation of this file.
1 #ifndef TIL_DISTANCE_TRANSFORM_H
2 #define TIL_DISTANCE_TRANSFORM_H
3 
4 // Local includes
5 #include "til/til_common.h"
6 
7 namespace til
8 {
9 
10  // NB: both foreground and background are needed, since it might be that
11  // other objects are present inside the image, inside which the distance
12  // is not computed. However they are important, since the geodesics
13  // goes around these objects.
14 
15  template < typename TImage >
16  void
17  cityBlock(TImage &seg,
18  typename TImage::value_type foreground,
19  typename TImage::value_type background
20  )
21  {
22  {
23  typename TImage::LinearIterator iSeg(seg);
24 
25  const TImage::value_type INFINITY = std::numerical_limits<T>::max() - 1;
26  const TImage::value_type OTHER_OBJECT = std::numerical_limits<T>::max();
27 
28  // Initialize seg for distance map
29  for (; !iSeg.isAtEnd(); ++iSeg)
30  {
31  // Object has a distance of zero
32  if (*iSeg == foreground) *iSeg = 0;
33  // Background has an initial distance of as close as infinity we can get
34  else if (*iSeg == background) *iSeg = INFINITY;
35  // Other objects have a special value
36  else *iSeg = OTHER_OBJECT;
37  }
38 
39 
40  {
41  // forward loop
42  typename TImage::LinearIterator iSeg(seg);
43  for(; !iSeg.isAtEnd(); ++iSeg)
44  {
45  if (*iSeg == OTHER_OBJECT) continue;
46  *iSeg = min(*iSeg, min(min(min(iSeg.getValue<-1,0,0>(), iSeg.getValue<0,-1,0>()), iSeg.getValue<0,0,-1>) + T(1), INFINITY)));
47  }
48  }
49  {
50  // backward loop
51  typename TImage::LinearIterator iSeg(seg);
52  for(; !iSeg.isAtEnd(); --iSeg)
53  {
54  if (*iSeg == OTHER_OBJECT) continue;
55  *iSeg = min(*iSeg, min(min(min(iSeg.getValue<+1,0,0>(), iSeg.getValue<0,+1,0>()), iSeg.getValue<0,0,+1>) + T(1), INFINITY)));
56  }
57  }
58  }
59 
60 
61 } // namespace
62 
63 #endif
64 
boost::enable_if< is_Image< TImage >, typename TImage::value_type >::type min(const TImage &im)
Belongs to package Box Do not include directly, include til/Box.h instead.
Definition: Accumulator.h:10
General macros, definitions and functions.
TImage::value_type max(const TImage &im)
Returns the maximum intensity of the input image.