OTB  7.1.0
Orfeo Toolbox
otbChannelSelectorFunctor.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2019 Centre National d'Etudes Spatiales (CNES)
3  *
4  * This file is part of Orfeo Toolbox
5  *
6  * https://www.orfeo-toolbox.org/
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 #ifndef otbChannelSelectorFunctor_h
22 #define otbChannelSelectorFunctor_h
23 #include <cassert>
24 
25 #include "itkVariableLengthVector.h"
26 #include "itkRGBPixel.h"
27 #include "itkRGBAPixel.h"
28 #include "itkObject.h"
29 #include "itkObjectFactory.h"
30 
31 namespace otb
32 {
33 namespace Function
34 {
44 template <class TInputPixel>
45 class ChannelSelectorFunctor : public itk::Object
46 {
47 public:
50  typedef itk::Object Superclass;
51  typedef itk::SmartPointer<Self> Pointer;
52  typedef itk::SmartPointer<const Self> ConstPointer;
53 
55  itkNewMacro(Self);
56 
58  itkTypeMacro(ChannelSelectorFunctor, itk::Object);
59 
60  typedef TInputPixel PixelType;
61  typedef typename itk::NumericTraits<PixelType>::ValueType ScalarType;
62  typedef itk::VariableLengthVector<ScalarType> VectorPixelType;
63  typedef itk::RGBPixel<ScalarType> RGBPixelType;
64  typedef itk::RGBAPixel<ScalarType> RGBAPixelType;
65 
66  typedef VectorPixelType OutputPixelType;
67 
68  typedef std::vector<unsigned int> ChannelListType;
69 
70  const char* GetDescription() const
71  {
72  return "Channel Selection";
73  }
74 
75  virtual OutputPixelType operator()(const VectorPixelType& inPixel) const
76  {
77  // otbMsgDevMacro(<<"Channel list "<< m_ChannelList[0]);
78  OutputPixelType outPixel;
79  outPixel.SetSize(m_ChannelList.size());
80  for (unsigned int i = 0; i < m_ChannelList.size(); ++i)
81  {
82  // assert as the verification should be done outside and only
83  // once for the image, not for every pixel, when we reach this point
84  // the m_ChannelList MUST be valid
85  assert(m_ChannelList[i] < inPixel.Size());
86  outPixel[i] = inPixel[m_ChannelList[i]];
87  }
88  return outPixel;
89  }
90 
91  virtual OutputPixelType operator()(ScalarType inPixel) const
92  {
93  OutputPixelType outPixel;
94  outPixel.SetSize(1);
95  for (unsigned int i = 0; i < m_ChannelList.size(); ++i)
96  {
97  assert(m_ChannelList[i] < 1);
98  outPixel[0] = inPixel;
99  }
100  return outPixel;
101  }
102 
103  virtual OutputPixelType operator()(const RGBPixelType& inPixel) const
104  {
105  OutputPixelType outPixel;
106  outPixel.SetSize(m_ChannelList.size());
107  for (unsigned int i = 0; i < m_ChannelList.size(); ++i)
108  {
109  assert(m_ChannelList[i] < 3);
110  outPixel[i] = inPixel[m_ChannelList[i]];
111  }
112  return outPixel;
113  }
114 
115  virtual OutputPixelType operator()(const RGBAPixelType& inPixel) const
116  {
117  OutputPixelType outPixel;
118  outPixel.SetSize(m_ChannelList.size());
119  for (unsigned int i = 0; i < m_ChannelList.size(); ++i)
120  {
121  assert(m_ChannelList[i] < 4);
122  outPixel[i] = inPixel[m_ChannelList[i]];
123  }
124  return outPixel;
125  }
126 
127  virtual unsigned int GetOutputSize() const
128  {
129  return m_ChannelList.size();
130  }
131 
132  virtual std::vector<unsigned int> GetChannelList() const
133  {
134  return m_ChannelList;
135  }
136 
137  virtual void SetChannelList(std::vector<unsigned int> channels)
138  {
139  m_ChannelList = channels;
140  usingDefaultParameters = false;
141  }
142 
143  virtual void SetChannelIndex(unsigned int channelPosition, unsigned int channel)
144  {
145  if (m_ChannelList.size() < channelPosition + 1)
146  {
147  m_ChannelList.resize(channelPosition + 1, 0);
148  }
149  m_ChannelList[channelPosition] = channel;
150  usingDefaultParameters = false;
151  }
152 
153  virtual unsigned int GetChannelIndex(unsigned int channelPosition) const
154  {
155  if (channelPosition >= m_ChannelList.size())
156  {
157  itkExceptionMacro(<< "Can't get channel " << channelPosition << ", there is only " << m_ChannelList.size() << " element in the list");
158  }
159  return m_ChannelList[channelPosition];
160  }
161 
163  virtual void SetAllChannels(unsigned int channel)
164  {
165  if (m_ChannelList.size() < 3)
166  {
167  m_ChannelList.resize(3, 0);
168  }
169  m_ChannelList[0] = channel;
170  m_ChannelList[1] = channel;
171  m_ChannelList[2] = channel;
172  usingDefaultParameters = false;
173  }
174  virtual void SetRedChannelIndex(unsigned int channel)
175  {
176  if (m_ChannelList.size() < 1)
177  {
178  m_ChannelList.resize(3, 0);
179  }
180  m_ChannelList[0] = channel;
181  usingDefaultParameters = false;
182  }
184 
185  virtual void SetGreenChannelIndex(unsigned int channel)
186  {
187  if (m_ChannelList.size() < 2)
188  {
189  m_ChannelList.resize(3, 0);
190  }
191  m_ChannelList[1] = channel;
192  usingDefaultParameters = false;
193  }
194 
195  virtual void SetBlueChannelIndex(unsigned int channel)
196  {
197  if (m_ChannelList.size() < 3)
198  {
199  m_ChannelList.resize(3, 0);
200  }
201  m_ChannelList[2] = channel;
202  usingDefaultParameters = false;
203  }
204 
205  virtual unsigned int GetRedChannelIndex() const
206  {
207  return m_ChannelList[0];
208  }
209  virtual unsigned int GetGreenChannelIndex() const
210  {
211  return m_ChannelList[1];
212  }
213  virtual unsigned int GetBlueChannelIndex() const
214  {
215  return m_ChannelList[2];
216  }
217 
219  {
220  return usingDefaultParameters;
221  }
222 
223 protected:
226  {
227  if (std::string(typeid(PixelType).name()).find("RGBAPixel") != std::string::npos)
228  {
229  m_ChannelList.push_back(0);
230  m_ChannelList.push_back(1);
231  m_ChannelList.push_back(2);
232  m_ChannelList.push_back(3);
233  }
234  else if (std::string(typeid(PixelType).name()).find("RGBPixel") != std::string::npos)
235  {
236  m_ChannelList.push_back(0);
237  m_ChannelList.push_back(1);
238  m_ChannelList.push_back(2);
239  }
240  else if (std::string(typeid(PixelType).name()).find("VariableLengthVector") != std::string::npos)
241  {
242  m_ChannelList.push_back(0);
243  }
244  else
245  {
246  m_ChannelList.push_back(0);
247  }
248  }
250 
253  {
254  }
255 
256 private:
257  ChannelListType m_ChannelList;
259 };
260 }
261 }
262 
263 #endif
virtual unsigned int GetChannelIndex(unsigned int channelPosition) const
static constexpr char const * name() noexcept
Base class for pixel representation functions.
virtual OutputPixelType operator()(ScalarType inPixel) const
virtual void SetChannelIndex(unsigned int channelPosition, unsigned int channel)
virtual void SetAllChannels(unsigned int channel)
itk::NumericTraits< PixelType >::ValueType ScalarType
virtual std::vector< unsigned int > GetChannelList() const
virtual void SetChannelList(std::vector< unsigned int > channels)
virtual void SetRedChannelIndex(unsigned int channel)
virtual OutputPixelType operator()(const RGBAPixelType &inPixel) const
virtual unsigned int GetRedChannelIndex() const
itk::VariableLengthVector< ScalarType > VectorPixelType
virtual OutputPixelType operator()(const RGBPixelType &inPixel) const
virtual void SetBlueChannelIndex(unsigned int channel)
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
virtual OutputPixelType operator()(const VectorPixelType &inPixel) const
virtual void SetGreenChannelIndex(unsigned int channel)
virtual unsigned int GetBlueChannelIndex() const
itk::SmartPointer< const Self > ConstPointer
virtual unsigned int GetGreenChannelIndex() const