iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ImageAlgorithmsImpl.H
Go to the documentation of this file.
1 /*! @file
2  @author David J. Berg
3  @copyright GNU Public License (GPL v3)
4  @section License
5  @verbatim
6  // ////////////////////////////////////////////////////////////////////////
7  // The iLab Neuromorphic Robotics Toolkit (NRT) //
8  // Copyright 2010-2012 by the University of Southern California (USC) //
9  // and the iLab at USC. //
10  // //
11  // iLab - University of Southern California //
12  // Hedco Neurociences Building, Room HNB-10 //
13  // Los Angeles, Ca 90089-2520 - USA //
14  // //
15  // See http://ilab.usc.edu for information about this project. //
16  // ////////////////////////////////////////////////////////////////////////
17  // This file is part of The iLab Neuromorphic Robotics Toolkit. //
18  // //
19  // The iLab Neuromorphic Robotics Toolkit is free software: you can //
20  // redistribute it and/or modify it under the terms of the GNU General //
21  // Public License as published by the Free Software Foundation, either //
22  // version 3 of the License, or (at your option) any later version. //
23  // //
24  // The iLab Neuromorphic Robotics Toolkit is distributed in the hope //
25  // that it will be useful, but WITHOUT ANY WARRANTY; without even the //
26  // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
27  // PURPOSE. See the GNU General Public License for more details. //
28  // //
29  // You should have received a copy of the GNU General Public License //
30  // along with The iLab Neuromorphic Robotics Toolkit. If not, see //
31  // <http://www.gnu.org/licenses/>. //
32  // ////////////////////////////////////////////////////////////////////////
33  @endverbatim */
34 
35 
36 #ifndef INCLUDE_NRT_CORE_IMAGE_DETAILS_IMAGEALGORITHMSIMPL_H
37 #define INCLUDE_NRT_CORE_IMAGE_DETAILS_IMAGEALGORITHMSIMPL_H
38 
39 namespace nrt
40 {
41  // ######################################################################
42  // accumulate
43  // ######################################################################
44  template <class PixType, class BinaryOperation> inline
45  PixType accumulate(nrt::Image<PixType> const & img, PixType val, BinaryOperation op)
46  {
47  return std::accumulate(img.const_begin(), img.const_end(), val, op);
48  }
49 
50  // ######################################################################
51  // Transform in Pace
52  // ######################################################################
53  template <class PixType, class UnaryOperator> inline
54  void transformInPlace(nrt::Image<PixType>& lhs, UnaryOperator op)
55  {
56  std::transform(lhs.const_begin(), lhs.const_end(), lhs.begin(), op);
57  }
58 
59  // ######################################################################
60  template <class PixType1, class PixType2, class BinaryOperator> inline
61  void transformInPlace(nrt::Image<PixType1> & lhs, nrt::Image<PixType2> const& rhs, BinaryOperator op)
62  {
63  std::transform(lhs.const_begin(), lhs.const_end(), rhs.const_begin(), lhs.begin(), op);
64  }
65 
66  // ######################################################################
67  // Transform
68  // ######################################################################
69  template <class PixType, class UnaryOperator> inline
70  nrt::Image<PixType> transform(nrt::Image<PixType> const& lhs, UnaryOperator op)
71  {
72  nrt::Image<PixType> ret(lhs.dims());
73  std::transform(lhs.const_begin(), lhs.const_end(), ret.begin(), op);
74  return ret;
75  }
76 
77  // ######################################################################
78  template <class PromoteType, class PixType, class UnaryOperator> inline
79  nrt::Image<PromoteType> transform(nrt::Image<PixType> const& lhs, UnaryOperator op)
80  {
81  nrt::Image<PromoteType> ret(lhs.dims());
82  std::transform(lhs.const_begin(), lhs.const_end(), ret.begin(), op);
83  return ret;
84  }
85 
86  // ######################################################################
87  template <class PixType1, class PixType2, class BinaryOperator> inline
88  typename std::enable_if<nrt_traits<PixType1, PixType2>::has_arithmetic,
90  transform(nrt::Image<PixType1> const& lhs, nrt::Image<PixType2> const& rhs, BinaryOperator op)
91  {
92  typedef typename nrt_traits<PixType1,PixType2>::promote_type PromoType;
93  nrt::Image<PromoType> ret(lhs.dims());
94  std::transform(lhs.const_begin(), lhs.const_end(), rhs.const_begin(), ret.begin(), op);
95  return ret;
96  }
97 
98  // ######################################################################
99  template <class PixType, class UnaryOperator>
101  {
102  return nrt::transform(lhs, [&op](PixType const& p)
103  {
104  PixType ret;
105  std::transform(p.begin(), p.end(), ret.begin(), op);
106  return ret;
107  });
108  }
109 
110  // ######################################################################
111  template <class PromoteType, class PixType, class UnaryOperator>
113  {
114  return nrt::transform<PromoteType>(lhs, [&op](PixType const& p)
115  {
116  PromoteType ret;
117  std::transform(p.begin(), p.end(), ret.begin(), op);
118  return ret;
119  });
120  }
121 
122  // ######################################################################
123  template <class PixType1, class PixType2, class BinaryOperator>
124  typename std::enable_if<nrt_traits<PixType1, PixType2>::has_arithmetic,
126  channel_transform(nrt::Image<PixType1> const& lhs, nrt::Image<PixType2> const& rhs, BinaryOperator op)
127  {
128  typedef typename nrt_traits<PixType1,PixType2>::promote_type PromoType;
129  return nrt::transform<PromoType>(lhs, rhs, [&op](PixType1 const& p1, PixType2 const& p2)
130  {
131  PromoType ret;
132  std::transform(p1.begin(), p1.end(), p2.begin(), ret.begin(), op);
133  return ret;
134  });
135  }
136 
137 
138  // ######################################################################
139  // for each
140  // ######################################################################
141  template <class PixType, class UnaryFunction> inline
142  void for_each(nrt::Image<PixType> const& img, UnaryFunction op)
143  {
144  std::for_each(img.const_begin(), img.const_end(), op);
145  }
146 }
147 #endif // INCLUDE_NRT_CORE_IMAGE_DETAILS_IMAGEALGORITHMSIMPL_H
148