22 #ifndef otbMRFSamplerRandomMAP_h
23 #define otbMRFSamplerRandomMAP_h
25 #include "itkMersenneTwisterRandomVariateGenerator.h"
52 template <
class TInput1,
class TInput2>
77 if ((nClasses != this->m_NumberOfClasses) || (m_EnergiesInvalid ==
true))
79 this->m_NumberOfClasses = nClasses;
80 if (m_Energy !=
nullptr)
82 if (m_RepartitionFunction !=
nullptr)
83 free(m_RepartitionFunction);
84 m_Energy = (
double*)calloc(this->m_NumberOfClasses,
sizeof(
double));
85 m_RepartitionFunction = (
double*)calloc(this->m_NumberOfClasses,
sizeof(
double));
92 if (this->m_NumberOfClasses == 0)
94 itkExceptionMacro(<<
"NumberOfClasses has to be greater than 0.");
97 this->m_EnergyBefore = this->m_EnergyFidelity->GetValue(itData, itRegul.GetCenterPixel());
98 this->m_EnergyBefore += this->m_Lambda * this->m_EnergyRegularization->GetValue(itRegul, itRegul.GetCenterPixel());
101 this->m_EnergyAfter = this->m_EnergyBefore;
102 this->m_Value = itRegul.GetCenterPixel();
105 double totalProba = 0.0;
106 unsigned int valueCurrent = 0;
107 for (valueCurrent = 0; valueCurrent < this->m_NumberOfClasses; ++valueCurrent)
109 this->m_EnergyCurrent = this->m_EnergyFidelity->GetValue(itData,
static_cast<LabelledImagePixelType>(valueCurrent));
110 this->m_EnergyCurrent += this->m_Lambda * this->m_EnergyRegularization->GetValue(itRegul,
static_cast<LabelledImagePixelType>(valueCurrent));
112 m_Energy[valueCurrent] = this->m_EnergyCurrent;
113 m_RepartitionFunction[valueCurrent] = std::exp(-this->m_EnergyCurrent) + totalProba;
114 totalProba = m_RepartitionFunction[valueCurrent];
120 double select = (m_Generator->GetIntegerVariate() / (double(itk::NumericTraits<RandomGeneratorType::IntegerType>::max()) + 1) * totalProba);
122 while ((valueCurrent < this->GetNumberOfClasses()) && (m_RepartitionFunction[valueCurrent] <= select))
127 if (valueCurrent == this->GetNumberOfClasses())
129 valueCurrent = this->GetNumberOfClasses() - 1;
135 this->m_EnergyAfter = m_Energy[
static_cast<unsigned int>(valueCurrent)];
138 this->m_DeltaEnergy = this->m_EnergyAfter - this->m_EnergyBefore;
146 m_Generator->SetSeed(seed);
150 m_Generator->SetSeed();
158 m_Generator = RandomGeneratorType::GetInstance();
159 m_Generator->SetSeed();
163 if (m_Energy !=
nullptr)
165 if (m_RepartitionFunction !=
nullptr)
166 free(m_RepartitionFunction);