OTB  10.0.0
Orfeo Toolbox
otbBinaryImageToDensityImageFilter.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 otbBinaryImageToDensityImageFilter_hxx
22 #define otbBinaryImageToDensityImageFilter_hxx
23 
25 #include "itkImageRegionIterator.h"
26 #include "itkProgressReporter.h"
27 #include "itkZeroFluxNeumannBoundaryCondition.h"
28 #include "itkNeighborhoodAlgorithm.h"
29 
30 #include "otbMacro.h"
31 
32 namespace otb
33 {
35 template <class TInputImage, class TOutputImage, class TCountFunction>
37 {
38  m_NeighborhoodRadius.Fill(1);
39  m_CountFunction = CountFunctionType::New();
40  this->DynamicMultiThreadingOn();
41 }
43 
45 template <class TInputImage, class TOutputImage, class TCountFunction>
47 {
48 }
49 
50 template <class TInputImage, class TOutputImage, class TCountFunction>
52 {
53  // call the superclass' implementation of this method
54  Superclass::GenerateInputRequestedRegion();
55 
56  // get pointers to the input and output
57  InputImagePointerType inputPtr = const_cast<TInputImage*>(this->GetInput());
58  OutputImagePointerType outputPtr = this->GetOutput();
59 
60  if (!inputPtr || !outputPtr)
61  {
62  return;
63  }
64  // get a copy of the input requested region (should equal the output
65  // requested region)
66  InputImageRegionType inputRequestedRegion = inputPtr->GetRequestedRegion();
67 
68  // pad the input requested region by the operator radius
69  inputRequestedRegion.PadByRadius(m_NeighborhoodRadius);
70 
71  // crop the input requested region at the input's largest possible region
72  if (inputRequestedRegion.Crop(inputPtr->GetLargestPossibleRegion()))
73  {
74  inputPtr->SetRequestedRegion(inputRequestedRegion);
75  return;
76  }
77  else
78  {
79  // Couldn't crop the region (requested region is outside the largest
80  // possible region). Throw an exception.
81 
82  // store what we tried to request (prior to trying to crop)
83  inputPtr->SetRequestedRegion(inputRequestedRegion);
84 
85  // build an exception
86  itk::InvalidRequestedRegionError e(__FILE__, __LINE__);
87  std::ostringstream msg;
88  msg << this->GetNameOfClass() << "::GenerateInputRequestedRegion()";
89  e.SetLocation(msg.str());
90  e.SetDescription("Requested region is (at least partially) outside the largest possible region.");
91  e.SetDataObject(inputPtr);
92  throw e;
93  }
94 }
95 
96 template <class TInputImage, class TOutputImage, class TCountFunction>
98 {
99  Superclass::BeforeThreadedGenerateData();
100 
101  m_CountFunction->SetInputImage(this->GetInput());
102  m_CountFunction->SetNeighborhoodRadius(m_NeighborhoodRadius);
103 }
104 
106 template <class TInputImage, class TOutputImage, class TCountFunction>
108 {
109  InputImagePointerType inputPtr = const_cast<InputImageType*>(this->GetInput());
110  OutputImagePointerType outputPtr = this->GetOutput();
112 
113  itk::ZeroFluxNeumannBoundaryCondition<TInputImage> nbc;
114  RadiusType r;
115  r[0] = m_NeighborhoodRadius[0];
116  r[1] = m_NeighborhoodRadius[1];
117 
118  NeighborhoodIteratorType it;
119  typename itk::NeighborhoodAlgorithm::ImageBoundaryFacesCalculator<TInputImage>::FaceListType faceList;
120  typename itk::NeighborhoodAlgorithm::ImageBoundaryFacesCalculator<TInputImage> bC;
121  faceList = bC(inputPtr, outputRegionForThread, r);
122  typename itk::NeighborhoodAlgorithm::ImageBoundaryFacesCalculator<TInputImage>::FaceListType::iterator fit;
123 
124  itk::ImageRegionIterator<OutputImageType> itOut(outputPtr, outputRegionForThread);
125 
126  typename InputImageType::IndexType index;
127 
128  for (fit = faceList.begin(); fit != faceList.end(); ++fit)
129  {
130  it = itk::ConstNeighborhoodIterator<TInputImage>(r, inputPtr, *fit);
131 
132  itOut = itk::ImageRegionIterator<TOutputImage>(outputPtr, *fit);
133  it.OverrideBoundaryCondition(&nbc);
134  it.GoToBegin();
135 
136  while (!itOut.IsAtEnd())
137  {
138  index = it.GetIndex();
139 
140  if (outputRegionForThread.IsInside(index))
141  {
142  itOut.Set(m_CountFunction->EvaluateAtIndex(index));
143  }
144 
145  ++itOut;
146  ++it;
147  }
148  }
149 }
150 
void DynamicThreadedGenerateData(const InputImageRegionType &outputRegionForThread) override
void GenerateInputRequestedRegion() override
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.