2 * This program is free software: you can redistribute it and/or modify
3 * it under the terms of the GNU Lesser General Public License as
4 * published by the Free Software Foundation, either version 3 of the
5 * License, or (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 * @file ImageHelper.ih
19 * @author Tristan Roussillon (\c tristan.roussillon@liris.cnrs.fr )
20 * Laboratoire d'InfoRmatique en Image et Systèmes d'information - LIRIS (CNRS, UMR 5205), CNRS, France
24 * Implementation of inline methods defined in ImageHelper.h
26 * This file is part of the DGtal library.
30 //////////////////////////////////////////////////////////////////////////////
34 #include "DGtal/kernel/PointVector.h"
35 //////////////////////////////////////////////////////////////////////////////
39 //------------------------------------------------------------------------------
40 template<typename I, typename O, typename P>
43 DGtal::setFromPointsRangeAndPredicate(const I& itb, const I& ite, const O& ito, const P& aPred)
45 BOOST_CONCEPT_ASSERT(( concepts::CPointPredicate<P> ));
46 BOOST_CONCEPT_ASSERT(( boost::InputIterator<I> ));
47 BOOST_CONCEPT_ASSERT(( boost::OutputIterator<O,typename P::Point> ));
49 functors::NotPointPredicate<P> nPred( aPred );
50 std::remove_copy_if(itb, ite, ito, nPred);
53 //------------------------------------------------------------------------------
54 template<typename I, typename O, typename F>
57 DGtal::setFromPointsRangeAndFunctor(const I& itb, const I& ite, const O& ito,
58 const F& aFunctor, const typename F::Value& aThreshold)
60 BOOST_CONCEPT_ASSERT(( concepts::CPointFunctor<F> ));
61 BOOST_CONCEPT_ASSERT(( boost::InputIterator<I> ));
62 BOOST_CONCEPT_ASSERT(( boost::OutputIterator<O,typename F::Point> ));
64 typedef functors::Thresholder<typename F::Value,false,false> T;
66 functors::Composer<F, T, bool> aPred(aFunctor, t);
67 std::remove_copy_if(itb, ite, ito, aPred);
70 //------------------------------------------------------------------------------
71 template<typename I, typename O>
74 DGtal::setFromImage(const I& aImg, const O& ito, const typename I::Value& aThreshold)
76 BOOST_CONCEPT_ASSERT(( concepts::CConstImage<I> ));
78 typename I::Domain d = aImg.domain();
79 setFromPointsRangeAndFunctor(d.begin(), d.end(), ito, aImg, aThreshold);
82 //------------------------------------------------------------------------------
83 template<typename I, typename O>
86 DGtal::setFromImage(const I& aImg, const O& ito,
87 const typename I::Value& low,
88 const typename I::Value& up)
90 BOOST_CONCEPT_ASSERT(( concepts::CConstImage<I> ));
94 typename I::Domain d = aImg.domain();
95 //predicate from two thresholders and an image
96 typedef functors::Thresholder<typename I::Value,true,false> T1;
98 typedef functors::Thresholder<typename I::Value,false,false> T2;
100 typedef functors::PredicateCombiner< T1, T2, functors::OrBoolFct2 > P;
101 P p( t1, t2, functors::OrBoolFct2() );
102 functors::Composer<I, P, bool> aPred(aImg, p);
104 std::remove_copy_if(d.begin(), d.end(), ito, aPred);
107 //------------------------------------------------------------------------------
108 template<typename It, typename Im>
111 DGtal::imageFromRangeAndValue(const It& itb, const It& ite, Im& aImg,
112 const typename Im::Value& aValue)
114 BOOST_CONCEPT_ASSERT(( boost::InputIterator<It> ));
115 BOOST_CONCEPT_ASSERT(( concepts::CImage<Im> ));
117 typename Im::Domain d = aImg.domain();
118 for (It it = itb; it != ite; ++it)
120 if (d.isInside( *it ))
121 aImg.setValue( *it, aValue );
125 //------------------------------------------------------------------------------
126 template<typename R, typename I>
129 DGtal::imageFromRangeAndValue(const R& aRange, I& aImg,
130 const typename I::Value& aValue)
132 BOOST_CONCEPT_ASSERT(( concepts::CConstSinglePassRange<R> ));
133 BOOST_CONCEPT_ASSERT(( concepts::CImage<I> ));
135 imageFromRangeAndValue( aRange.begin(), aRange.end(), aImg, aValue);
138 //------------------------------------------------------------------------------
139 template<typename I, typename F>
142 DGtal::imageFromFunctor(I& aImg, const F& aFun)
144 BOOST_CONCEPT_ASSERT(( concepts::CImage<I> ));
145 BOOST_CONCEPT_ASSERT(( concepts::CPointFunctor<F> ));
147 typename I::Domain d = aImg.domain();
149 std::transform(d.begin(), d.end(), aImg.range().outputIterator(), aFun );
152 //------------------------------------------------------------------------------
153 template<typename I1, typename I2>
156 DGtal::imageFromImage(I1& aImg1, const I2& aImg2)
158 BOOST_CONCEPT_ASSERT(( concepts::CImage<I1> ));
159 BOOST_CONCEPT_ASSERT(( concepts::CConstImage<I2> ));
161 typename I2::ConstRange r = aImg2.constRange();
162 std::copy( r.begin(), r.end(), aImg1.range().outputIterator() );
165 //------------------------------------------------------------------------------
166 template<typename I, typename S, typename D, typename V>
167 struct InsertAndSetValue
169 static bool implementation(I& aImg, S& aSet,
170 const typename D::Point& aPoint,
173 // std::pair<typename S::Iterator, bool> res
174 // = aSet.insert( aPoint );
175 // aImg.setValue( aPoint, aValue );
176 // return res.second;
179 if ( aSet.find( aPoint ) == aSet.end() )
182 aSet.insert( aPoint );
183 aImg.setValue( aPoint, aValue );
188 //------------------------------------------------------------------------------
189 //Partial specialization
190 template<typename D, typename V>
191 struct InsertAndSetValue<
192 DGtal::ImageContainerBySTLMap<D,V>,
193 DGtal::DigitalSetFromMap<DGtal::ImageContainerBySTLMap<D,V> >,
196 static bool implementation
197 (DGtal::ImageContainerBySTLMap<D,V>& aImg,
198 DGtal::DigitalSetFromMap<DGtal::ImageContainerBySTLMap<D,V> >& /*aSet*/,
199 const typename D::Point& aPoint,
202 typedef typename D::Point P;
203 typedef typename DGtal::ImageContainerBySTLMap<D,V>::iterator Iterator;
206 pair( aPoint, aValue );
207 std::pair<Iterator, bool> res
208 = aImg.insert( pair );
213 //------------------------------------------------------------------------------
214 template<typename I, typename S>
217 DGtal::insertAndSetValue(I& aImg, S& aSet,
218 const typename I::Point& aPoint,
219 const typename I::Value& aValue )
221 BOOST_CONCEPT_ASSERT(( concepts::CImage<I> ));
222 BOOST_CONCEPT_ASSERT(( concepts::CDigitalSet<S> ));
223 BOOST_STATIC_ASSERT(( boost::is_same< typename I::Point, typename S::Point >::value ));
225 typedef typename I::Domain D;
226 typedef typename I::Value V;
227 return InsertAndSetValue<I,S,D,V>::implementation(aImg, aSet, aPoint, aValue);
230 //------------------------------------------------------------------------------
231 template<typename I, typename S, typename D, typename V>
232 struct InsertAndAlwaysSetValue
234 static bool implementation(I& aImg, S& aSet,
235 const typename D::Point& aPoint,
239 if ( aSet.find( aPoint ) != aSet.end() )
242 aSet.insert( aPoint );
243 aImg.setValue( aPoint, aValue );
247 //------------------------------------------------------------------------------
248 //Partial specialization
249 template<typename D, typename V>
250 struct InsertAndAlwaysSetValue<
251 DGtal::ImageContainerBySTLMap<D,V>,
252 DGtal::DigitalSetFromMap<DGtal::ImageContainerBySTLMap<D,V> >,
255 static bool implementation
256 (DGtal::ImageContainerBySTLMap<D,V>& aImg,
257 DGtal::DigitalSetFromMap<DGtal::ImageContainerBySTLMap<D,V> >& /*aSet*/,
258 const typename D::Point& aPoint,
261 typedef typename D::Point P;
262 typedef typename DGtal::ImageContainerBySTLMap<D,V>::iterator Iterator;
265 pair( aPoint, aValue );
266 std::pair<Iterator, bool> res
267 = aImg.insert( pair );
268 bool flag = res.second;
269 if (flag == false) //set value even in this case
270 res.first->second = aValue;
275 //------------------------------------------------------------------------------
276 template<typename I, typename S>
279 DGtal::insertAndAlwaysSetValue(I& aImg, S& aSet,
280 const typename I::Point& aPoint,
281 const typename I::Value& aValue )
284 BOOST_CONCEPT_ASSERT(( concepts::CImage<I> ));
285 BOOST_CONCEPT_ASSERT(( concepts::CDigitalSet<S> ));
286 BOOST_STATIC_ASSERT(( boost::is_same< typename I::Point, typename S::Point >::value ));
288 typedef typename I::Domain D;
289 typedef typename I::Value V;
290 return InsertAndAlwaysSetValue<I,S,D,V>::implementation(aImg, aSet, aPoint, aValue);
294 //------------------------------------------------------------------------------
295 template<typename I, typename S, typename D, typename V>
296 struct FindAndGetValue
298 static bool implementation(const I& aImg, const S& aSet,
299 const typename D::Point& aPoint,
302 if ( aSet.find( aPoint ) != aSet.end() )
304 aValue = aImg( aPoint );
310 //------------------------------------------------------------------------------
311 //Partial specialization
312 template<typename D, typename V>
313 struct FindAndGetValue<
314 DGtal::ImageContainerBySTLMap<D,V>,
315 DGtal::DigitalSetFromMap<DGtal::ImageContainerBySTLMap<D,V> >,
318 static bool implementation
319 (const DGtal::ImageContainerBySTLMap<D,V>& aImg,
320 const DGtal::DigitalSetFromMap<DGtal::ImageContainerBySTLMap<D,V> >& /*aSet*/,
321 const typename D::Point& aPoint,
324 typedef typename DGtal::ImageContainerBySTLMap<D,V>::const_iterator ConstIterator;
326 ConstIterator it = aImg.find( aPoint );
327 if ( it != aImg.end() )
336 //------------------------------------------------------------------------------
337 template<typename I, typename S>
340 DGtal::findAndGetValue(const I& aImg, const S& aSet,
341 const typename I::Point& aPoint,
342 typename I::Value& aValue )
345 BOOST_CONCEPT_ASSERT(( concepts::CConstImage<I> ));
346 BOOST_CONCEPT_ASSERT(( concepts::CDigitalSet<S> ));
347 BOOST_STATIC_ASSERT(( boost::is_same< typename I::Point, typename S::Point >::value ));
349 typedef typename I::Domain D;
350 typedef typename I::Value V;
351 return FindAndGetValue<I,S,D,V>::implementation(aImg, aSet, aPoint, aValue);