OTB  9.0.0
Orfeo Toolbox
otbImageDimensionalityReductionFilter.hxx
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 #ifndef otbImageDimensionalityReductionFilter_hxx
21 #define otbImageDimensionalityReductionFilter_hxx
22 
24 #include "itkImageRegionIterator.h"
25 #include "itkProgressReporter.h"
26 
27 namespace otb
28 {
32 template <class TInputImage, class TOutputImage, class TMaskImage>
34 {
35  this->SetNumberOfIndexedInputs(2);
36  this->SetNumberOfRequiredInputs(1);
38 
39  this->SetNumberOfRequiredOutputs(2);
40  this->SetNthOutput(0, TOutputImage::New());
41  this->SetNthOutput(1, ConfidenceImageType::New());
42  m_UseConfidenceMap = false;
43  m_BatchMode = true;
44 }
45 
46 template <class TInputImage, class TOutputImage, class TMaskImage>
48 {
49  this->itk::ProcessObject::SetNthInput(1, const_cast<MaskImageType*>(mask));
50 }
51 
52 template <class TInputImage, class TOutputImage, class TMaskImage>
55 {
56  if (this->GetNumberOfInputs() < 2)
57  {
58  return nullptr;
59  }
60  return static_cast<const MaskImageType*>(this->itk::ProcessObject::GetInput(1));
61 }
62 
63 template <class TInputImage, class TOutputImage, class TMaskImage>
66 {
67  if (this->GetNumberOfOutputs() < 2)
68  {
69  return nullptr;
70  }
71  return static_cast<ConfidenceImageType*>(this->itk::ProcessObject::GetOutput(1));
72 }
73 
74 template <class TInputImage, class TOutputImage, class TMaskImage>
76 {
77  if (m_BatchMode)
78  {
79 #ifdef _OPENMP
80  // OpenMP will take care of threading
81  this->SetNumberOfThreads(1);
82 #endif
83  }
84 }
85 
86 template <class TInputImage, class TOutputImage, class TMaskImage>
88  itk::ThreadIdType threadId)
89 {
90  // Get the input pointers
91  InputImageConstPointerType inputPtr = this->GetInput();
92  MaskImageConstPointerType inputMaskPtr = this->GetInputMask();
93  OutputImagePointerType outputPtr = this->GetOutput();
94  ConfidenceImagePointerType confidencePtr = this->GetOutputConfidence();
95 
96  // Progress reporting
97  itk::ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels());
98 
99  // Define iterators
100  typedef itk::ImageRegionConstIterator<InputImageType> InputIteratorType;
101  typedef itk::ImageRegionIterator<OutputImageType> OutputIteratorType;
102 
103  InputIteratorType inIt(inputPtr, outputRegionForThread);
104  OutputIteratorType outIt(outputPtr, outputRegionForThread);
105 
106  // Walk the part of the image
107  for (inIt.GoToBegin(), outIt.GoToBegin(); !inIt.IsAtEnd() && !outIt.IsAtEnd(); ++inIt, ++outIt)
108  {
109  // Classifify
110  outIt.Set(m_Model->Predict(inIt.Get()));
111  progress.CompletedPixel();
112  }
113 }
114 
115 template <class TInputImage, class TOutputImage, class TMaskImage>
117 {
118  Superclass::GenerateOutputInformation();
119  if (!m_Model)
120  {
121  itkGenericExceptionMacro(<< "No model for dimensionality reduction");
122  }
123  this->GetOutput()->SetNumberOfComponentsPerPixel(m_Model->GetDimension());
124 }
125 
126 template <class TInputImage, class TOutputImage, class TMaskImage>
128  itk::ThreadIdType threadId)
129 {
130  // Get the input pointers
131  InputImageConstPointerType inputPtr = this->GetInput();
132  MaskImageConstPointerType inputMaskPtr = this->GetInputMask();
133  OutputImagePointerType outputPtr = this->GetOutput();
134  ConfidenceImagePointerType confidencePtr = this->GetOutputConfidence();
135 
136  // Progress reporting
137  itk::ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels());
138 
139  // Define iterators
140  typedef itk::ImageRegionConstIterator<InputImageType> InputIteratorType;
141  typedef itk::ImageRegionIterator<OutputImageType> OutputIteratorType;
142 
143  InputIteratorType inIt(inputPtr, outputRegionForThread);
144  OutputIteratorType outIt(outputPtr, outputRegionForThread);
145 
146  typedef typename ModelType::InputSampleType InputSampleType;
147  typedef typename ModelType::InputListSampleType InputListSampleType;
148  typedef typename ModelType::TargetValueType TargetValueType;
149  typedef typename ModelType::TargetListSampleType TargetListSampleType;
150 
151  typename InputListSampleType::Pointer samples = InputListSampleType::New();
152  unsigned int num_features = inputPtr->GetNumberOfComponentsPerPixel();
153  samples->SetMeasurementVectorSize(num_features);
154  InputSampleType sample(num_features);
155 
156  // Fill the samples
157  for (inIt.GoToBegin(); !inIt.IsAtEnd(); ++inIt)
158  {
159  typename InputImageType::PixelType pix = inIt.Get();
160  for (size_t feat = 0; feat < num_features; ++feat)
161  {
162  sample[feat] = pix[feat];
163  }
164  samples->PushBack(sample);
165  }
166  // Make the batch prediction
167  typename TargetListSampleType::Pointer labels;
168 
169  // This call is threadsafe
170  labels = m_Model->PredictBatch(samples);
171 
172  // Set the output values
173  typename TargetListSampleType::ConstIterator labIt = labels->Begin();
174  for (outIt.GoToBegin(); !outIt.IsAtEnd(); ++outIt)
175  {
176  itk::VariableLengthVector<TargetValueType> labelValue;
177  labelValue = labIt.GetMeasurementVector();
178  ++labIt;
179  outIt.Set(labelValue);
180  progress.CompletedPixel();
181  }
182 }
183 
184 template <class TInputImage, class TOutputImage, class TMaskImage>
186  itk::ThreadIdType threadId)
187 {
188  if (m_BatchMode)
189  {
190  this->BatchThreadedGenerateData(outputRegionForThread, threadId);
191  }
192  else
193  {
194  this->ClassicThreadedGenerateData(outputRegionForThread, threadId);
195  }
196 }
197 
201 template <class TInputImage, class TOutputImage, class TMaskImage>
203 {
204  Superclass::PrintSelf(os, indent);
205 }
206 
207 } // End namespace otb
208 #endif
otb::ImageDimensionalityReductionFilter::BatchThreadedGenerateData
void BatchThreadedGenerateData(const OutputImageRegionType &outputRegionForThread, itk::ThreadIdType threadId)
Definition: otbImageDimensionalityReductionFilter.hxx:127
otb::MachineLearningModel::TargetListSampleType
itk::Statistics::ListSample< TargetSampleType > TargetListSampleType
Definition: otbMachineLearningModel.h:92
otb::ImageDimensionalityReductionFilter::MaskImageConstPointerType
MaskImageType::ConstPointer MaskImageConstPointerType
Definition: otbImageDimensionalityReductionFilter.h:62
otb::ImageDimensionalityReductionFilter::MaskImageType
TMaskImage MaskImageType
Definition: otbImageDimensionalityReductionFilter.h:61
otb::ImageDimensionalityReductionFilter::GetInputMask
const MaskImageType * GetInputMask(void)
Definition: otbImageDimensionalityReductionFilter.hxx:54
otb::MachineLearningModel::InputListSampleType
itk::Statistics::ListSample< InputSampleType > InputListSampleType
Definition: otbMachineLearningModel.h:85
otb::ImageDimensionalityReductionFilter::ImageDimensionalityReductionFilter
ImageDimensionalityReductionFilter()
Definition: otbImageDimensionalityReductionFilter.hxx:33
otb::ImageDimensionalityReductionFilter::ClassicThreadedGenerateData
void ClassicThreadedGenerateData(const OutputImageRegionType &outputRegionForThread, itk::ThreadIdType threadId)
Definition: otbImageDimensionalityReductionFilter.hxx:87
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::Image
Creation of an "otb" image which contains metadata.
Definition: otbImage.h:89
otb::ImageDimensionalityReductionFilter::ThreadedGenerateData
void ThreadedGenerateData(const OutputImageRegionType &outputRegionForThread, itk::ThreadIdType threadId) override
Definition: otbImageDimensionalityReductionFilter.hxx:185
otbImageDimensionalityReductionFilter.h
otb::ImageDimensionalityReductionFilter::SetInputMask
void SetInputMask(const MaskImageType *mask)
Definition: otbImageDimensionalityReductionFilter.hxx:47
otb::ImageDimensionalityReductionFilter::BeforeThreadedGenerateData
void BeforeThreadedGenerateData() override
Definition: otbImageDimensionalityReductionFilter.hxx:75
otb::ImageDimensionalityReductionFilter::GetOutputConfidence
ConfidenceImageType * GetOutputConfidence(void)
Definition: otbImageDimensionalityReductionFilter.hxx:65
otb::ImageDimensionalityReductionFilter::OutputImageRegionType
OutputImageType::RegionType OutputImageRegionType
Definition: otbImageDimensionalityReductionFilter.h:67
otb::MachineLearningModel::TargetValueType
MLMTargetTraits< TTargetValue >::ValueType TargetValueType
Definition: otbMachineLearningModel.h:90
otb::ImageDimensionalityReductionFilter::PrintSelf
void PrintSelf(std::ostream &os, itk::Indent indent) const override
Definition: otbImageDimensionalityReductionFilter.hxx:202
otb::ImageDimensionalityReductionFilter::OutputImagePointerType
OutputImageType::Pointer OutputImagePointerType
Definition: otbImageDimensionalityReductionFilter.h:66
otb::MachineLearningModel::InputSampleType
MLMSampleTraits< TInputValue >::SampleType InputSampleType
Definition: otbMachineLearningModel.h:84
otb::ImageDimensionalityReductionFilter::InputImageConstPointerType
InputImageType::ConstPointer InputImageConstPointerType
Definition: otbImageDimensionalityReductionFilter.h:58
otb::ImageDimensionalityReductionFilter::GenerateOutputInformation
virtual void GenerateOutputInformation() override
Definition: otbImageDimensionalityReductionFilter.hxx:116
otb::ImageDimensionalityReductionFilter::ConfidenceImagePointerType
ConfidenceImageType::Pointer ConfidenceImagePointerType
Definition: otbImageDimensionalityReductionFilter.h:74