OTB  9.0.0
Orfeo Toolbox
otbRealMomentsImageFunction.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 otbRealMomentsImageFunction_hxx
22 #define otbRealMomentsImageFunction_hxx
23 
25 #include "itkConstNeighborhoodIterator.h"
26 #include "itkNumericTraits.h"
27 
28 namespace otb
29 {
30 
34 template <class TInputImage, class TCoordRep>
36 {
37  m_NeighborhoodRadius = 1;
38  m_Pmax = 4;
39  m_Qmax = 4;
40 }
41 
42 template <class TInputImage, class TCoordRep>
43 void RealMomentsImageFunction<TInputImage, TCoordRep>::PrintSelf(std::ostream& os, itk::Indent indent) const
44 {
45  this->Superclass::PrintSelf(os, indent);
46  os << indent << " p indice maximum value : " << m_Pmax << std::endl;
47  os << indent << " q indice maximum value : " << m_Qmax << std::endl;
48  os << indent << " Neighborhood radius value : " << m_NeighborhoodRadius << std::endl;
49 }
50 
51 template <class TInputImage, class TCoordRep>
54 {
55  // Build moments vector
56  OutputType moments;
57  moments.resize(m_Pmax + 1);
58 
59  std::vector<ScalarRealType> valXpY, valXqY;
60  valXpY.resize(m_Pmax + 1);
61  valXqY.resize(m_Qmax + 1);
62 
63  // Initialize moments
64  for (unsigned int p = 0; p <= m_Pmax; p++)
65  {
66  moments.at(p).resize(m_Qmax + 1);
67  valXpY.at(p) = 1.0;
68  for (unsigned int q = 0; q <= m_Qmax; q++)
69  {
70  moments.at(p).at(q) = 0.0;
71  valXqY.at(q) = 1.0;
72  }
73  }
74 
75  // Check for input image
76  if (!this->GetInputImage())
77  {
78  return moments;
79  }
80 
81  // Check for out of buffer
82  if (!this->IsInsideBuffer(index))
83  {
84  return moments;
85  }
86 
87  // Create an N-d neighborhood kernel, using a zeroflux boundary condition
88  typename InputImageType::SizeType kernelSize;
89  kernelSize.Fill(m_NeighborhoodRadius);
90 
91  itk::ConstNeighborhoodIterator<InputImageType> it(kernelSize, this->GetInputImage(), this->GetInputImage()->GetBufferedRegion());
92 
93  // Set the iterator at the desired location
94  it.SetLocation(index);
95 
96  // Walk the neighborhood
97  const unsigned int size = it.Size();
98  for (unsigned int i = 0; i < size; ++i)
99  {
100  // Retrieve value, and centered-reduced position
101  ScalarRealType value = static_cast<ScalarRealType>(it.GetPixel(i));
102  ScalarRealType x = static_cast<ScalarRealType>(it.GetOffset(i)[0]) / (2 * m_NeighborhoodRadius + 1);
103  ScalarRealType y = static_cast<ScalarRealType>(it.GetOffset(i)[1]) / (2 * m_NeighborhoodRadius + 1);
104 
105  unsigned int pTmp = 1;
106  unsigned int qTmp = 1;
107 
108  while (pTmp <= m_Pmax)
109  {
110  valXpY.at(pTmp) = valXpY.at(pTmp - 1) * x;
111  pTmp++;
112  }
113  while (qTmp <= m_Qmax)
114  {
115  valXqY.at(qTmp) = valXqY.at(qTmp - 1) * y;
116  qTmp++;
117  }
118 
119 
120  // Update cumulants
121  for (unsigned int p = 0; p <= m_Pmax; p++)
122  {
123  for (unsigned int q = 0; q <= m_Qmax; q++)
124  {
125  moments.at(p).at(q) += valXpY.at(p) * valXqY.at(q) * value;
126  }
127  }
128  }
129 
130  // Normalisation
131  for (int p = m_Pmax; p >= 0; p--)
132  {
133  for (int q = m_Qmax; q >= 0; q--)
134  {
135  moments.at(p).at(q) /= moments.at(0).at(0);
136  }
137  }
138 
139  // Return result
140  return moments;
141 }
142 
143 } // namespace otb
144 
145 #endif
otbRealMomentsImageFunction.h
otb::RealMomentsImageFunction::OutputType
Superclass::OutputType OutputType
Definition: otbRealMomentsImageFunction.h:65
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::RealMomentsImageFunction::EvaluateAtIndex
OutputType EvaluateAtIndex(const IndexType &index) const override
Definition: otbRealMomentsImageFunction.hxx:53
otb::RealMomentsImageFunction::PrintSelf
void PrintSelf(std::ostream &os, itk::Indent indent) const override
Definition: otbRealMomentsImageFunction.hxx:43
otb::RealMomentsImageFunction::IndexType
Superclass::IndexType IndexType
Definition: otbRealMomentsImageFunction.h:61
otb::RealMomentsImageFunction::ScalarRealType
float ScalarRealType
Definition: otbRealMomentsImageFunction.h:66
otb::RealMomentsImageFunction::RealMomentsImageFunction
RealMomentsImageFunction()
Definition: otbRealMomentsImageFunction.hxx:35