OTB  9.0.0
Orfeo Toolbox
otbSynthetizeFilter.h
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 
21 #ifndef otbSynthetizeFilter_h
22 #define otbSynthetizeFilter_h
23 
24 #include "otbZipIterator.h"
25 #include "itkImageToImageFilter.h"
26 #include "itkImageScanlineConstIterator.h"
27 #include "itkImageScanlineIterator.h"
28 #include "itkProgressReporter.h"
29 
30 namespace otb
31 {
46 template <typename TInputImage, typename TOutputImage, typename TFunctor>
47 class SynthetizeFilter : public itk::ImageToImageFilter<TInputImage, TOutputImage>
48 {
49 public:
50 
53  using InputImageType = TInputImage;
54  using OutputImageType = TOutputImage;
55  using FunctorType = TFunctor;
57 
60  itkStaticConstMacro(InputImageDimension, unsigned int, InputImageType::ImageDimension);
61  itkStaticConstMacro(OutputImageDimension, unsigned int, OutputImageType::ImageDimension);
63 
67  using Superclass = itk::ImageToImageFilter<InputImageType, OutputImageType>;
68  using Pointer = itk::SmartPointer<Self>;
69  using ConstPointer = itk::SmartPointer<const Self>;
71 
73  static Pointer New(FunctorType functor)
74  {
75  Pointer smartPtr = new Self(std::move(functor));
76  smartPtr->UnRegister();
77  return smartPtr;
78  }
80 
82  itkTypeMacro(SynthetizeFilter, ImageToImageFilter);
83 
86  using InputPixelType = typename InputImageType::PixelType;
87  using OutputPixelType = typename OutputImageType::PixelType;
88  using InputRealType = typename itk::NumericTraits<InputPixelType>::RealType;
89  using InputImageRegionType = typename InputImageType::RegionType;
90  using OutputImageRegionType = typename OutputImageType::RegionType;
91  using InputSizeType = typename InputImageType::SizeType;
92  using OutputIndexType = typename OutputImageType::IndexType;
93  using OutputSizeType = typename OutputImageType::SizeType;
94 
95  static_assert(InputImageDimension == OutputImageDimension, "Images have the same number of components");
96 
97  using DataObjectPointerArraySizeType = itk::ProcessObject::DataObjectPointerArraySizeType;
99 
106  using Superclass::SetNthInput;
107  using Superclass::GetInput;
108 
111  {
112  return const_cast<InputImageType*>(this->GetInput(idx));
113  }
114 
116  std::vector<InputImageType const*> GetInputs() const
117  {
118  std::vector<InputImageType const*> res;
119  auto const nbInputImages = this->GetNumberOfInputs();
120  res.reserve(nbInputImages);
121  for (std::size_t i = 0 ; i != nbInputImages ; ++i)
122  res.push_back(this->GetInput(i));
123  return res;
124  }
126 
127 
128 protected:
130  explicit SynthetizeFilter(FunctorType functor)
131  : m_functor(functor){}
132  ~SynthetizeFilter() = default;
133 
136  {
137  Superclass::GenerateOutputInformation();
139  }
141 
142  // void GenerateInputRequestedRegion() override;
143  // +-> TODO: detect neighborhood to apply pad radius
144 
151  OutputImageRegionType const& outputRegionForThread,
152  itk::ThreadIdType threadId) override
153  {
154  using ImageScanlineConstIteratorType = itk::ImageScanlineConstIterator<InputImageType const>;
155  // using OutImageScanlineConstIteratorType = itk::ImageScanlineIterator<OutputImageType>;
156  using OutputIterator = itk::ImageScanlineIterator<OutputImageType>;
158 
159  auto const regSizeY = outputRegionForThread.GetSize()[1];
160  itk::ProgressReporter progress( this, threadId, outputRegionForThread.GetNumberOfPixels() / regSizeY );
161 
162  InputIterator inputIterator(this->GetInputs(), outputRegionForThread);
163  OutputIterator outputIterator(this->GetOutput(), outputRegionForThread);
164 
165  inputIterator.GoToBegin();
166  outputIterator.GoToBegin();
167  for (
168  ; !inputIterator.IsAtEnd()
169  ; inputIterator.NextLine(), outputIterator.NextLine())
170  {
171  assert(! outputIterator.IsAtEnd());
172  // inputIterator.GoToBeginOfLine();
173  // outputIterator.GoToBeginOfLine();
174  for (
175  ; !inputIterator.IsAtEndOfLine()
176  ; ++inputIterator, ++outputIterator)
177  {
178  assert(!outputIterator.IsAtEndOfLine());
179 
180  outputIterator.Set(m_functor(inputIterator.Get()));
181  }
182  progress.CompletedPixel(); // Completed...Line()
183  }
184  }
185 
186 private:
187 
189  {
190  // Check if input image dimensions match
191  auto const nbInputImages = this->GetNumberOfInputs();
192  auto const& inputSize = this->GetInput(0)->GetLargestPossibleRegion().GetSize();
193 
194  for (auto p = 1U; p < nbInputImages; ++p)
195  {
196  auto const& regionSize = this->GetInput(p)->GetLargestPossibleRegion().GetSize();
197  if (inputSize != regionSize)
198  {
199  itkExceptionMacro(<< "Input images must have the same dimensions.\n"
200  << "band #1 is [" << inputSize[0] << ";" << inputSize[1] << "]\n"
201  << "band #" << p + 1 << " is [" << this->GetInput(p)->GetLargestPossibleRegion().GetSize(0) << ";"
202  << regionSize << "]");
203  }
204  }
205  }
206 
211 
212 };
213 
229 template <typename TInputImage, typename TOutputImage, typename TFunctor>
230 auto MakeSynthetizeFilter(TFunctor functor)
231 {
232  auto filter = SynthetizeFilter<TInputImage, TOutputImage, TFunctor>::New(std::move(functor));
233  return filter;
234 }
236 
237 } // otb namespace
238 
239 #ifndef OTB_MANUAL_INSTANTIATION
240 // #include "otbSynthetizeFilter.hxx"
241 #endif
242 
243 #endif // otbSynthetizeFilter_h
otb::SynthetizeFilter::OutputImageRegionType
typename OutputImageType::RegionType OutputImageRegionType
Definition: otbSynthetizeFilter.h:90
otb::SynthetizeFilter::GetInputs
std::vector< InputImageType const * > GetInputs() const
Definition: otbSynthetizeFilter.h:116
otb::SynthetizeFilter::InputPixelType
typename InputImageType::PixelType InputPixelType
Definition: otbSynthetizeFilter.h:86
otb::SynthetizeFilter::m_functor
FunctorType m_functor
Definition: otbSynthetizeFilter.h:210
otb::SynthetizeFilter::InputImageRegionType
typename InputImageType::RegionType InputImageRegionType
Definition: otbSynthetizeFilter.h:89
otb::SynthetizeFilter::Self
SynthetizeFilter Self
Definition: otbSynthetizeFilter.h:66
otb::SynthetizeFilter::OutputIndexType
typename OutputImageType::IndexType OutputIndexType
Definition: otbSynthetizeFilter.h:92
otb::SynthetizeFilter::GetNthInput
InputImageType * GetNthInput(DataObjectPointerArraySizeType idx)
Definition: otbSynthetizeFilter.h:110
otb::SynthetizeFilter::DataObjectPointerArraySizeType
itk::ProcessObject::DataObjectPointerArraySizeType DataObjectPointerArraySizeType
Definition: otbSynthetizeFilter.h:97
otb::SynthetizeFilter::OutputImageType
TOutputImage OutputImageType
Definition: otbSynthetizeFilter.h:54
otb::SynthetizeFilter::OutputPixelType
typename OutputImageType::PixelType OutputPixelType
Definition: otbSynthetizeFilter.h:87
otb::SynthetizeFilter::InputRealType
typename itk::NumericTraits< InputPixelType >::RealType InputRealType
Definition: otbSynthetizeFilter.h:88
otb::SynthetizeFilter::SynthetizeFilter
SynthetizeFilter(FunctorType functor)
Definition: otbSynthetizeFilter.h:130
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::SynthetizeFilter::InputImageType
TInputImage InputImageType
Definition: otbSynthetizeFilter.h:53
otbZipIterator.h
otb::SynthetizeFilter::InputImageDimension
static const unsigned int InputImageDimension
Definition: otbSynthetizeFilter.h:60
otb::SynthetizeFilter
Definition: otbSynthetizeFilter.h:47
otb::SynthetizeFilter::FunctorType
TFunctor FunctorType
Definition: otbSynthetizeFilter.h:55
otb::SynthetizeFilter::New
static Pointer New(FunctorType functor)
Definition: otbSynthetizeFilter.h:73
otb::SynthetizeFilter::Superclass
itk::ImageToImageFilter< InputImageType, OutputImageType > Superclass
Definition: otbSynthetizeFilter.h:67
otb::SynthetizeFilter::GenerateOutputInformation
void GenerateOutputInformation() override
Definition: otbSynthetizeFilter.h:135
otb::SynthetizeFilter::Pointer
itk::SmartPointer< Self > Pointer
Definition: otbSynthetizeFilter.h:68
otb::internals::ZipIterator
Definition: otbZipIterator.h:47
otb::SynthetizeFilter::~SynthetizeFilter
~SynthetizeFilter()=default
otb::SynthetizeFilter::CheckInputImageDimensions
void CheckInputImageDimensions()
Definition: otbSynthetizeFilter.h:188
otb::SynthetizeFilter::ThreadedGenerateData
void ThreadedGenerateData(OutputImageRegionType const &outputRegionForThread, itk::ThreadIdType threadId) override
Definition: otbSynthetizeFilter.h:150
otb::SynthetizeFilter::ConstPointer
itk::SmartPointer< const Self > ConstPointer
Definition: otbSynthetizeFilter.h:69
otb::SynthetizeFilter::InputSizeType
typename InputImageType::SizeType InputSizeType
Definition: otbSynthetizeFilter.h:91
otb::MakeSynthetizeFilter
auto MakeSynthetizeFilter(TFunctor functor)
Definition: otbSynthetizeFilter.h:230
otb::SynthetizeFilter::OutputImageDimension
static const unsigned int OutputImageDimension
Definition: otbSynthetizeFilter.h:61
otb::SynthetizeFilter::OutputSizeType
typename OutputImageType::SizeType OutputSizeType
Definition: otbSynthetizeFilter.h:93