OTB  9.0.0
Orfeo Toolbox
otbChannelSelectorFunctor.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2022 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 
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:
259 };
260 }
261 }
262 
263 #endif
otb::Function::ChannelSelectorFunctor::ConstPointer
itk::SmartPointer< const Self > ConstPointer
Definition: otbChannelSelectorFunctor.h:52
otb::Function::ChannelSelectorFunctor::SetRedChannelIndex
virtual void SetRedChannelIndex(unsigned int channel)
Definition: otbChannelSelectorFunctor.h:174
otb::Function::ChannelSelectorFunctor::operator()
virtual OutputPixelType operator()(const VectorPixelType &inPixel) const
Definition: otbChannelSelectorFunctor.h:75
otb::Function::ChannelSelectorFunctor::usingDefaultParameters
bool usingDefaultParameters
Definition: otbChannelSelectorFunctor.h:258
otb::Function::ChannelSelectorFunctor::GetBlueChannelIndex
virtual unsigned int GetBlueChannelIndex() const
Definition: otbChannelSelectorFunctor.h:213
otb::find
string_view find(string_view const &haystack, string_view const &needle)
Definition: otbStringUtilities.h:305
otb::Function::ChannelSelectorFunctor::GetGreenChannelIndex
virtual unsigned int GetGreenChannelIndex() const
Definition: otbChannelSelectorFunctor.h:209
otb::Function::ChannelSelectorFunctor::operator()
virtual OutputPixelType operator()(const RGBPixelType &inPixel) const
Definition: otbChannelSelectorFunctor.h:103
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::Function::ChannelSelectorFunctor::RGBAPixelType
itk::RGBAPixel< ScalarType > RGBAPixelType
Definition: otbChannelSelectorFunctor.h:64
otb::Function::ChannelSelectorFunctor::GetOutputSize
virtual unsigned int GetOutputSize() const
Definition: otbChannelSelectorFunctor.h:127
otb::Function::ChannelSelectorFunctor::GetChannelIndex
virtual unsigned int GetChannelIndex(unsigned int channelPosition) const
Definition: otbChannelSelectorFunctor.h:153
otb::Function::ChannelSelectorFunctor::operator()
virtual OutputPixelType operator()(const RGBAPixelType &inPixel) const
Definition: otbChannelSelectorFunctor.h:115
otb::Function::ChannelSelectorFunctor::GetDescription
const char * GetDescription() const
Definition: otbChannelSelectorFunctor.h:70
otb::Function::ChannelSelectorFunctor::RGBPixelType
itk::RGBPixel< ScalarType > RGBPixelType
Definition: otbChannelSelectorFunctor.h:63
otb::Function::ChannelSelectorFunctor::SetChannelList
virtual void SetChannelList(std::vector< unsigned int > channels)
Definition: otbChannelSelectorFunctor.h:137
otb::Function::ChannelSelectorFunctor::OutputPixelType
VectorPixelType OutputPixelType
Definition: otbChannelSelectorFunctor.h:66
otb::Function::ChannelSelectorFunctor::GetRedChannelIndex
virtual unsigned int GetRedChannelIndex() const
Definition: otbChannelSelectorFunctor.h:205
otb::Function::ChannelSelectorFunctor::GetChannelList
virtual std::vector< unsigned int > GetChannelList() const
Definition: otbChannelSelectorFunctor.h:132
otb::Function::ChannelSelectorFunctor::SetAllChannels
virtual void SetAllChannels(unsigned int channel)
Definition: otbChannelSelectorFunctor.h:163
otb::Function::ChannelSelectorFunctor::VectorPixelType
itk::VariableLengthVector< ScalarType > VectorPixelType
Definition: otbChannelSelectorFunctor.h:62
otb::Function::ChannelSelectorFunctor::operator()
virtual OutputPixelType operator()(ScalarType inPixel) const
Definition: otbChannelSelectorFunctor.h:91
otb::Function::ChannelSelectorFunctor::SetGreenChannelIndex
virtual void SetGreenChannelIndex(unsigned int channel)
Definition: otbChannelSelectorFunctor.h:185
otb::Function::ChannelSelectorFunctor::ChannelSelectorFunctor
ChannelSelectorFunctor()
Definition: otbChannelSelectorFunctor.h:225
otb::Function::ChannelSelectorFunctor::ScalarType
itk::NumericTraits< PixelType >::ValueType ScalarType
Definition: otbChannelSelectorFunctor.h:61
otb::Function::ChannelSelectorFunctor::Self
ChannelSelectorFunctor Self
Definition: otbChannelSelectorFunctor.h:49
otb::Function::ChannelSelectorFunctor
Base class for pixel representation functions.
Definition: otbChannelSelectorFunctor.h:45
otb::Function::ChannelSelectorFunctor::Superclass
itk::Object Superclass
Definition: otbChannelSelectorFunctor.h:50
otb::Function::ChannelSelectorFunctor::IsUsingDefaultParameters
virtual bool IsUsingDefaultParameters()
Definition: otbChannelSelectorFunctor.h:218
otb::Function::ChannelSelectorFunctor::SetBlueChannelIndex
virtual void SetBlueChannelIndex(unsigned int channel)
Definition: otbChannelSelectorFunctor.h:195
otb::Function::ChannelSelectorFunctor::~ChannelSelectorFunctor
~ChannelSelectorFunctor() override
Definition: otbChannelSelectorFunctor.h:252
otb::Function::ChannelSelectorFunctor::m_ChannelList
ChannelListType m_ChannelList
Definition: otbChannelSelectorFunctor.h:257
otb::Function::ChannelSelectorFunctor::ChannelListType
std::vector< unsigned int > ChannelListType
Definition: otbChannelSelectorFunctor.h:68
otb::Function::ChannelSelectorFunctor::Pointer
itk::SmartPointer< Self > Pointer
Definition: otbChannelSelectorFunctor.h:51
otb::Function::ChannelSelectorFunctor::SetChannelIndex
virtual void SetChannelIndex(unsigned int channelPosition, unsigned int channel)
Definition: otbChannelSelectorFunctor.h:143
otb::Function::ChannelSelectorFunctor::PixelType
TInputPixel PixelType
Definition: otbChannelSelectorFunctor.h:58