File: otbChannelSelectorFunctor.h

package info (click to toggle)
otb 5.8.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 38,496 kB
  • ctags: 40,282
  • sloc: cpp: 306,573; ansic: 3,575; python: 450; sh: 214; perl: 74; java: 72; makefile: 70
file content (260 lines) | stat: -rw-r--r-- 6,848 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*=========================================================================

  Program:   ORFEO Toolbox
  Language:  C++
  Date:      $Date$
  Version:   $Revision$


  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
  See OTBCopyright.txt for details.


     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/
#ifndef otbChannelSelectorFunctor_h
#define otbChannelSelectorFunctor_h
#include <cassert>

#include "itkVariableLengthVector.h"
#include "itkRGBPixel.h"
#include "itkRGBAPixel.h"
#include "itkObject.h"
#include "itkObjectFactory.h"

namespace otb
{
namespace Function
{
/** \class ChannelSelectorFunctor
* \brief Base class for pixel representation functions.
*
*  \ingroup Visualization
*
* \sa AmplitudeFunctor PhaseFunctor
 *
 * \ingroup OTBCommon
*/
template <class TInputPixel>
class ChannelSelectorFunctor : public itk::Object
{
public:
  /** Standard class typedefs */
  typedef ChannelSelectorFunctor        Self;
  typedef itk::Object                   Superclass;
  typedef itk::SmartPointer<Self>       Pointer;
  typedef itk::SmartPointer<const Self> ConstPointer;
  /** Method for creation through the object factory */
  itkNewMacro(Self);

  /** Runtime information */
  itkTypeMacro(ChannelSelectorFunctor, itk::Object);

  typedef TInputPixel                                       PixelType;
  typedef typename itk::NumericTraits<PixelType>::ValueType ScalarType;
  typedef itk::VariableLengthVector<ScalarType>             VectorPixelType;
  typedef itk::RGBPixel<ScalarType>                         RGBPixelType;
  typedef itk::RGBAPixel<ScalarType>                        RGBAPixelType;

  typedef VectorPixelType OutputPixelType;

  typedef std::vector<unsigned int> ChannelListType;

  const char *GetDescription() const
  {return "Channel Selection"; }

  virtual OutputPixelType operator ()(const VectorPixelType& inPixel) const
  {
//           otbMsgDevMacro(<<"Channel list "<< m_ChannelList[0]);
    OutputPixelType outPixel;
    outPixel.SetSize(m_ChannelList.size());
    for (unsigned int i = 0; i < m_ChannelList.size(); ++i)
      {
      // assert as the verification should be done outside and only
      // once for the image, not for every pixel, when we reach this point
      // the m_ChannelList MUST be valid
      assert(m_ChannelList[i] < inPixel.Size());
      outPixel[i] = inPixel[m_ChannelList[i]];
      }
    return outPixel;
  }

  virtual OutputPixelType operator ()(ScalarType inPixel) const
  {
    OutputPixelType outPixel;
    outPixel.SetSize(1);
    for (unsigned int i = 0; i < m_ChannelList.size(); ++i)
      {
      assert(m_ChannelList[i] < 1);
      outPixel[0] = inPixel;
      }
    return outPixel;
  }

  virtual OutputPixelType operator ()(const RGBPixelType& inPixel) const
  {
    OutputPixelType outPixel;
    outPixel.SetSize(m_ChannelList.size());
    for (unsigned int i = 0; i < m_ChannelList.size(); ++i)
      {
      assert(m_ChannelList[i] < 3);
      outPixel[i] = inPixel[m_ChannelList[i]];
      }
    return outPixel;
  }

  virtual OutputPixelType operator ()(const RGBAPixelType& inPixel) const
  {
    OutputPixelType outPixel;
    outPixel.SetSize(m_ChannelList.size());
    for (unsigned int i = 0; i < m_ChannelList.size(); ++i)
      {
      assert(m_ChannelList[i] < 4);
      outPixel[i] = inPixel[m_ChannelList[i]];
      }
    return outPixel;
  }

  virtual unsigned int GetOutputSize() const
  {
    return m_ChannelList.size();
  }

  virtual std::vector<unsigned int> GetChannelList() const
  {
    return m_ChannelList;
  }

  virtual void SetChannelList(std::vector<unsigned int> channels)
  {
    m_ChannelList = channels;
    usingDefaultParameters = false;
  }

  virtual void SetChannelIndex(unsigned int channelPosition, unsigned int channel)
  {
    if (m_ChannelList.size() < channelPosition + 1)
      {
      m_ChannelList.resize(channelPosition + 1, 0);
      }
    m_ChannelList[channelPosition] = channel;
    usingDefaultParameters = false;
  }

  virtual unsigned int GetChannelIndex(unsigned int channelPosition) const
  {
    if (channelPosition >= m_ChannelList.size())
      {
      itkExceptionMacro(
        << "Can't get channel " << channelPosition << ", there is only " << m_ChannelList.size() <<
        " element in the list");
      }
    return m_ChannelList[channelPosition];

  }

  /** Only for backward compatibility but should not be used*/
  virtual void SetAllChannels(unsigned int channel)
  {
    if (m_ChannelList.size() < 3)
      {
      m_ChannelList.resize(3, 0);
      }
    m_ChannelList[0] = channel;
    m_ChannelList[1] = channel;
    m_ChannelList[2] = channel;
    usingDefaultParameters = false;
  }
  virtual void SetRedChannelIndex(unsigned int channel)
  {
    if (m_ChannelList.size() < 1)
      {
      m_ChannelList.resize(3, 0);
      }
    m_ChannelList[0] = channel;
    usingDefaultParameters = false;
  }

  virtual void SetGreenChannelIndex(unsigned int channel)
  {
    if (m_ChannelList.size() < 2)
      {
      m_ChannelList.resize(3, 0);
      }
    m_ChannelList[1] = channel;
    usingDefaultParameters = false;
  }

  virtual void SetBlueChannelIndex(unsigned int channel)
  {
    if (m_ChannelList.size() < 3)
      {
      m_ChannelList.resize(3, 0);
      }
    m_ChannelList[2] = channel;
    usingDefaultParameters = false;
  }

  virtual unsigned int GetRedChannelIndex() const
  {
    return m_ChannelList[0];
  }
  virtual unsigned int GetGreenChannelIndex() const
  {
    return m_ChannelList[1];
  }
  virtual unsigned int GetBlueChannelIndex() const
  {
    return m_ChannelList[2];
  }

  virtual bool IsUsingDefaultParameters()
  {
    return usingDefaultParameters;
  }
protected:

  /** Constructor */
  ChannelSelectorFunctor() :
    usingDefaultParameters(true)
  {
    if (std::string(typeid(PixelType).name()).find("RGBAPixel") != std::string::npos)
      {
      m_ChannelList.push_back(0);
      m_ChannelList.push_back(1);
      m_ChannelList.push_back(2);
      m_ChannelList.push_back(3);
      }
    else if (std::string(typeid(PixelType).name()).find("RGBPixel") != std::string::npos)
      {
      m_ChannelList.push_back(0);
      m_ChannelList.push_back(1);
      m_ChannelList.push_back(2);
      }
    else if (std::string(typeid(PixelType).name()).find("VariableLengthVector") != std::string::npos)
      {
      m_ChannelList.push_back(0);
      }
    else
      {
      m_ChannelList.push_back(0);
      }
  }

  /** Destructor */
  ~ChannelSelectorFunctor() ITK_OVERRIDE {}

private:

  ChannelListType m_ChannelList;
  bool            usingDefaultParameters;

};

}
}

#endif