OTB  9.0.0
Orfeo Toolbox
otbBinaryImageDensityFunction.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 
22 #ifndef otbBinaryImageDensityFunction_hxx
23 #define otbBinaryImageDensityFunction_hxx
24 
26 
27 #include "itkConstNeighborhoodIterator.h"
28 
29 namespace otb
30 {
31 
35 template <class TInputImage, class TCoordRep>
37 {
38  m_NeighborhoodRadius.Fill(1);
39 }
40 
44 template <class TInputImage, class TCoordRep>
45 void BinaryImageDensityFunction<TInputImage, TCoordRep>::PrintSelf(std::ostream& os, itk::Indent indent) const
46 {
47  this->Superclass::PrintSelf(os, indent);
48  os << indent << "NeighborhoodRadius: " << m_NeighborhoodRadius << std::endl;
49 }
51 
55 template <class TInputImage, class TCoordRep>
58 {
59  RealType sum;
60  RealType var;
61 
62  sum = itk::NumericTraits<RealType>::Zero;
63 
64  if (!this->GetInputImage())
65  {
66  return (itk::NumericTraits<RealType>::max());
67  }
68 
69  if (!this->IsInsideBuffer(index))
70  {
71  return (itk::NumericTraits<RealType>::max());
72  }
73 
74  // Create an N-d neighborhood kernel, using a zeroflux boundary condition
75  typename InputImageType::SizeType kernelSize = m_NeighborhoodRadius;
76 
77  itk::ConstNeighborhoodIterator<InputImageType> it(kernelSize, this->GetInputImage(), this->GetInputImage()->GetBufferedRegion());
78 
79  // Set the iterator at the desired location
80  it.SetLocation(index);
81 
82  // Walk the neighborhood
83  const unsigned int size = it.Size();
84  for (unsigned int i = 0; i < size; ++i)
85  {
86  const RealType value = static_cast<RealType>(it.GetPixel(i));
87  sum += value;
88  }
89 
90  const RealType num = static_cast<RealType>(size);
91  var = sum / num;
92 
93  return var;
94 }
95 
96 } // end namespace otb
97 
98 #endif
otb::BinaryImageDensityFunction::PrintSelf
void PrintSelf(std::ostream &os, itk::Indent indent) const override
Definition: otbBinaryImageDensityFunction.hxx:45
otb::var
Definition: otbParserXPlugins.h:282
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::BinaryImageDensityFunction::EvaluateAtIndex
RealType EvaluateAtIndex(const IndexType &index) const override
Definition: otbBinaryImageDensityFunction.hxx:57
otb::BinaryImageDensityFunction::BinaryImageDensityFunction
BinaryImageDensityFunction()
Definition: otbBinaryImageDensityFunction.hxx:36
otbBinaryImageDensityFunction.h
otb::BinaryImageDensityFunction::RealType
itk::NumericTraits< typename InputImageType::PixelType >::RealType RealType
Definition: otbBinaryImageDensityFunction.h:76
otb::BinaryImageDensityFunction::IndexType
Superclass::IndexType IndexType
Definition: otbBinaryImageDensityFunction.h:69