20 #ifndef otbImageDimensionalityReductionFilter_hxx
21 #define otbImageDimensionalityReductionFilter_hxx
24 #include "itkImageRegionIterator.h"
25 #include "itkProgressReporter.h"
32 template <
class TInputImage,
class TOutputImage,
class TMaskImage>
35 this->SetNumberOfIndexedInputs(2);
36 this->SetNumberOfRequiredInputs(1);
39 this->SetNumberOfRequiredOutputs(2);
40 this->SetNthOutput(0, TOutputImage::New());
41 this->SetNthOutput(1, ConfidenceImageType::New());
42 m_UseConfidenceMap =
false;
46 template <
class TInputImage,
class TOutputImage,
class TMaskImage>
49 this->itk::ProcessObject::SetNthInput(1,
const_cast<MaskImageType*
>(mask));
52 template <
class TInputImage,
class TOutputImage,
class TMaskImage>
56 if (this->GetNumberOfInputs() < 2)
60 return static_cast<const MaskImageType*
>(this->itk::ProcessObject::GetInput(1));
63 template <
class TInputImage,
class TOutputImage,
class TMaskImage>
67 if (this->GetNumberOfOutputs() < 2)
74 template <
class TInputImage,
class TOutputImage,
class TMaskImage>
81 this->SetNumberOfThreads(1);
86 template <
class TInputImage,
class TOutputImage,
class TMaskImage>
88 itk::ThreadIdType threadId)
97 itk::ProgressReporter progress(
this, threadId, outputRegionForThread.GetNumberOfPixels());
100 typedef itk::ImageRegionConstIterator<InputImageType> InputIteratorType;
101 typedef itk::ImageRegionIterator<OutputImageType> OutputIteratorType;
103 InputIteratorType inIt(inputPtr, outputRegionForThread);
104 OutputIteratorType outIt(outputPtr, outputRegionForThread);
107 for (inIt.GoToBegin(), outIt.GoToBegin(); !inIt.IsAtEnd() && !outIt.IsAtEnd(); ++inIt, ++outIt)
110 outIt.Set(m_Model->Predict(inIt.Get()));
111 progress.CompletedPixel();
115 template <
class TInputImage,
class TOutputImage,
class TMaskImage>
118 Superclass::GenerateOutputInformation();
121 itkGenericExceptionMacro(<<
"No model for dimensionality reduction");
123 this->GetOutput()->SetNumberOfComponentsPerPixel(m_Model->GetDimension());
126 template <
class TInputImage,
class TOutputImage,
class TMaskImage>
128 itk::ThreadIdType threadId)
137 itk::ProgressReporter progress(
this, threadId, outputRegionForThread.GetNumberOfPixels());
140 typedef itk::ImageRegionConstIterator<InputImageType> InputIteratorType;
141 typedef itk::ImageRegionIterator<OutputImageType> OutputIteratorType;
143 InputIteratorType inIt(inputPtr, outputRegionForThread);
144 OutputIteratorType outIt(outputPtr, outputRegionForThread);
151 typename InputListSampleType::Pointer samples = InputListSampleType::New();
152 unsigned int num_features = inputPtr->GetNumberOfComponentsPerPixel();
153 samples->SetMeasurementVectorSize(num_features);
154 InputSampleType sample(num_features);
157 for (inIt.GoToBegin(); !inIt.IsAtEnd(); ++inIt)
159 typename InputImageType::PixelType pix = inIt.Get();
160 for (
size_t feat = 0; feat < num_features; ++feat)
162 sample[feat] = pix[feat];
164 samples->PushBack(sample);
167 typename TargetListSampleType::Pointer labels;
170 labels = m_Model->PredictBatch(samples);
173 typename TargetListSampleType::ConstIterator labIt = labels->Begin();
174 for (outIt.GoToBegin(); !outIt.IsAtEnd(); ++outIt)
176 itk::VariableLengthVector<TargetValueType> labelValue;
177 labelValue = labIt.GetMeasurementVector();
179 outIt.Set(labelValue);
180 progress.CompletedPixel();
184 template <
class TInputImage,
class TOutputImage,
class TMaskImage>
186 itk::ThreadIdType threadId)
190 this->BatchThreadedGenerateData(outputRegionForThread, threadId);
194 this->ClassicThreadedGenerateData(outputRegionForThread, threadId);
201 template <
class TInputImage,
class TOutputImage,
class TMaskImage>
204 Superclass::PrintSelf(os, indent);