OTB  9.0.0
Orfeo Toolbox
otbLocalHistogramImageFunction.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 otbLocalHistogramImageFunction_hxx
22 #define otbLocalHistogramImageFunction_hxx
23 
25 #include "itkConstNeighborhoodIterator.h"
26 #include "itkExtractImageFilter.h"
27 #include "otbMath.h"
28 
29 namespace otb
30 {
31 
35 template <class TInputImage, class TCoordRep>
37  : m_NeighborhoodRadius(1), m_NumberOfHistogramBins(128), m_HistogramMin(0), m_HistogramMax(1), m_GaussianSmoothing(true)
38 {
39 }
40 
41 template <class TInputImage, class TCoordRep>
42 void LocalHistogramImageFunction<TInputImage, TCoordRep>::PrintSelf(std::ostream& os, itk::Indent indent) const
43 {
44  this->Superclass::PrintSelf(os, indent);
45  os << indent << " Neighborhood radius value : " << this->GetNeighborhoodRadius() << std::endl;
46  os << indent << " Number Of Histogram Bins : " << this->GetNumberOfHistogramBins() << std::endl;
47  os << indent << " Histogram Minimum : " << this->GetHistogramMin() << std::endl;
48  os << indent << " Histogram Maximum : " << this->GetHistogramMax() << std::endl;
49 }
50 
51 template <class TInputImage, class TCoordRep>
54 {
55  typename HistogramType::Pointer histogram = HistogramType::New();
56 
57  typename HistogramType::SizeType size(this->GetInputImage()->GetNumberOfComponentsPerPixel());
58  size.Fill(this->GetNumberOfHistogramBins());
59 
60  typename HistogramType::MeasurementVectorType lowerBound(this->GetInputImage()->GetNumberOfComponentsPerPixel());
61  typename HistogramType::MeasurementVectorType upperBound(this->GetInputImage()->GetNumberOfComponentsPerPixel());
62 
63  lowerBound.Fill(static_cast<typename HistogramType::MeasurementType>(this->GetHistogramMin()));
64  upperBound.Fill(static_cast<typename HistogramType::MeasurementType>(this->GetHistogramMax()));
65 
66  histogram->SetMeasurementVectorSize(this->GetInputImage()->GetNumberOfComponentsPerPixel());
67  histogram->Initialize(size, lowerBound, upperBound);
68  histogram->SetToZero();
69 
70  // Check for input image
71  if (!this->GetInputImage())
72  {
73  return histogram;
74  }
75 
76  // Check for out of buffer
77  if (!this->IsInsideBuffer(index))
78  {
79  return histogram;
80  }
81 
82  // Create an N-d neighborhood kernel, using a zeroflux boundary condition
83  typename InputImageType::SizeType kernelSize;
84  kernelSize.Fill(m_NeighborhoodRadius);
85 
86  itk::ConstNeighborhoodIterator<InputImageType> it(kernelSize, this->GetInputImage(), this->GetInputImage()->GetBufferedRegion());
87 
88  // Set the iterator at the desired location
89  it.SetLocation(index);
90 
91  // Define a gaussian kernel around the center location
92  double squaredRadius = m_NeighborhoodRadius * m_NeighborhoodRadius;
93  double squaredSigma = 0.25 * squaredRadius;
94 
95  // Offset to be used in the loops
96  typename InputImageType::OffsetType offset;
97 
98  // Fill the histogram
99  for (int i = -(int)m_NeighborhoodRadius; i < (int)m_NeighborhoodRadius; ++i)
100  {
101  for (int j = -(int)m_NeighborhoodRadius; j < (int)m_NeighborhoodRadius; ++j)
102  {
103  // Check if the current pixel lies within a disc of radius m_NeighborhoodRadius
104  double currentSquaredRadius = i * i + j * j;
105  if (currentSquaredRadius < squaredRadius)
106  {
107  // If so, compute the gaussian weighting (this could be
108  // computed once for all for the sake of optimisation) if necessary
109  double gWeight = 1.;
110  if (m_GaussianSmoothing)
111  {
112  gWeight = (1 / std::sqrt(otb::CONST_2PI * squaredSigma)) * std::exp(-currentSquaredRadius / (2 * squaredSigma));
113  }
114 
115  // Compute pixel location
116  offset[0] = i;
117  offset[1] = j;
118 
119  // Get the current value
120  typename HistogramType::MeasurementVectorType sample(this->GetInputImage()->GetNumberOfComponentsPerPixel());
121  sample[0] = it.GetPixel(offset);
122 
123  // Populate histogram
124  histogram->IncreaseFrequencyOfMeasurement(sample, gWeight);
125  }
126  }
127  }
128  return histogram;
129 }
130 
131 } // namespace otb
132 
133 #endif
otb::LocalHistogramImageFunction::OutputType
Superclass::OutputType OutputType
Definition: otbLocalHistogramImageFunction.h:77
otb::LocalHistogramImageFunction::IndexType
Superclass::IndexType IndexType
Definition: otbLocalHistogramImageFunction.h:73
otbMath.h
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otbLocalHistogramImageFunction.h
otb::CONST_2PI
constexpr double CONST_2PI
Definition: otbMath.h:55
otb::LocalHistogramImageFunction::EvaluateAtIndex
OutputType EvaluateAtIndex(const IndexType &index) const override
Definition: otbLocalHistogramImageFunction.hxx:53
otb::LocalHistogramImageFunction::PrintSelf
void PrintSelf(std::ostream &os, itk::Indent indent) const override
Definition: otbLocalHistogramImageFunction.hxx:42
otb::LocalHistogramImageFunction::LocalHistogramImageFunction
LocalHistogramImageFunction()
Definition: otbLocalHistogramImageFunction.hxx:36