OTB  10.0.0
Orfeo Toolbox
otbUnaryFunctorNeighborhoodImageFilter.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2024 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 otbUnaryFunctorNeighborhoodImageFilter_hxx
22 #define otbUnaryFunctorNeighborhoodImageFilter_hxx
23 
25 #include "itkImageRegionIterator.h"
26 #include "itkNeighborhoodAlgorithm.h"
27 #include "itkProgressReporter.h"
28 #include "itkZeroFluxNeumannBoundaryCondition.h"
29 #include "itkNeighborhoodAlgorithm.h"
30 
31 namespace otb
32 {
36 template <class TInputImage, class TOutputImage, class TFunction>
38 {
39  this->SetNumberOfRequiredInputs(1);
40  m_Radius.Fill(1);
41  this->DynamicMultiThreadingOn();
42 }
43 template <class TInputImage, class TOutputImage, class TFunction>
45 {
46  // call the superclass' implementation of this method
47  Superclass::GenerateInputRequestedRegion();
49 
50  // get pointers to the input and output
51  typename Superclass::InputImagePointer inputPtr = const_cast<TInputImage*>(this->GetInput());
52  typename Superclass::OutputImagePointer outputPtr = this->GetOutput();
53 
54  if (!inputPtr || !outputPtr)
55  {
56  return;
57  }
58  // get a copy of the input requested region (should equal the output
59  // requested region)
60  typename TInputImage::RegionType inputRequestedRegion;
61  inputRequestedRegion = inputPtr->GetRequestedRegion();
62 
63  // pad the input requested region by the operator radius
64  inputRequestedRegion.PadByRadius(m_Radius);
65 
66  // crop the input requested region at the input's largest possible region
67  if (inputRequestedRegion.Crop(inputPtr->GetLargestPossibleRegion()))
68  {
69  inputPtr->SetRequestedRegion(inputRequestedRegion);
70  return;
71  }
72  else
73  {
74  // Couldn't crop the region (requested region is outside the largest
75  // possible region). Throw an exception.
76 
77  // store what we tried to request (prior to trying to crop)
78  inputPtr->SetRequestedRegion(inputRequestedRegion);
79 
80  // build an exception
81  itk::InvalidRequestedRegionError e(__FILE__, __LINE__);
82  std::ostringstream msg;
83  msg << this->GetNameOfClass() << "::GenerateInputRequestedRegion()";
84  e.SetLocation(msg.str());
85  e.SetDescription("Requested region is (at least partially) outside the largest possible region.");
86  e.SetDataObject(inputPtr);
87  throw e;
88  }
89 }
90 
94 template <class TInputImage, class TOutputImage, class TFunction>
96 {
97  itk::ZeroFluxNeumannBoundaryCondition<TInputImage> nbc;
98 
99  // We use dynamic_cast since inputs are stored as DataObjects. The
100  // ImageToImageFilter::GetInput(int) always returns a pointer to a
101  // TInputImage so it cannot be used for the second input.
102  InputImagePointer inputPtr = dynamic_cast<const TInputImage*>(ProcessObjectType::GetInput(0));
103  OutputImagePointer outputPtr = this->GetOutput(0);
104 
105  RadiusType r;
106 
107  r[0] = m_Radius[0];
108  r[1] = m_Radius[1];
109 
110  NeighborhoodIteratorType neighInputIt;
111 
112  itk::ImageRegionIterator<TOutputImage> outputIt;
113 
114  // Find the data-set boundary "faces"
115  typename itk::NeighborhoodAlgorithm::ImageBoundaryFacesCalculator<TInputImage>::FaceListType faceList;
116  typename itk::NeighborhoodAlgorithm::ImageBoundaryFacesCalculator<TInputImage> bC;
117  faceList = bC(inputPtr, outputRegionForThread, r);
118 
119  typename itk::NeighborhoodAlgorithm::ImageBoundaryFacesCalculator<TInputImage>::FaceListType::iterator fit;
120 
121  // Process each of the boundary faces. These are N-d regions which border
122  // the edge of the buffer.
123  for (fit = faceList.begin(); fit != faceList.end(); ++fit)
124  {
125  neighInputIt = itk::ConstNeighborhoodIterator<TInputImage>(r, inputPtr, *fit);
126 
127  outputIt = itk::ImageRegionIterator<TOutputImage>(outputPtr, *fit);
128  neighInputIt.OverrideBoundaryCondition(&nbc);
129  neighInputIt.GoToBegin();
130 
131  while (!outputIt.IsAtEnd())
132  {
133 
134  outputIt.Set(m_Functor(neighInputIt));
135 
136  ++neighInputIt;
137  ++outputIt;
138  }
139  }
140 }
141 
142 } // end namespace otb
143 
144 #endif
void DynamicThreadedGenerateData(const OutputImageRegionType &outputRegionForThread) override
itk::ConstNeighborhoodIterator< TInputImage > NeighborhoodIteratorType
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.