aimstil  5.0.5
RegionGrowingPlugin.h
Go to the documentation of this file.
1 #ifndef TIL_REGION_GROWING_PLUGIN_H
2 #define TIL_REGION_GROWING_PLUGIN_H
3 
4 // includes from STL
5 #include <numeric>
6 #include <vector>
7 
8 // includes from TIL library
9 #include "til/til_common.h"
10 #include "til/numeric_array.h"
11 #include "til/SymMatrix3.h"
12 
13 
14 
15 // The region growing plugins are classes that can be used during
16 // the region growing process to compute some numbers, e.g. the
17 // size or the maximum intensity.
18 
19 // Such numbers can also be computed after the region growing is
20 // done. But one might prefer to compute them on the fly.
21 // Especially when the region growing itself depends
22 // on these numbers (say, we stop region growing if a maximum
23 // size has been reached). This is after all the big feature
24 // of region growing over simple connected components algorithms.
25 
26 
27 namespace til
28 {
29 
30  template < typename TPoint >
32  {
33  public: // constructors
34 
35  RegionCentroid() { this->init(); };
36 
37  public: // initializations
38 
39  void init()
40  {
41  m_nElem = 0;
42  std::fill(m_sum.begin(), m_sum.end(), 0);
43  }
44 
45  public: // set & get
46 
48  TPoint get() { return m_sum / m_nElem; }
49 
50  /*
51  // TODO: this is really stupid, return v instead.
52  void getCentroid(numeric_array<double,3> & v)
53  {
54  v = m_sum * (1.0/ m_nElem);
55  / *
56  v[0] = m_sum[0] / m_nElem;
57  v[1] = m_sum[1] / m_nElem;
58  v[2] = m_sum[2] / m_nElem;
59  * /
60  }
61  */
62 
63  public: // functions
64 
65 
66  template < typename TIterator >
67  void update(TIterator begin, TIterator end)
68  {
69  for (; begin != end; ++begin)
70  {
71  m_sum += *begin;
72  }
73  }
74  /*
75  void update(const std::vector<numeric_array<int,3> > & pl)
76  {
77  //m_sum = std::accumulate(pl.begin(), pl.end(), m_sum, std::plus<numeric_array<double,3> >());
78  //m_sum = std::accumulate(pl.begin(), pl.end(), m_sum, functor::Add<numeric_array<std::plus<numeric_array<double,3> >());
79  //add(pl, m_sum);
80  m_nElem += pl.size();
81  }
82  */
83 
84  private: // data
85 
86  //numeric_array<double,3> m_sum;
87  TPoint m_sum;
88  std::size_t m_nElem;
89  };
90 
91 
92 
93 template < class TImage >
95 {
96 public: // typedefs
97 
98  typedef typename TImage::value_type value_type;
99 
100 public: // constuctors & destructor
101 
103 
104  RegionMaxInt(const TImage &im)
105  {
106  this->init(im);
107  }
108 
109 public: // set & get
110 
111  value_type getMaxInt() { return m_max; }
112 
113 public: // functions
114 
115  void init(const TImage &im)
116  {
117  m_im.shallowCopy(im);
119  }
120 
121  void update(const numeric_array<int,3> & v)
122  {
123  if (m_im(v) > m_max)
124  {
125  m_max = m_im(v);
126  }
127  }
128 
129 
130 private: // data
131 
132  TImage m_im;
133  value_type m_max;
134 };
135 
136 
137 /*
138 class TIL_API RegionBoundingBox
139 {
140 
141 public: // constructors & destructor
142 
143  RegionBoundingBox() { this->initialize(); }
144 
145 public: // set & get
146 
147  int getMaxz() { return m_range.getZMax(); }
148 
149 public: // functions
150 
151  void initialize()
152  {
153  m_minx = m_miny = m_minz = std::numeric_limits<int>::max();
154  m_maxx = m_maxy = m_maxz = std::numeric_limits<int>::min();
155  }
156 
157  void update(const ConstPtr<PointList<int> > &pl)
158  {
159  Range tmpRange;
160  boundingBox(pl, tmpRange);
161  include(tmpRange, m_range);
162  }
163 
164 
165 private: // data
166 
167  Range m_range;
168 };
169 */
170 
171 
172 // TODO: use Accumulator
174 {
175 
176 public: // constructors & destructor
177 
178  RegionMoments() { this->initialize(); }
179 
180 public: // set & get
181 
183  {
184 
185  // NB: we do not do m_xx /= m_nElem, etc. in case sb calls
186  // getMoments twice or more
187 
188  // TODO: replace this by an operator or something...
189 
190  mat(0,0) = (m_sum2(0,0) / m_nElem) - square(m_sum[0] / m_nElem);
191  mat(1,1) = (m_sum2(1,1) / m_nElem) - square(m_sum[1] / m_nElem);
192  mat(2,2) = (m_sum2(2,2) / m_nElem) - square(m_sum[2] / m_nElem);
193 
194  mat(0,1) = (m_sum2(0,1) / m_nElem) - (m_sum[0] / m_nElem) * (m_sum[1] / m_nElem);
195  mat(0,2) = (m_sum2(0,2) / m_nElem) - (m_sum[0] / m_nElem) * (m_sum[2] / m_nElem);
196  mat(1,2) = (m_sum2(1,2) / m_nElem) - (m_sum[2] / m_nElem) * (m_sum[1] / m_nElem);
197  }
198 
199 
200 public: // functions
201 
202  void initialize()
203  {
204  m_nElem = 0;
205  }
206 
208  {
209  static SymMatrix3<int> mat;
210  m_sum += v;
211  tdot(v, mat);
212  add(mat, m_sum2);
213  ++m_nElem;
214  }
215 
216 
217 
218 private: // data
219 
220  // sum of coordinates
222 
223  // sum of multiplication of coordinates
224  SymMatrix3<double> m_sum2;
225 
226  // number of elements
227  int m_nElem;
228 };
229 
230 
231 
233 {
234 
235 public: // constuctors & destructor
236 
237  DoNothing() {};
238 
239 public:
240 
241  void update(const std::vector<numeric_array<int,3> > &) {}
242 };
243 
244 
246 {
247 
248 public: // constructors & destructor
249 
250  RegionSize() { this->init(); };
251 
252 
253 public: // set & get
254 
255  size_t size()
256  {
257  return m_nElem;
258  }
259 
260 public: // functions
261 
262  void init() { m_nElem = 0; };
263 
264  void update(const std::vector<numeric_array<int,3> > & pl)
265  {
266  m_nElem += pl.size();
267  }
268 
269 
270 
271 private: // data
272 
273  size_t m_nElem;
274 };
275 
276 
277 
278 } // namespace
279 
280 #endif
281 
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.
TRes add(T1 x, T2 y)
Definition: functors.h:394
void tdot(numeric_array< T, 3 > const &v, SymMatrix3< T > &mat)
Stores v.v^T in mat.
TImage::value_type value_type
RegionMaxInt(const TImage &im)
void update(TIterator begin, TIterator end)
void update(const numeric_array< int, 3 > &v)
void update(const std::vector< numeric_array< int, 3 > > &)
void init(const TImage &im)
void update(const numeric_array< int, 3 > &v)
void square(const TImage &in, TImage &out)
Definition: imageArith.h:310
void fill(sparse_vector< T, BaselinePolicy > &v, typename boost::call_traits< T >::param_type value)
Specialized fill for sparse_vector.
void update(const std::vector< numeric_array< int, 3 > > &pl)
void getMoments(SymMatrix3< double > &mat)