21 #ifndef otbMultiChannelExtractROI_hxx
22 #define otbMultiChannelExtractROI_hxx
26 #include "itkImageRegionIterator.h"
27 #include "itkObjectFactory.h"
28 #include "itkExtractImageFilterRegionCopier.h"
29 #include "itkProgressReporter.h"
37 template <
class TInputPixelType,
class TOutputPixelType>
47 template <
class TInputPixelType,
class TOutputPixelType>
50 if (m_ChannelsKind == 1)
52 itkExceptionMacro(<<
"m_Channels already set using channels interval.");
54 m_Channels.push_back(channel);
55 if (m_ChannelsKind == 0)
63 template <
class TInputPixelType,
class TOutputPixelType>
66 if (m_ChannelsKind == 2)
68 itkExceptionMacro(<<
"m_Channels already set using SetChannels method.");
71 if (m_ChannelsKind == 0)
78 template <
class TInputPixelType,
class TOutputPixelType>
81 if (m_ChannelsKind == 2)
83 itkExceptionMacro(<<
"m_Channels already set using SetChannels method.");
86 if (m_ChannelsKind == 0)
96 template <
class TInputPixelType,
class TOutputPixelType>
103 m_ChannelsWorks.clear();
110 template <
class TInputPixelType,
class TOutputPixelType>
113 Superclass::PrintSelf(os, indent);
116 template <
class TInputPixelType,
class TOutputPixelType>
120 m_ChannelsWorks.clear();
122 if (m_Channels.empty() ==
true)
125 if (m_ChannelsKind == 1)
127 this->SetChannelsWorkWithLimits();
132 if (m_Channels.empty() ==
true && m_ChannelsKind == 2)
134 m_ChannelsWorks = m_Channels;
142 if (m_ChannelsKind == 1)
145 this->SetChannelsWorkWithLimits();
150 if (m_ChannelsKind == 2)
152 m_ChannelsWorks = m_Channels;
158 template <
class TInputPixelType,
class TOutputPixelType>
161 if ((m_FirstChannel == 0) || (m_LastChannel == 0))
163 itkExceptionMacro(<<
"otb::ExtractImageFilter::GenerateOutputInformation "
164 <<
"Channels must reside into [1...] " <<
typeid(itk::ImageBase<InputImageDimension>*).name());
166 if (m_FirstChannel > m_LastChannel)
168 itkExceptionMacro(<<
"otb::ExtractImageFilter::GenerateOutputInformation "
169 <<
"FirstChannel is greater than LastChannel" <<
typeid(itk::ImageBase<InputImageDimension>*).name());
172 for (
unsigned int channel = m_FirstChannel; channel <= m_LastChannel; channel++)
174 m_ChannelsWorks.push_back(channel);
177 m_Channels = m_ChannelsWorks;
189 template <
class TInputPixelType,
class TOutputPixelType>
193 Superclass::GenerateOutputInformation();
194 this->ChannelsReInitialization();
197 typename Superclass::InputImageConstPointer inputPtr = this->GetInput();
198 typename Superclass::OutputImagePointer outputPtr = this->GetOutput();
200 unsigned int nbComponentsPerPixel = inputPtr->GetNumberOfComponentsPerPixel();
201 if (m_ChannelsKind != 0)
205 m_BadChannels.clear();
206 for (
unsigned int i = 0; i < m_ChannelsWorks.size(); ++i)
208 if ((m_ChannelsWorks[i] < 1) || (m_ChannelsWorks[i] > nbComponentsPerPixel))
210 bool isInsideBadChannels =
false;
211 for (
unsigned int j = 0; j < m_BadChannels.size(); ++j)
213 if (m_BadChannels[j] == m_ChannelsWorks[i])
214 isInsideBadChannels =
true;
216 if (!isInsideBadChannels)
217 m_BadChannels.push_back(m_ChannelsWorks[i]);
220 if (m_BadChannels.empty() ==
false)
222 std::ostringstream oss;
223 oss <<
"otb::ExtractImageFilter::GenerateOutputInformation : ";
224 oss <<
"Channel(s) [ ";
225 for (
unsigned int i = 0; i < m_BadChannels.size(); ++i)
227 oss << m_BadChannels[i] <<
" ";
229 oss <<
"] not authorized.";
230 oss <<
" Each channel index has to be in [1," << nbComponentsPerPixel <<
"].";
231 itkExceptionMacro(<< oss.str().c_str());
233 nbComponentsPerPixel = m_ChannelsWorks.size();
237 outputPtr->SetNumberOfComponentsPerPixel(nbComponentsPerPixel);
240 template <
class TInputPixelType,
class TOutputPixelType>
242 itk::ThreadIdType threadId)
244 itkDebugMacro(<<
"Actually executing");
246 typename Superclass::InputImageConstPointer inputPtr = this->GetInput();
247 typename Superclass::OutputImagePointer outputPtr = this->GetOutput();
250 itk::ProgressReporter progress(
this, threadId, outputRegionForThread.GetNumberOfPixels());
254 this->CallCopyOutputRegionToInputRegion(inputRegionForThread, outputRegionForThread);
257 typedef itk::ImageRegionIterator<OutputImageType> OutputIterator;
258 typedef itk::ImageRegionConstIterator<InputImageType> InputIterator;
260 OutputIterator outIt(outputPtr, outputRegionForThread);
261 InputIterator inIt(inputPtr, inputRegionForThread);
267 if (m_ChannelsKind == 0)
270 while (!outIt.IsAtEnd())
272 outIt.Set(inIt.Get());
275 progress.CompletedPixel();
282 unsigned int channelIn(0);
283 unsigned int channelOut(0);
284 unsigned int nbChannels(0);
287 while (!outIt.IsAtEnd())
290 pixelOutput.Reserve(outputPtr->GetVectorLength());
291 pixelInput = inIt.Get();
293 for (nbChannels = 0; nbChannels < m_ChannelsWorks.size(); ++nbChannels)
295 channelIn = m_ChannelsWorks[nbChannels] - 1;
296 pixelOutput[channelOut] =
static_cast<OutputValueType>(pixelInput[channelIn]);
299 outIt.Set(pixelOutput);
302 progress.CompletedPixel();