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