Orfeo Toolbox  4.0
otbMRFEnergy.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: ORFEO Toolbox
4  Language: C++
5  Date: $Date$
6  Version: $Revision$
7 
8 
9  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
10  See OTBCopyright.txt for details.
11 
12 
13  This software is distributed WITHOUT ANY WARRANTY; without even
14  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  PURPOSE. See the above copyright notices for more information.
16 
17 =========================================================================*/
18 #ifndef __otbMRFEnergy_h
19 #define __otbMRFEnergy_h
20 
22 #include "itkObject.h"
23 #include "itkArray.h"
24 
25 namespace otb
26 {
37 template<class TInput1, class TInput2>
38 class ITK_EXPORT MRFEnergy : public itk::Object
39 {
40 public:
41  typedef MRFEnergy Self;
45 
46  typedef TInput1 InputImageType;
47  typedef TInput2 LabelledImageType;
48  typedef typename InputImageType::PixelType InputImagePixelType;
49  typedef typename LabelledImageType::PixelType LabelledImagePixelType;
50 
55 
57 
58  itkNewMacro(Self);
59 
60  itkTypeMacro(MRFEnergy, itk::Object);
61 
62  itkSetMacro(NumberOfParameters, unsigned int);
63  itkGetConstMacro(NumberOfParameters, unsigned int);
64 
65  // Get the parameters
66  const ParametersType& GetParameters(void) const
67  {
68  return this->m_Parameters;
69  }
70 
71  void SetParameters(const ParametersType& parameters)
72  {
73  if (parameters.Size() != m_NumberOfParameters)
74  {
75  itkExceptionMacro(<< "Invalid number of parameters");
76  }
77  m_Parameters = parameters;
78  this->Modified();
79  }
80 
81  virtual double GetSingleValue(const InputImagePixelType& value1, const LabelledImagePixelType& value2)
82  {
83  itkExceptionMacro(<< "GetSingleValue() has to be declared in child classes.");
84  }
85 
86  virtual double GetValue(const InputImagePixelType& value1, const LabelledImagePixelType& value2)
87  {
88  return GetSingleValue(value1, value2);
89  }
90 
91  virtual double GetValue(const LabelledNeighborhoodIterator& it, const LabelledImagePixelType& value2)
92  {
93  double result = 0.0;
94  unsigned int centerIndex = it.GetCenterNeighborhoodIndex();
95  InputImagePixelType value1; //TODO put also the other neighborhood ?
96  bool isInside = false;
97  unsigned int insideNeighbors = 0;
98  for (unsigned long pos = 0; pos < it.Size(); ++pos)
99  {
100  if (pos != centerIndex) //TODO put outside loop for faster access ?
101  {
102  value1 = it.GetPixel(pos, isInside);
103  if (isInside)
104  {
105  result += GetSingleValue(value1, value2);
106  ++insideNeighbors;
107  }
108  }
109  }
110  return result / insideNeighbors;
111  }
112 
113  virtual double GetValue(const InputNeighborhoodIterator& it, const LabelledImagePixelType& value2)
114  {
115  double result = 0.0;
116  unsigned int centerIndex = it.GetCenterNeighborhoodIndex();
117  InputImagePixelType value1; //TODO put also the other neighborhood ?
118  bool isInside = false;
119  unsigned int insideNeighbors = 0;
120  for (unsigned long pos = 0; pos < it.Size(); ++pos)
121  {
122  if (pos != centerIndex) //TODO put outside loop for faster access ?
123  {
124  value1 = it.GetPixel(pos, isInside);
125  if (isInside)
126  {
127  result += GetSingleValue(value1, value2);
128  ++insideNeighbors;
129  }
130  }
131  }
132  return result / insideNeighbors;
133  }
134 
135 protected:
136  // The constructor and destructor.
138  m_NumberOfParameters(1),
139  m_Parameters(0) {};
140  virtual ~MRFEnergy() {}
141  unsigned int m_NumberOfParameters;
143 };
144 
145 template<class TInput2>
146 class ITK_EXPORT MRFEnergy<TInput2, TInput2> : public itk::Object
147 {
148 public:
149  typedef MRFEnergy Self;
153 
154  typedef TInput2 LabelledImageType;
155  typedef typename LabelledImageType::PixelType LabelledImagePixelType;
156 
160 
161  itkNewMacro(Self);
162 
163  itkTypeMacro(MRFEnergy, itk::Object);
164 
165  itkSetMacro(NumberOfParameters, unsigned int);
166  itkGetConstMacro(NumberOfParameters, unsigned int);
167 
168  // Get the parameters
169  const ParametersType& GetParameters(void) const
170  {
171  return this->m_Parameters;
172  }
173 
174  void SetParameters(const ParametersType& parameters)
175  {
176  if (parameters.Size() != m_NumberOfParameters)
177  {
178  itkExceptionMacro(<< "Invalid number of parameters");
179  }
180  m_Parameters = parameters;
181  this->Modified();
182  }
183 
184  virtual double GetSingleValue(const LabelledImagePixelType& value1, const LabelledImagePixelType& value2)
185  {
186  itkExceptionMacro(<< "GetSingleValue() has to be declared in child classes.");
187  }
188 
189  virtual double GetValue(const LabelledImagePixelType& value1, const LabelledImagePixelType& value2)
190  {
191  return GetSingleValue(value1, value2);
192  }
193 
194  virtual double GetValue(const LabelledNeighborhoodIterator& it, const LabelledImagePixelType& value2)
195  {
196  double result = 0.0;
197  unsigned int centerIndex = it.GetCenterNeighborhoodIndex();
198  LabelledImagePixelType value1; //TODO put also the other neighborhood ?
199  bool isInside = false;
200  unsigned int insideNeighbors = 0;
201  for (unsigned long pos = 0; pos < it.Size(); ++pos)
202  {
203  if (pos != centerIndex) //TODO put outside loop for faster access ?
204  {
205  value1 = it.GetPixel(pos, isInside);
206  if (isInside)
207  {
208  result += GetSingleValue(value1, value2);
209  ++insideNeighbors;
210  }
211  }
212  }
213  return result / insideNeighbors;
214  }
215 
216 protected:
217  // The constructor and destructor.
219  m_NumberOfParameters(1),
220  m_Parameters(0)
221  {};
222  virtual ~MRFEnergy() {}
223  unsigned int m_NumberOfParameters;
225 };
226 }
227 
228 #endif

Generated at Sat Mar 8 2014 16:09:25 for Orfeo Toolbox with doxygen 1.8.3.1