OTB  9.0.0
Orfeo Toolbox
otbBinaryImageToDensityImageFilter.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 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 }
42 
44 template <class TInputImage, class TOutputImage, class TCountFunction>
46 {
47 }
48 
49 template <class TInputImage, class TOutputImage, class TCountFunction>
51 {
52  // call the superclass' implementation of this method
53  Superclass::GenerateInputRequestedRegion();
54 
55  // get pointers to the input and output
56  InputImagePointerType inputPtr = const_cast<TInputImage*>(this->GetInput());
57  OutputImagePointerType outputPtr = this->GetOutput();
58 
59  if (!inputPtr || !outputPtr)
60  {
61  return;
62  }
63  // get a copy of the input requested region (should equal the output
64  // requested region)
65  InputImageRegionType inputRequestedRegion = inputPtr->GetRequestedRegion();
66 
67  // pad the input requested region by the operator radius
68  inputRequestedRegion.PadByRadius(m_NeighborhoodRadius);
69 
70  // crop the input requested region at the input's largest possible region
71  if (inputRequestedRegion.Crop(inputPtr->GetLargestPossibleRegion()))
72  {
73  inputPtr->SetRequestedRegion(inputRequestedRegion);
74  return;
75  }
76  else
77  {
78  // Couldn't crop the region (requested region is outside the largest
79  // possible region). Throw an exception.
80 
81  // store what we tried to request (prior to trying to crop)
82  inputPtr->SetRequestedRegion(inputRequestedRegion);
83 
84  // build an exception
85  itk::InvalidRequestedRegionError e(__FILE__, __LINE__);
86  std::ostringstream msg;
87  msg << this->GetNameOfClass() << "::GenerateInputRequestedRegion()";
88  e.SetLocation(msg.str());
89  e.SetDescription("Requested region is (at least partially) outside the largest possible region.");
90  e.SetDataObject(inputPtr);
91  throw e;
92  }
93 }
94 
95 template <class TInputImage, class TOutputImage, class TCountFunction>
97 {
98  Superclass::BeforeThreadedGenerateData();
99 
100  m_CountFunction->SetInputImage(this->GetInput());
101  m_CountFunction->SetNeighborhoodRadius(m_NeighborhoodRadius);
102 }
103 
105 template <class TInputImage, class TOutputImage, class TCountFunction>
107  itk::ThreadIdType threadId)
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  itk::ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels());
127 
128  typename InputImageType::IndexType index;
129 
130  for (fit = faceList.begin(); fit != faceList.end(); ++fit)
131  {
132  it = itk::ConstNeighborhoodIterator<TInputImage>(r, inputPtr, *fit);
133 
134  itOut = itk::ImageRegionIterator<TOutputImage>(outputPtr, *fit);
135  it.OverrideBoundaryCondition(&nbc);
136  it.GoToBegin();
137 
138  while (!itOut.IsAtEnd())
139  {
140  index = it.GetIndex();
141 
142  if (outputRegionForThread.IsInside(index))
143  {
144  itOut.Set(m_CountFunction->EvaluateAtIndex(index));
145  }
146 
147  ++itOut;
148  ++it;
149  progress.CompletedPixel(); // potential exception thrown here
150  }
151  }
152 }
153 
otb::BinaryImageToDensityImageFilter::BinaryImageToDensityImageFilter
BinaryImageToDensityImageFilter()
otb::BinaryImageToDensityImageFilter::GenerateInputRequestedRegion
void GenerateInputRequestedRegion() override
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otbMacro.h
otb::BinaryImageToDensityImageFilter::ThreadedGenerateData
void ThreadedGenerateData(const InputImageRegionType &outputRegionForThread, itk::ThreadIdType threadId) override
otb::BinaryImageToDensityImageFilter::BeforeThreadedGenerateData
void BeforeThreadedGenerateData() override
otbBinaryImageToDensityImageFilter.h
otb::BinaryImageToDensityImageFilter::~BinaryImageToDensityImageFilter
~BinaryImageToDensityImageFilter() override