OTB  9.0.0
Orfeo Toolbox
otbUnaryFunctorNeighborhoodImageFilter.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 
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 }
42 template <class TInputImage, class TOutputImage, class TFunction>
44 {
45  // call the superclass' implementation of this method
46  Superclass::GenerateInputRequestedRegion();
48 
49  // get pointers to the input and output
50  typename Superclass::InputImagePointer inputPtr = const_cast<TInputImage*>(this->GetInput());
51  typename Superclass::OutputImagePointer outputPtr = this->GetOutput();
52 
53  if (!inputPtr || !outputPtr)
54  {
55  return;
56  }
57  // get a copy of the input requested region (should equal the output
58  // requested region)
59  typename TInputImage::RegionType inputRequestedRegion;
60  inputRequestedRegion = inputPtr->GetRequestedRegion();
61 
62  // pad the input requested region by the operator radius
63  inputRequestedRegion.PadByRadius(m_Radius);
64 
65  // crop the input requested region at the input's largest possible region
66  if (inputRequestedRegion.Crop(inputPtr->GetLargestPossibleRegion()))
67  {
68  inputPtr->SetRequestedRegion(inputRequestedRegion);
69  return;
70  }
71  else
72  {
73  // Couldn't crop the region (requested region is outside the largest
74  // possible region). Throw an exception.
75 
76  // store what we tried to request (prior to trying to crop)
77  inputPtr->SetRequestedRegion(inputRequestedRegion);
78 
79  // build an exception
80  itk::InvalidRequestedRegionError e(__FILE__, __LINE__);
81  std::ostringstream msg;
82  msg << this->GetNameOfClass() << "::GenerateInputRequestedRegion()";
83  e.SetLocation(msg.str());
84  e.SetDescription("Requested region is (at least partially) outside the largest possible region.");
85  e.SetDataObject(inputPtr);
86  throw e;
87  }
88 }
89 
93 template <class TInputImage, class TOutputImage, class TFunction>
95  itk::ThreadIdType threadId)
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  // support progress methods/callbacks
122  itk::ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels());
123 
124  // Process each of the boundary faces. These are N-d regions which border
125  // the edge of the buffer.
126  for (fit = faceList.begin(); fit != faceList.end(); ++fit)
127  {
128  neighInputIt = itk::ConstNeighborhoodIterator<TInputImage>(r, inputPtr, *fit);
129 
130  outputIt = itk::ImageRegionIterator<TOutputImage>(outputPtr, *fit);
131  neighInputIt.OverrideBoundaryCondition(&nbc);
132  neighInputIt.GoToBegin();
133 
134  while (!outputIt.IsAtEnd())
135  {
136 
137  outputIt.Set(m_Functor(neighInputIt));
138 
139  ++neighInputIt;
140  ++outputIt;
141  progress.CompletedPixel();
142  }
143  }
144 }
145 
146 } // end namespace otb
147 
148 #endif
otb::UnaryFunctorNeighborhoodImageFilter::GenerateInputRequestedRegion
void GenerateInputRequestedRegion(void) override
Definition: otbUnaryFunctorNeighborhoodImageFilter.hxx:43
otb::UnaryFunctorNeighborhoodImageFilter< TInputImage1, TOutputImage, Functor::OutcoreFunctor< itk::ConstNeighborhoodIterator< TInputImage1 >, TOutputImage::PixelType > >::InputImagePointer
InputImageType::ConstPointer InputImagePointer
Definition: otbUnaryFunctorNeighborhoodImageFilter.h:61
otbUnaryFunctorNeighborhoodImageFilter.h
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::UnaryFunctorNeighborhoodImageFilter::ThreadedGenerateData
void ThreadedGenerateData(const OutputImageRegionType &outputRegionForThread, itk::ThreadIdType threadId) override
Definition: otbUnaryFunctorNeighborhoodImageFilter.hxx:94
otb::UnaryFunctorNeighborhoodImageFilter< TInputImage1, TOutputImage, Functor::OutcoreFunctor< itk::ConstNeighborhoodIterator< TInputImage1 >, TOutputImage::PixelType > >::RadiusType
NeighborhoodIteratorType::RadiusType RadiusType
Definition: otbUnaryFunctorNeighborhoodImageFilter.h:117
otb::UnaryFunctorNeighborhoodImageFilter< TInputImage1, TOutputImage, Functor::OutcoreFunctor< itk::ConstNeighborhoodIterator< TInputImage1 >, TOutputImage::PixelType > >::OutputImagePointer
OutputImageType::Pointer OutputImagePointer
Definition: otbUnaryFunctorNeighborhoodImageFilter.h:66
otb::UnaryFunctorNeighborhoodImageFilter< TInputImage1, TOutputImage, Functor::OutcoreFunctor< itk::ConstNeighborhoodIterator< TInputImage1 >, TOutputImage::PixelType > >::OutputImageRegionType
OutputImageType::RegionType OutputImageRegionType
Definition: otbUnaryFunctorNeighborhoodImageFilter.h:67
otb::UnaryFunctorNeighborhoodImageFilter< TInputImage1, TOutputImage, Functor::OutcoreFunctor< itk::ConstNeighborhoodIterator< TInputImage1 >, TOutputImage::PixelType > >::NeighborhoodIteratorType
itk::ConstNeighborhoodIterator< TInputImage1 > NeighborhoodIteratorType
Definition: otbUnaryFunctorNeighborhoodImageFilter.h:116
otb::UnaryFunctorNeighborhoodImageFilter::UnaryFunctorNeighborhoodImageFilter
UnaryFunctorNeighborhoodImageFilter()
Definition: otbUnaryFunctorNeighborhoodImageFilter.hxx:37