DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ImageHelper.ih
1 
30 
31 #include <cstdlib>
33 
34 
35 
36 //------------------------------------------------------------------------------
37 template<typename I, typename O, typename P>
38 inline
39 void
40 DGtal::setFromPointsRangeAndPredicate(const I& itb, const I& ite, const O& ito, const P& aPred)
41 {
42  BOOST_CONCEPT_ASSERT(( CPointPredicate<P> ));
43  BOOST_CONCEPT_ASSERT(( boost::InputIterator<I> ));
44  BOOST_CONCEPT_ASSERT(( boost::OutputIterator<O,typename P::Point> ));
45 
46  NotPointPredicate<P> nPred( aPred );
47  std::remove_copy_if(itb, ite, ito, nPred);
48 }
49 
50 //------------------------------------------------------------------------------
51 template<typename I, typename O, typename F>
52 inline
53 void
54 DGtal::setFromPointsRangeAndFunctor(const I& itb, const I& ite, const O& ito,
55  const F& aFunctor, const typename F::Value& aThreshold)
56 {
57  BOOST_CONCEPT_ASSERT(( CPointFunctor<F> ));
58  BOOST_CONCEPT_ASSERT(( boost::InputIterator<I> ));
59  BOOST_CONCEPT_ASSERT(( boost::OutputIterator<O,typename F::Point> ));
60 
62  T t( aThreshold );
63  Composer<F, T, bool> aPred(aFunctor, t);
64  std::remove_copy_if(itb, ite, ito, aPred);
65 }
66 
67 //------------------------------------------------------------------------------
68 template<typename I, typename O>
69 inline
70 void
71 DGtal::setFromImage(const I& aImg, const O& ito, const typename I::Value& aThreshold)
72 {
73  BOOST_CONCEPT_ASSERT(( CConstImage<I> ));
74 
75  typename I::Domain d = aImg.domain();
76  setFromPointsRangeAndFunctor(d.begin(), d.end(), ito, aImg, aThreshold);
77 }
78 
79 //------------------------------------------------------------------------------
80 template<typename I, typename O>
81 inline
82 void
83 DGtal::setFromImage(const I& aImg, const O& ito,
84  const typename I::Value& low,
85  const typename I::Value& up)
86 {
87  BOOST_CONCEPT_ASSERT(( CConstImage<I> ));
88  ASSERT( low < up );
89 
90  //domain
91  typename I::Domain d = aImg.domain();
92  //predicate from two thresholders and an image
94  T1 t1( low );
96  T2 t2( up );
98  P p( t1, t2, OrBoolFct2() );
99  Composer<I, P, bool> aPred(aImg, p);
100  //call
101  std::remove_copy_if(d.begin(), d.end(), ito, aPred);
102 }
103 
104 //------------------------------------------------------------------------------
105 template<typename It, typename Im>
106 inline
107 void
108 DGtal::imageFromRangeAndValue(const It& itb, const It& ite, Im& aImg,
109  const typename Im::Value& aValue)
110 {
111  BOOST_CONCEPT_ASSERT(( boost::InputIterator<It> ));
112  BOOST_CONCEPT_ASSERT(( CImage<Im> ));
113 
114  typename Im::Domain d = aImg.domain();
115  for (It it = itb; it != ite; ++it)
116  {
117  if (d.isInside( *it ))
118  aImg.setValue( *it, aValue );
119  }
120 }
121 
122 //------------------------------------------------------------------------------
123 template<typename R, typename I>
124 inline
125 void
126 DGtal::imageFromRangeAndValue(const R& aRange, I& aImg,
127  const typename I::Value& aValue)
128 {
129  BOOST_CONCEPT_ASSERT(( CConstSinglePassRange<R> ));
130  BOOST_CONCEPT_ASSERT(( CImage<I> ));
131 
132  imageFromRangeAndValue( aRange.begin(), aRange.end(), aImg, aValue);
133 }
134 
135 //------------------------------------------------------------------------------
136 template<typename I, typename F>
137 inline
138 void
139 DGtal::imageFromFunctor(I& aImg, const F& aFun)
140 {
141  BOOST_CONCEPT_ASSERT(( CImage<I> ));
142  BOOST_CONCEPT_ASSERT(( CPointFunctor<F> ));
143 
144  typename I::Domain d = aImg.domain();
145 
146  std::transform(d.begin(), d.end(), aImg.range().outputIterator(), aFun );
147 }
148 
149 //------------------------------------------------------------------------------
150 template<typename I>
151 inline
152 void
153 DGtal::imageFromImage(I& aImg1, const I& aImg2)
154 {
155  BOOST_CONCEPT_ASSERT(( CImage<I> ));
156 
157  typename I::ConstRange r = aImg2.constRange();
158  std::copy( r.begin(), r.end(), aImg1.range().outputIterator() );
159 }
160 
161 //------------------------------------------------------------------------------
162 template<typename I, typename S, typename D, typename V>
164 {
165  static bool implementation(I& aImg, S& aSet,
166  const typename D::Point& aPoint,
167  const V& aValue)
168  {
169  // std::pair<typename S::Iterator, bool> res
170  // = aSet.insert( aPoint );
171  // aImg.setValue( aPoint, aValue );
172  // return res.second;
173 
174  bool found = true;
175  if ( aSet.find( aPoint ) == aSet.end() )
176  { //if not found
177  found = false;
178  aSet.insert( aPoint );
179  aImg.setValue( aPoint, aValue );
180  }
181  return !found;
182  }
183 };
184 //------------------------------------------------------------------------------
185 //Partial specialization
186 template<typename D, typename V>
188  ImageContainerBySTLMap<D,V>,
189  DigitalSetFromMap<ImageContainerBySTLMap<D,V> >,
190  D, V >
191 {
192  static bool implementation
193  (ImageContainerBySTLMap<D,V>& aImg,
194  DigitalSetFromMap<ImageContainerBySTLMap<D,V> >& /*aSet*/,
195  const typename D::Point& aPoint,
196  const V& aValue)
197  {
198  typedef typename D::Point P;
199  typedef typename ImageContainerBySTLMap<D,V>::iterator Iterator;
200 
201  std::pair<P, V>
202  pair( aPoint, aValue );
203  std::pair<Iterator, bool> res
204  = aImg.insert( pair );
205  return res.second;
206  }
207 };
208 
209 //------------------------------------------------------------------------------
210 template<typename I, typename S>
211 inline
212 bool
213 DGtal::insertAndSetValue(I& aImg, S& aSet,
214  const typename I::Point& aPoint,
215  const typename I::Value& aValue )
216 {
217  BOOST_CONCEPT_ASSERT(( CImage<I> ));
218  BOOST_CONCEPT_ASSERT(( CDigitalSet<S> ));
219  BOOST_STATIC_ASSERT(( boost::is_same< typename I::Point, typename S::Point >::value ));
220 
221  typedef typename I::Domain D;
222  typedef typename I::Value V;
223  return InsertAndSetValue<I,S,D,V>::implementation(aImg, aSet, aPoint, aValue);
224 }
225 
226 //------------------------------------------------------------------------------
227 template<typename I, typename S, typename D, typename V>
229 {
230  static bool implementation(I& aImg, S& aSet,
231  const typename D::Point& aPoint,
232  const V& aValue)
233  {
234 
235  // std::pair<typename S::Iterator, bool> res
236  // = aSet.insert( aPoint );
237  // aImg.setValue( aPoint, aValue );
238  // return res.second;
239 
240  bool found = false;
241  if ( aSet.find( aPoint ) != aSet.end() )
242  found = true;
243  //always set value
244  aSet.insert( aPoint );
245  aImg.setValue( aPoint, aValue );
246  return !found;
247  }
248 };
249 //------------------------------------------------------------------------------
250 //Partial specialization
251 template<typename D, typename V>
253  ImageContainerBySTLMap<D,V>,
254  DigitalSetFromMap<ImageContainerBySTLMap<D,V> >,
255  D, V >
256 {
257  static bool implementation
258  (ImageContainerBySTLMap<D,V>& aImg,
259  DigitalSetFromMap<ImageContainerBySTLMap<D,V> >& /*aSet*/,
260  const typename D::Point& aPoint,
261  const V& aValue)
262  {
263  typedef typename D::Point P;
264  typedef typename ImageContainerBySTLMap<D,V>::iterator Iterator;
265 
266  std::pair<P, V>
267  pair( aPoint, aValue );
268  std::pair<Iterator, bool> res
269  = aImg.insert( pair );
270  bool flag = res.second;
271  if (flag == false) //set value even in this case
272  res.first->second = aValue;
273  return flag;
274  }
275 };
276 
277 //------------------------------------------------------------------------------
278 template<typename I, typename S>
279 inline
280 bool
282  const typename I::Point& aPoint,
283  const typename I::Value& aValue )
284 {
285 
286  BOOST_CONCEPT_ASSERT(( CImage<I> ));
287  BOOST_CONCEPT_ASSERT(( CDigitalSet<S> ));
288  BOOST_STATIC_ASSERT(( boost::is_same< typename I::Point, typename S::Point >::value ));
289 
290  typedef typename I::Domain D;
291  typedef typename I::Value V;
292  return InsertAndAlwaysSetValue<I,S,D,V>::implementation(aImg, aSet, aPoint, aValue);
293 }
294 
295 
296 //------------------------------------------------------------------------------
297 template<typename I, typename S, typename D, typename V>
299 {
300  static bool implementation(const I& aImg, const S& aSet,
301  const typename D::Point& aPoint,
302  V& aValue)
303  {
304  if ( aSet.find( aPoint ) != aSet.end() )
305  {
306  aValue = aImg( aPoint );
307  return true;
308  }
309  else return false;
310  }
311 };
312 //------------------------------------------------------------------------------
313 //Partial specialization
314 template<typename D, typename V>
316  ImageContainerBySTLMap<D,V>,
317  DigitalSetFromMap<ImageContainerBySTLMap<D,V> >,
318  D, V >
319 {
320  static bool implementation
321  (const ImageContainerBySTLMap<D,V>& aImg,
322  const DigitalSetFromMap<ImageContainerBySTLMap<D,V> >& /*aSet*/,
323  const typename D::Point& aPoint,
324  V& aValue)
325  {
326  typedef typename D::Point P;
327  typedef typename ImageContainerBySTLMap<D,V>::const_iterator ConstIterator;
328 
329  ConstIterator it = aImg.find( aPoint );
330  if ( it != aImg.end() )
331  {
332  aValue = it->second;
333  return true;
334  }
335  else return false;
336  }
337 };
338 
339 //------------------------------------------------------------------------------
340 template<typename I, typename S>
341 inline
342 bool
343 DGtal::findAndGetValue(const I& aImg, const S& aSet,
344  const typename I::Point& aPoint,
345  typename I::Value& aValue )
346 {
347 
348  BOOST_CONCEPT_ASSERT(( CImage<I> ));
349  BOOST_CONCEPT_ASSERT(( CDigitalSet<S> ));
350  BOOST_STATIC_ASSERT(( boost::is_same< typename I::Point, typename S::Point >::value ));
351 
352  typedef typename I::Domain D;
353  typedef typename I::Value V;
354  return FindAndGetValue<I,S,D,V>::implementation(aImg, aSet, aPoint, aValue);
355 }