OTB  9.0.0
Orfeo Toolbox
otbFunctionWithNeighborhoodToImageFilter.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1999-2011 Insight Software Consortium
3  * Copyright (C) 2005-2022 Centre National d'Etudes Spatiales (CNES)
4  *
5  * This file is part of Orfeo Toolbox
6  *
7  * https://www.orfeo-toolbox.org/
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21 
22 #ifndef otbFunctionWithNeighborhoodToImageFilter_hxx
23 #define otbFunctionWithNeighborhoodToImageFilter_hxx
24 
26 #include "otbMacro.h"
27 
28 namespace otb
29 {
30 
34 template <class TInputImage, class TOutputImage, class TFunction>
36 {
37  this->InPlaceOff();
38  this->SetNumberOfRequiredInputs(1);
39  m_Radius.Fill(1);
40  m_Offset.Fill(1);
41  m_FunctionList.clear();
42  m_Function = FunctionType::New();
43 }
45 
46 template <class TInputImage, class TOutputImage, class TFunction>
48 {
49  Superclass::BeforeThreadedGenerateData();
50 
51  InputImageConstPointer inputPtr = dynamic_cast<const TInputImage*>((itk::ProcessObject::GetInput(0)));
52  if (inputPtr.IsNull())
53  {
54  itkExceptionMacro(<< "At least one input is missing."
55  << " Input is missing :" << inputPtr.GetPointer();)
56  }
57  m_Function->SetInputImage(inputPtr);
58  for (unsigned int i = 0; i < static_cast<unsigned int>(this->GetNumberOfThreads()); ++i)
59  {
60  FunctionPointerType func = m_Function;
61  m_FunctionList.push_back(func);
62  }
63 }
64 
65 template <class TInputImage, class TOutputImage, class TFunction>
67 {
68  // call the superclass' implementation of this method
69  Superclass::GenerateInputRequestedRegion();
70 
71  // get pointers to the input and output
72  InputImagePointer inputPtr = const_cast<TInputImage*>(this->GetInput());
73  OutputImagePointer outputPtr = this->GetOutput();
74 
75  if (!inputPtr || !outputPtr)
76  {
77  return;
78  }
79  // get a copy of the input requested region (should equal the output
80  // requested region)
81  InputImageRegionType inputRequestedRegion = inputPtr->GetRequestedRegion();
82 
83  // pad the input requested region by the operator radius
84  InputImageSizeType maxRad;
85  maxRad[0] = m_Radius[0] + std::abs(m_Offset[0]);
86  maxRad[1] = m_Radius[0] + std::abs(m_Offset[1]);
87  inputRequestedRegion.PadByRadius(maxRad);
88 
89  // crop the input requested region at the input's largest possible region
90  if (inputRequestedRegion.Crop(inputPtr->GetLargestPossibleRegion()))
91  {
92  inputPtr->SetRequestedRegion(inputRequestedRegion);
93  return;
94  }
95  else
96  {
97  // Couldn't crop the region (requested region is outside the largest
98  // possible region). Throw an exception.
99 
100  // store what we tried to request (prior to trying to crop)
101  inputPtr->SetRequestedRegion(inputRequestedRegion);
102 
103  // build an exception
104  itk::InvalidRequestedRegionError e(__FILE__, __LINE__);
105  std::ostringstream msg;
106  msg << this->GetNameOfClass() << "::GenerateInputRequestedRegion()";
107  e.SetLocation(msg.str());
108  e.SetDescription("Requested region is (at least partially) outside the largest possible region.");
109  e.SetDataObject(inputPtr);
110  throw e;
111  }
112 }
113 
117 template <class TInputImage, class TOutputImage, class TFunction>
119  itk::ThreadIdType threadId)
120 {
121  // We use dynamic_cast since inputs are stored as DataObjects.
122  InputImageConstPointer inputPtr = dynamic_cast<const TInputImage*>((itk::ProcessObject::GetInput(0)));
123 
124  OutputImagePointer outputPtr = this->GetOutput(0);
125 
126  itk::ImageRegionConstIterator<TInputImage> inputIt(inputPtr, outputRegionForThread);
127  itk::ImageRegionIterator<TOutputImage> outputIt(outputPtr, outputRegionForThread);
128 
129  itk::ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels());
130 
131  inputIt.GoToBegin();
132  outputIt.GoToBegin();
133 
134  while (!inputIt.IsAtEnd())
135  {
136  outputIt.Set(static_cast<OutputImagePixelType>(m_FunctionList[threadId]->EvaluateAtIndex(inputIt.GetIndex())));
137  ++inputIt;
138  ++outputIt;
139 
140  progress.CompletedPixel(); // potential exception thrown here
141  }
142 }
143 } // end namespace otb
144 
145 #endif
otbFunctionWithNeighborhoodToImageFilter.h
otb::FunctionWithNeighborhoodToImageFilter::InputImageSizeType
InputImageType::SizeType InputImageSizeType
Definition: otbFunctionWithNeighborhoodToImageFilter.h:74
otb::FunctionWithNeighborhoodToImageFilter::ThreadedGenerateData
void ThreadedGenerateData(const OutputImageRegionType &outputRegionForThread, itk::ThreadIdType threadId) override
Definition: otbFunctionWithNeighborhoodToImageFilter.hxx:118
otb::FunctionWithNeighborhoodToImageFilter::OutputImageRegionType
OutputImageType::RegionType OutputImageRegionType
Definition: otbFunctionWithNeighborhoodToImageFilter.h:78
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otbMacro.h
otb::FunctionWithNeighborhoodToImageFilter::GenerateInputRequestedRegion
void GenerateInputRequestedRegion() override
Definition: otbFunctionWithNeighborhoodToImageFilter.hxx:66
otb::FunctionWithNeighborhoodToImageFilter::FunctionWithNeighborhoodToImageFilter
FunctionWithNeighborhoodToImageFilter()
Definition: otbFunctionWithNeighborhoodToImageFilter.hxx:35
otb::FunctionWithNeighborhoodToImageFilter::OutputImagePixelType
OutputImageType::PixelType OutputImagePixelType
Definition: otbFunctionWithNeighborhoodToImageFilter.h:79
otb::FunctionWithNeighborhoodToImageFilter::FunctionPointerType
FunctionType::Pointer FunctionPointerType
Definition: otbFunctionWithNeighborhoodToImageFilter.h:83
otb::FunctionWithNeighborhoodToImageFilter::OutputImagePointer
OutputImageType::Pointer OutputImagePointer
Definition: otbFunctionWithNeighborhoodToImageFilter.h:77
otb::FunctionWithNeighborhoodToImageFilter::InputImageRegionType
InputImageType::RegionType InputImageRegionType
Definition: otbFunctionWithNeighborhoodToImageFilter.h:72
otb::FunctionWithNeighborhoodToImageFilter::BeforeThreadedGenerateData
void BeforeThreadedGenerateData() override
Definition: otbFunctionWithNeighborhoodToImageFilter.hxx:47
otb::FunctionWithNeighborhoodToImageFilter::InputImageConstPointer
InputImageType::ConstPointer InputImageConstPointer
Definition: otbFunctionWithNeighborhoodToImageFilter.h:70
otb::FunctionWithNeighborhoodToImageFilter::InputImagePointer
InputImageType::Pointer InputImagePointer
Definition: otbFunctionWithNeighborhoodToImageFilter.h:71