OTB  10.0.0
Orfeo Toolbox
otbChannelSelectorFunctor.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2024 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  using Superclass = itk::Object;
51  using Pointer = itk::SmartPointer<Self>;
52  using ConstPointer = itk::SmartPointer<const Self>;
53 
55  itkNewMacro(Self);
56 
58  itkTypeMacro(ChannelSelectorFunctor, itk::Object);
59 
60  using PixelType = TInputPixel;
61  using ScalarType = typename itk::NumericTraits<PixelType>::ValueType;
62  using VectorPixelType = itk::VariableLengthVector<ScalarType>;
63  using RGBPixelType = itk::RGBPixel<ScalarType>;
64  using RGBAPixelType = itk::RGBAPixel<ScalarType>;
66  using ChannelListType = std::vector<unsigned int>;
67 
68  const char* GetDescription() const
69  {
70  return "Channel Selection";
71  }
72 
73  virtual OutputPixelType operator()(const VectorPixelType& inPixel) const
74  {
75  // otbMsgDevMacro(<<"Channel list "<< m_ChannelList[0]);
76  OutputPixelType outPixel;
77  outPixel.SetSize(m_ChannelList.size());
78  for (unsigned int i = 0; i < m_ChannelList.size(); ++i)
79  {
80  // assert as the verification should be done outside and only
81  // once for the image, not for every pixel, when we reach this point
82  // the m_ChannelList MUST be valid
83  assert(m_ChannelList[i] < inPixel.Size());
84  outPixel[i] = inPixel[m_ChannelList[i]];
85  }
86  return outPixel;
87  }
88 
89  virtual OutputPixelType operator()(ScalarType inPixel) const
90  {
91  OutputPixelType outPixel;
92  outPixel.SetSize(1);
93  for (unsigned int i = 0; i < m_ChannelList.size(); ++i)
94  {
95  assert(m_ChannelList[i] < 1);
96  outPixel[0] = inPixel;
97  }
98  return outPixel;
99  }
100 
101  virtual OutputPixelType operator()(const RGBPixelType& inPixel) const
102  {
103  OutputPixelType outPixel;
104  outPixel.SetSize(m_ChannelList.size());
105  for (unsigned int i = 0; i < m_ChannelList.size(); ++i)
106  {
107  assert(m_ChannelList[i] < 3);
108  outPixel[i] = inPixel[m_ChannelList[i]];
109  }
110  return outPixel;
111  }
112 
113  virtual OutputPixelType operator()(const RGBAPixelType& inPixel) const
114  {
115  OutputPixelType outPixel;
116  outPixel.SetSize(m_ChannelList.size());
117  for (unsigned int i = 0; i < m_ChannelList.size(); ++i)
118  {
119  assert(m_ChannelList[i] < 4);
120  outPixel[i] = inPixel[m_ChannelList[i]];
121  }
122  return outPixel;
123  }
124 
125  virtual unsigned int GetOutputSize() const
126  {
127  return m_ChannelList.size();
128  }
129 
130  virtual std::vector<unsigned int> GetChannelList() const
131  {
132  return m_ChannelList;
133  }
134 
135  virtual void SetChannelList(std::vector<unsigned int> channels)
136  {
137  m_ChannelList = channels;
138  usingDefaultParameters = false;
139  }
140 
141  virtual void SetChannelIndex(unsigned int channelPosition, unsigned int channel)
142  {
143  if (m_ChannelList.size() < channelPosition + 1)
144  {
145  m_ChannelList.resize(channelPosition + 1, 0);
146  }
147  m_ChannelList[channelPosition] = channel;
148  usingDefaultParameters = false;
149  }
150 
151  virtual unsigned int GetChannelIndex(unsigned int channelPosition) const
152  {
153  if (channelPosition >= m_ChannelList.size())
154  {
155  itkExceptionMacro(<< "Can't get channel " << channelPosition << ", there is only " << m_ChannelList.size() << " element in the list");
156  }
157  return m_ChannelList[channelPosition];
158  }
159 
161  virtual void SetAllChannels(unsigned int channel)
162  {
163  if (m_ChannelList.size() < 3)
164  {
165  m_ChannelList.resize(3, 0);
166  }
167  m_ChannelList[0] = channel;
168  m_ChannelList[1] = channel;
169  m_ChannelList[2] = channel;
170  usingDefaultParameters = false;
171  }
172  virtual void SetRedChannelIndex(unsigned int channel)
173  {
174  if (m_ChannelList.size() < 1)
175  {
176  m_ChannelList.resize(3, 0);
177  }
178  m_ChannelList[0] = channel;
179  usingDefaultParameters = false;
180  }
182 
183  virtual void SetGreenChannelIndex(unsigned int channel)
184  {
185  if (m_ChannelList.size() < 2)
186  {
187  m_ChannelList.resize(3, 0);
188  }
189  m_ChannelList[1] = channel;
190  usingDefaultParameters = false;
191  }
192 
193  virtual void SetBlueChannelIndex(unsigned int channel)
194  {
195  if (m_ChannelList.size() < 3)
196  {
197  m_ChannelList.resize(3, 0);
198  }
199  m_ChannelList[2] = channel;
200  usingDefaultParameters = false;
201  }
202 
203  virtual unsigned int GetRedChannelIndex() const
204  {
205  return m_ChannelList[0];
206  }
207  virtual unsigned int GetGreenChannelIndex() const
208  {
209  return m_ChannelList[1];
210  }
211  virtual unsigned int GetBlueChannelIndex() const
212  {
213  return m_ChannelList[2];
214  }
215 
217  {
218  return usingDefaultParameters;
219  }
220 
221 protected:
224  {
225  if (std::string(typeid(PixelType).name()).find("RGBAPixel") != std::string::npos)
226  {
227  m_ChannelList.push_back(0);
228  m_ChannelList.push_back(1);
229  m_ChannelList.push_back(2);
230  m_ChannelList.push_back(3);
231  }
232  else if (std::string(typeid(PixelType).name()).find("RGBPixel") != std::string::npos)
233  {
234  m_ChannelList.push_back(0);
235  m_ChannelList.push_back(1);
236  m_ChannelList.push_back(2);
237  }
238  else if (std::string(typeid(PixelType).name()).find("VariableLengthVector") != std::string::npos)
239  {
240  m_ChannelList.push_back(0);
241  }
242  else
243  {
244  m_ChannelList.push_back(0);
245  }
246  }
248 
251 
252 private:
255 };
256 }
257 }
258 
259 #endif
Base class for pixel representation functions.
virtual unsigned int GetGreenChannelIndex() const
virtual unsigned int GetRedChannelIndex() const
itk::SmartPointer< const Self > ConstPointer
virtual unsigned int GetBlueChannelIndex() const
virtual unsigned int GetChannelIndex(unsigned int channelPosition) const
virtual std::vector< unsigned int > GetChannelList() const
virtual void SetAllChannels(unsigned int channel)
typename itk::NumericTraits< PixelType >::ValueType ScalarType
virtual void SetRedChannelIndex(unsigned int channel)
itk::VariableLengthVector< ScalarType > VectorPixelType
virtual void SetChannelIndex(unsigned int channelPosition, unsigned int channel)
virtual void SetGreenChannelIndex(unsigned int channel)
virtual void SetChannelList(std::vector< unsigned int > channels)
virtual void SetBlueChannelIndex(unsigned int channel)
virtual OutputPixelType operator()(const RGBAPixelType &inPixel) const
virtual OutputPixelType operator()(const VectorPixelType &inPixel) const
virtual OutputPixelType operator()(const RGBPixelType &inPixel) const
virtual OutputPixelType operator()(ScalarType inPixel) const
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
string_view find(string_view const &haystack, string_view const &needle)