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