OTB  9.0.0
Orfeo Toolbox
otbMRFEnergy.h
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 otbMRFEnergy_h
22 #define otbMRFEnergy_h
23 
24 #include "itkConstNeighborhoodIterator.h"
25 #include "itkObject.h"
26 #include "itkArray.h"
27 
28 namespace otb
29 {
42 template <class TInput1, class TInput2>
43 class ITK_EXPORT MRFEnergy : public itk::Object
44 {
45 public:
46  typedef MRFEnergy Self;
47  typedef itk::Object Superclass;
48  typedef itk::SmartPointer<Self> Pointer;
49  typedef itk::SmartPointer<const Self> ConstPointer;
50 
51  typedef TInput1 InputImageType;
52  typedef TInput2 LabelledImageType;
53  typedef typename InputImageType::PixelType InputImagePixelType;
54  typedef typename LabelledImageType::PixelType LabelledImagePixelType;
55 
56  typedef itk::ConstNeighborhoodIterator<LabelledImageType> LabelledNeighborhoodIterator;
57  typedef itk::ConstNeighborhoodIterator<InputImageType> InputNeighborhoodIterator;
58 
59  typedef itk::Array<double> ParametersType;
60 
61  itkNewMacro(Self);
62 
63  itkTypeMacro(MRFEnergy, itk::Object);
64 
65  itkSetMacro(NumberOfParameters, unsigned int);
66  itkGetConstMacro(NumberOfParameters, unsigned int);
67 
68  // Get the parameters
69  const ParametersType& GetParameters(void) const
70  {
71  return this->m_Parameters;
72  }
73 
74  void SetParameters(const ParametersType& parameters)
75  {
76  if (parameters.Size() != m_NumberOfParameters)
77  {
78  itkExceptionMacro(<< "Invalid number of parameters");
79  }
80  m_Parameters = parameters;
81  this->Modified();
82  }
83 
84  virtual double GetSingleValue(const InputImagePixelType& itkNotUsed(value1), const LabelledImagePixelType& itkNotUsed(value2))
85  {
86  itkExceptionMacro(<< "GetSingleValue() has to be declared in child classes.");
87  }
88 
89  virtual double GetValue(const InputImagePixelType& value1, const LabelledImagePixelType& value2)
90  {
91  return GetSingleValue(value1, value2);
92  }
93 
94  virtual double GetValue(const LabelledNeighborhoodIterator& it, const LabelledImagePixelType& value2)
95  {
96  double result = 0.0;
97  unsigned int centerIndex = it.GetCenterNeighborhoodIndex();
98  InputImagePixelType value1; // TODO put also the other neighborhood ?
99  bool isInside = false;
100  unsigned int insideNeighbors = 0;
101  for (unsigned long pos = 0; pos < it.Size(); ++pos)
102  {
103  if (pos != centerIndex) // TODO put outside loop for faster access ?
104  {
105  value1 = it.GetPixel(pos, isInside);
106  if (isInside)
107  {
108  result += GetSingleValue(value1, value2);
109  ++insideNeighbors;
110  }
111  }
112  }
113  return result / insideNeighbors;
114  }
115 
116  virtual double GetValue(const InputNeighborhoodIterator& it, const LabelledImagePixelType& value2)
117  {
118  double result = 0.0;
119  unsigned int centerIndex = it.GetCenterNeighborhoodIndex();
120  InputImagePixelType value1; // TODO put also the other neighborhood ?
121  bool isInside = false;
122  unsigned int insideNeighbors = 0;
123  for (unsigned long pos = 0; pos < it.Size(); ++pos)
124  {
125  if (pos != centerIndex) // TODO put outside loop for faster access ?
126  {
127  value1 = it.GetPixel(pos, isInside);
128  if (isInside)
129  {
130  result += GetSingleValue(value1, value2);
131  ++insideNeighbors;
132  }
133  }
134  }
135  return result / insideNeighbors;
136  }
137 
138 protected:
139  // The constructor and destructor.
140  MRFEnergy() : m_NumberOfParameters(1), m_Parameters(0){};
141  ~MRFEnergy() override
142  {
143  }
144  unsigned int m_NumberOfParameters;
146 };
147 
148 template <class TInput2>
149 class ITK_EXPORT MRFEnergy<TInput2, TInput2> : public itk::Object
150 {
151 public:
152  typedef MRFEnergy Self;
153  typedef itk::Object Superclass;
154  typedef itk::SmartPointer<Self> Pointer;
155  typedef itk::SmartPointer<const Self> ConstPointer;
156 
157  typedef TInput2 LabelledImageType;
158  typedef typename LabelledImageType::PixelType LabelledImagePixelType;
159 
160  typedef itk::ConstNeighborhoodIterator<LabelledImageType> LabelledNeighborhoodIterator;
161  typedef itk::Array<double> ParametersType;
162 
163  itkNewMacro(Self);
164 
165  itkTypeMacro(MRFEnergy, itk::Object);
166 
167  itkSetMacro(NumberOfParameters, unsigned int);
168  itkGetConstMacro(NumberOfParameters, unsigned int);
169 
170  // Get the parameters
171  const ParametersType& GetParameters(void) const
172  {
173  return this->m_Parameters;
174  }
175 
176  void SetParameters(const ParametersType& parameters)
177  {
178  if (parameters.Size() != m_NumberOfParameters)
179  {
180  itkExceptionMacro(<< "Invalid number of parameters");
181  }
182  m_Parameters = parameters;
183  this->Modified();
184  }
185 
186  virtual double GetSingleValue(const LabelledImagePixelType& itkNotUsed(value1), const LabelledImagePixelType& itkNotUsed(value2))
187  {
188  itkExceptionMacro(<< "GetSingleValue() has to be declared in child classes.");
189  }
190 
191  virtual double GetValue(const LabelledImagePixelType& value1, const LabelledImagePixelType& value2)
192  {
193  return GetSingleValue(value1, value2);
194  }
195 
196  virtual double GetValue(const LabelledNeighborhoodIterator& it, const LabelledImagePixelType& value2)
197  {
198  double result = 0.0;
199  unsigned int centerIndex = it.GetCenterNeighborhoodIndex();
200  LabelledImagePixelType value1; // TODO put also the other neighborhood ?
201  bool isInside = false;
202  unsigned int insideNeighbors = 0;
203  for (unsigned long pos = 0; pos < it.Size(); ++pos)
204  {
205  if (pos != centerIndex) // TODO put outside loop for faster access ?
206  {
207  value1 = it.GetPixel(pos, isInside);
208  if (isInside)
209  {
210  result += GetSingleValue(value1, value2);
211  ++insideNeighbors;
212  }
213  }
214  }
215  return result / insideNeighbors;
216  }
217 
218 protected:
219  // The constructor and destructor.
220  MRFEnergy() : m_NumberOfParameters(1), m_Parameters(0){};
221  ~MRFEnergy() override
222  {
223  }
224  unsigned int m_NumberOfParameters;
226 };
227 }
228 
229 #endif
otb::MRFEnergy::Self
MRFEnergy Self
Definition: otbMRFEnergy.h:46
otb::MRFEnergy::MRFEnergy
MRFEnergy()
Definition: otbMRFEnergy.h:140
otb::MRFEnergy::ConstPointer
itk::SmartPointer< const Self > ConstPointer
Definition: otbMRFEnergy.h:49
otb::MRFEnergy::Pointer
itk::SmartPointer< Self > Pointer
Definition: otbMRFEnergy.h:48
otb::MRFEnergy< TInput2, TInput2 >::ParametersType
itk::Array< double > ParametersType
Definition: otbMRFEnergy.h:161
otb::MRFEnergy< TInput2, TInput2 >::Self
MRFEnergy Self
Definition: otbMRFEnergy.h:152
otb::MRFEnergy::ParametersType
itk::Array< double > ParametersType
Definition: otbMRFEnergy.h:59
otb::MRFEnergy< TInput2, TInput2 >::GetParameters
const ParametersType & GetParameters(void) const
Definition: otbMRFEnergy.h:171
otb::MRFEnergy< TInput2, TInput2 >::LabelledImagePixelType
LabelledImageType::PixelType LabelledImagePixelType
Definition: otbMRFEnergy.h:158
otb::MRFEnergy::LabelledImageType
TInput2 LabelledImageType
Definition: otbMRFEnergy.h:52
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::MRFEnergy< TInput2, TInput2 >::m_NumberOfParameters
unsigned int m_NumberOfParameters
Definition: otbMRFEnergy.h:224
otb::MRFEnergy::GetValue
virtual double GetValue(const InputNeighborhoodIterator &it, const LabelledImagePixelType &value2)
Definition: otbMRFEnergy.h:116
otb::MRFEnergy::~MRFEnergy
~MRFEnergy() override
Definition: otbMRFEnergy.h:141
otb::MRFEnergy::InputImageType
TInput1 InputImageType
Definition: otbMRFEnergy.h:51
otb::MRFEnergy< TInput2, TInput2 >::Superclass
itk::Object Superclass
Definition: otbMRFEnergy.h:153
otb::MRFEnergy::GetParameters
const ParametersType & GetParameters(void) const
Definition: otbMRFEnergy.h:69
otb::MRFEnergy::SetParameters
void SetParameters(const ParametersType &parameters)
Definition: otbMRFEnergy.h:74
otb::MRFEnergy< TInput2, TInput2 >::MRFEnergy
MRFEnergy()
Definition: otbMRFEnergy.h:220
otb::MRFEnergy< TInput2, TInput2 >::GetValue
virtual double GetValue(const LabelledImagePixelType &value1, const LabelledImagePixelType &value2)
Definition: otbMRFEnergy.h:191
otb::MRFEnergy::m_Parameters
ParametersType m_Parameters
Definition: otbMRFEnergy.h:145
otb::MRFEnergy< TInput2, TInput2 >::ConstPointer
itk::SmartPointer< const Self > ConstPointer
Definition: otbMRFEnergy.h:155
otb::MRFEnergy::InputNeighborhoodIterator
itk::ConstNeighborhoodIterator< InputImageType > InputNeighborhoodIterator
Definition: otbMRFEnergy.h:57
otb::MRFEnergy< TInput2, TInput2 >::GetValue
virtual double GetValue(const LabelledNeighborhoodIterator &it, const LabelledImagePixelType &value2)
Definition: otbMRFEnergy.h:196
otb::MRFEnergy::LabelledImagePixelType
LabelledImageType::PixelType LabelledImagePixelType
Definition: otbMRFEnergy.h:54
otb::MRFEnergy< TInput2, TInput2 >::SetParameters
void SetParameters(const ParametersType &parameters)
Definition: otbMRFEnergy.h:176
otb::MRFEnergy::InputImagePixelType
InputImageType::PixelType InputImagePixelType
Definition: otbMRFEnergy.h:53
otb::MRFEnergy< TInput2, TInput2 >::~MRFEnergy
~MRFEnergy() override
Definition: otbMRFEnergy.h:221
otb::MRFEnergy< TInput2, TInput2 >::LabelledNeighborhoodIterator
itk::ConstNeighborhoodIterator< LabelledImageType > LabelledNeighborhoodIterator
Definition: otbMRFEnergy.h:160
otb::MRFEnergy< TInput2, TInput2 >::GetSingleValue
virtual double GetSingleValue(const LabelledImagePixelType &, const LabelledImagePixelType &)
Definition: otbMRFEnergy.h:186
otb::MRFEnergy::LabelledNeighborhoodIterator
itk::ConstNeighborhoodIterator< LabelledImageType > LabelledNeighborhoodIterator
Definition: otbMRFEnergy.h:56
otb::MRFEnergy::GetValue
virtual double GetValue(const InputImagePixelType &value1, const LabelledImagePixelType &value2)
Definition: otbMRFEnergy.h:89
otb::MRFEnergy::m_NumberOfParameters
unsigned int m_NumberOfParameters
Definition: otbMRFEnergy.h:144
otb::MRFEnergy< TInput2, TInput2 >::Pointer
itk::SmartPointer< Self > Pointer
Definition: otbMRFEnergy.h:154
otb::MRFEnergy< TInput2, TInput2 >::m_Parameters
ParametersType m_Parameters
Definition: otbMRFEnergy.h:225
otb::MRFEnergy::GetSingleValue
virtual double GetSingleValue(const InputImagePixelType &, const LabelledImagePixelType &)
Definition: otbMRFEnergy.h:84
otb::MRFEnergy::GetValue
virtual double GetValue(const LabelledNeighborhoodIterator &it, const LabelledImagePixelType &value2)
Definition: otbMRFEnergy.h:94
otb::MRFEnergy
This is the base class for energy function used in the MRF framework.
Definition: otbMRFEnergy.h:43
otb::MRFEnergy< TInput2, TInput2 >::LabelledImageType
TInput2 LabelledImageType
Definition: otbMRFEnergy.h:157
otb::MRFEnergy::Superclass
itk::Object Superclass
Definition: otbMRFEnergy.h:47