OTB  9.0.0
Orfeo Toolbox
otbMRFSamplerMAP.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 
22 #ifndef otbMRFSamplerMAP_h
23 #define otbMRFSamplerMAP_h
24 
25 #include "otbMRFSampler.h"
26 
27 namespace otb
28 {
44 template <class TInput1, class TInput2>
45 class ITK_EXPORT MRFSamplerMAP : public MRFSampler<TInput1, TInput2>
46 {
47 public:
50  typedef itk::SmartPointer<Self> Pointer;
51  typedef itk::SmartPointer<const Self> ConstPointer;
52 
53  typedef typename Superclass::InputImageNeighborhoodIterator InputImageNeighborhoodIterator;
54  typedef typename Superclass::LabelledImageNeighborhoodIterator LabelledImageNeighborhoodIterator;
55  typedef typename Superclass::LabelledImagePixelType LabelledImagePixelType;
56  typedef typename Superclass::InputImagePixelType InputImagePixelType;
57  typedef typename Superclass::EnergyFidelityType EnergyFidelityType;
58  typedef typename Superclass::EnergyRegularizationType EnergyRegularizationType;
59  typedef typename Superclass::EnergyFidelityPointer EnergyFidelityPointer;
60  typedef typename Superclass::EnergyRegularizationPointer EnergyRegularizationPointer;
61 
62  itkNewMacro(Self);
63 
64  itkTypeMacro(MRFSamplerMAP, MRFSampler);
65 
66  inline int Compute(const InputImageNeighborhoodIterator& itData, const LabelledImageNeighborhoodIterator& itRegul) override
67  {
68  if (this->m_NumberOfClasses == 0)
69  {
70  itkExceptionMacro(<< "NumberOfClasses has to be greater than 0.");
71  }
72 
73  this->m_EnergyBefore = this->m_EnergyFidelity->GetValue(itData, itRegul.GetCenterPixel());
74  this->m_EnergyBefore += this->m_Lambda * this->m_EnergyRegularization->GetValue(itRegul, itRegul.GetCenterPixel());
75 
76  // Try all possible value (how to be generic ?)
77  this->m_EnergyAfter = this->m_EnergyBefore; // default values to current one
78  this->m_Value = itRegul.GetCenterPixel();
79 
80  LabelledImagePixelType valueCurrent = 0;
81  while (valueCurrent < static_cast<LabelledImagePixelType>(this->GetNumberOfClasses()) && valueCurrent != itk::NumericTraits<LabelledImagePixelType>::max())
82  {
83  this->m_EnergyCurrent = this->m_EnergyFidelity->GetValue(itData, valueCurrent);
84  this->m_EnergyCurrent += this->m_Lambda * this->m_EnergyRegularization->GetValue(itRegul, valueCurrent);
85  if (this->m_EnergyCurrent < this->m_EnergyAfter)
86  {
87  this->m_EnergyAfter = this->m_EnergyCurrent;
88  this->m_Value = valueCurrent;
89  }
90  valueCurrent++;
91  }
92 
93  this->m_DeltaEnergy = this->m_EnergyAfter - this->m_EnergyBefore;
94 
95  return 0;
96  }
97 
98 protected:
99  // The constructor and destructor.
101  ~MRFSamplerMAP() override
102  {
103  }
104 };
105 }
106 
107 #endif
otb::MRFSamplerMAP::Superclass
MRFSampler< TInput1, TInput2 > Superclass
Definition: otbMRFSamplerMAP.h:49
otb::MRFSamplerMAP::LabelledImagePixelType
Superclass::LabelledImagePixelType LabelledImagePixelType
Definition: otbMRFSamplerMAP.h:55
otbMRFSampler.h
otb::MRFSamplerMAP::Pointer
itk::SmartPointer< Self > Pointer
Definition: otbMRFSamplerMAP.h:50
otb::MRFSamplerMAP::LabelledImageNeighborhoodIterator
Superclass::LabelledImageNeighborhoodIterator LabelledImageNeighborhoodIterator
Definition: otbMRFSamplerMAP.h:54
otb::MRFSamplerMAP::MRFSamplerMAP
MRFSamplerMAP()
Definition: otbMRFSamplerMAP.h:100
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::MRFSamplerMAP::EnergyRegularizationPointer
Superclass::EnergyRegularizationPointer EnergyRegularizationPointer
Definition: otbMRFSamplerMAP.h:60
otb::MRFSampler::InputImageNeighborhoodIterator
itk::ConstNeighborhoodIterator< TInput1 > InputImageNeighborhoodIterator
Definition: otbMRFSampler.h:52
otb::MRFSamplerMAP::EnergyFidelityType
Superclass::EnergyFidelityType EnergyFidelityType
Definition: otbMRFSamplerMAP.h:57
otb::MRFSamplerMAP::Compute
int Compute(const InputImageNeighborhoodIterator &itData, const LabelledImageNeighborhoodIterator &itRegul) override
Definition: otbMRFSamplerMAP.h:66
otb::MRFSamplerMAP::EnergyFidelityPointer
Superclass::EnergyFidelityPointer EnergyFidelityPointer
Definition: otbMRFSamplerMAP.h:59
otb::MRFSamplerMAP
This is the base class for sampler methods used in the MRF framework.
Definition: otbMRFSamplerMAP.h:45
otb::MRFSamplerMAP::InputImagePixelType
Superclass::InputImagePixelType InputImagePixelType
Definition: otbMRFSamplerMAP.h:56
otb::MRFSampler
This is the base class for sampler methods used in the MRF framework.
Definition: otbMRFSampler.h:44
otb::MRFSamplerMAP::ConstPointer
itk::SmartPointer< const Self > ConstPointer
Definition: otbMRFSamplerMAP.h:51
otb::MRFSampler::LabelledImagePixelType
TInput2::PixelType LabelledImagePixelType
Definition: otbMRFSampler.h:56
otb::MRFSamplerMAP::~MRFSamplerMAP
~MRFSamplerMAP() override
Definition: otbMRFSamplerMAP.h:101
otb::MRFSampler::LabelledImageNeighborhoodIterator
itk::NeighborhoodIterator< TInput2 > LabelledImageNeighborhoodIterator
Definition: otbMRFSampler.h:55
otb::MRFSamplerMAP::InputImageNeighborhoodIterator
Superclass::InputImageNeighborhoodIterator InputImageNeighborhoodIterator
Definition: otbMRFSamplerMAP.h:53
otb::MRFSamplerMAP::EnergyRegularizationType
Superclass::EnergyRegularizationType EnergyRegularizationType
Definition: otbMRFSamplerMAP.h:58
otb::MRFSamplerMAP::Self
MRFSamplerMAP Self
Definition: otbMRFSamplerMAP.h:48