diff --git a/Code/Markov/otbMRFOptimizer.h b/Code/Markov/otbMRFOptimizer.h index f98f17a5b588d22a7bc2f6ca1ed47cef2bdd8003..35411a505c4c4d30526a418de95ef21ce1bbe1bd 100644 --- a/Code/Markov/otbMRFOptimizer.h +++ b/Code/Markov/otbMRFOptimizer.h @@ -35,11 +35,37 @@ class ITK_EXPORT MRFOptimizer : public itk::Object typedef itk::Object Superclass; typedef itk::SmartPointer<Self> Pointer; typedef itk::SmartPointer<const Self> ConstPointer; - + typedef itk::Array< double > ParametersType; + itkNewMacro(Self); itkTypeMacro(MRFOptimizer, itk::Object); + unsigned int GetNumberOfParameters(void) const + { return m_NumberOfParameters; } + + // Get the parameters + const ParametersType& GetParameters( void ) const + { + return this->m_Parameters; + } + + void SetParameters( const ParametersType & parameters ) + { + bool modified = false; + for( unsigned int i=0; i<m_NumberOfParameters; i++ ) + { + if (m_Parameters[i] != parameters[i]) + { + m_Parameters[i] = parameters[i]; + modified = true; + } + } + if (modified) + { + this->Modified(); + } + } virtual bool Compute(double deltaEnergy) { @@ -49,6 +75,8 @@ class ITK_EXPORT MRFOptimizer : public itk::Object protected: MRFOptimizer() {} virtual ~MRFOptimizer() {} + unsigned int m_NumberOfParameters; + ParametersType m_Parameters; }; } diff --git a/Code/Markov/otbMRFOptimizerICM.h b/Code/Markov/otbMRFOptimizerICM.h new file mode 100644 index 0000000000000000000000000000000000000000..4bec79732cf26f5e6fb04b5724b3f55dcc7e5a95 --- /dev/null +++ b/Code/Markov/otbMRFOptimizerICM.h @@ -0,0 +1,54 @@ + + +#ifndef _MRFOptimizerICM_h +#define _MRFOptimizerICM_h + +#include "otbMRFOptimizer.h" + +namespace otb +{ + /** + * \class MRFOptimizerICM + * \brief This is the optimizer class implementing the ICM algorithm + * + * This is one optimizer to be used in the MRF framework. This optimizer + * follows the ICM algorithm to accept of reject the value proposed by the sampler + */ + class ITK_EXPORT MRFOptimizerICM : + public MRFOptimizer + { + public: + + typedef MRFOptimizerICM Self; + typedef itk::Object Superclass; + typedef itk::SmartPointer<Self> Pointer; + typedef itk::SmartPointer<const Self> ConstPointer; + + itkNewMacro(Self); + + itkTypeMacro(MRFOptimizerICM,Object); + + + inline bool operator()(double deltaEnergy) + { + if (deltaEnergy < 0) + { + return true; + } + else + { + return false; + } + } + + + protected: + MRFOptimizerICM() {} + virtual ~MRFOptimizerICM() {} + + }; + +} + +#endif + diff --git a/Code/Markov/otbMRFOptimizerMetropolis.h b/Code/Markov/otbMRFOptimizerMetropolis.h index b2726536fcbd54bf9ec8e52d05b845f00d0bd44d..a9954cd7c5f13262a4a79994aeef8737d63cab93 100644 --- a/Code/Markov/otbMRFOptimizerMetropolis.h +++ b/Code/Markov/otbMRFOptimizerMetropolis.h @@ -23,6 +23,14 @@ namespace otb { + /** + * \class MRFOptimizerMetropolis + * \brief This is the optimizer class implementing the Metropolis algorithm + * + * This is one optimizer to be used in the MRF framework. This optimizer + * follows the metropolis algorithm to accept of reject the value proposed by the sampler + */ + class ITK_EXPORT MRFOptimizerMetropolis : public MRFOptimizer { public: @@ -36,8 +44,8 @@ class ITK_EXPORT MRFOptimizerMetropolis : public MRFOptimizer itkTypeMacro(MRFOptimizerMetropolis,MRFOptimizer); - itkSetMacro(Temperature, double); - itkGetMacro(Temperature, double); +// itkSetMacro(Temperature, double); +// itkGetMacro(Temperature, double); inline bool Compute(double deltaEnergy) { @@ -51,7 +59,7 @@ class ITK_EXPORT MRFOptimizerMetropolis : public MRFOptimizer } else { - double proba = vcl_exp(-(deltaEnergy)/m_Temperature); + double proba = vcl_exp(-(deltaEnergy)/this->m_Parameters[0]); if ( (rand() % 10000) < proba*10000) { return true; @@ -62,7 +70,11 @@ class ITK_EXPORT MRFOptimizerMetropolis : public MRFOptimizer protected: - MRFOptimizerMetropolis() {} + MRFOptimizerMetropolis() { + this->m_NumberOfParameters = 1; + this->m_Parameters.SetSize(this->m_NumberOfParameters); + this->m_Parameters[0]=1.0; + } virtual ~MRFOptimizerMetropolis() {} double m_Temperature; }; diff --git a/Examples/Markov/MarkovClassification1Example.cxx b/Examples/Markov/MarkovClassification1Example.cxx index 01e831faad0e52a837cd1fd2e2fc1aa3ac1ba752..497b880d8a1f32e0e7905da020ff5e1508bbf27d 100644 --- a/Examples/Markov/MarkovClassification1Example.cxx +++ b/Examples/Markov/MarkovClassification1Example.cxx @@ -261,7 +261,8 @@ int main(int argc, char* argv[] ) parameters[6]=220.0;//Class 3 mean parameters[7]=10.0; //Class 3 stde energyFidelity->SetParameters(parameters); - optimizer->SetTemperature(atof(argv[6])); +// optimizer->SetTemperature(atof(argv[6])); + optimizer->SetParameters(atof(argv[6])); markovFilter->SetNumberOfClasses(nClass); markovFilter->SetMaximumNumberOfIterations(atoi(argv[5])); markovFilter->SetErrorTolerance(-1.0); diff --git a/Examples/Markov/MarkovClassification2Example.cxx b/Examples/Markov/MarkovClassification2Example.cxx index 02b9b266d562d3659f471c23fb96c36df43d03ec..7c25c723a9fe04dc75630a00a4c6225f2c88276d 100644 --- a/Examples/Markov/MarkovClassification2Example.cxx +++ b/Examples/Markov/MarkovClassification2Example.cxx @@ -68,7 +68,7 @@ // #include "otbMRFOptimizerICM.h" #include "otbMRFSamplerMAP.h" // #include "otbMRFSamplerRandomMAP.h" -#include "otbMRFOptimizerMetropolis.h" +#include "otbMRFOptimizerICM.h" // #include "otbMRFSamplerRandom.h" // Software Guide : EndCodeSnippet @@ -178,7 +178,7 @@ int main(int argc, char* argv[] ) // Software Guide : BeginCodeSnippet - typedef otb::MRFOptimizerMetropolis OptimizerType; + typedef otb::MRFOptimizerICM OptimizerType; // Software Guide : EndCodeSnippet @@ -262,7 +262,7 @@ int main(int argc, char* argv[] ) parameters[6]=220.0;//Class 3 mean parameters[7]=10.0; //Class 3 stde energyFidelity->SetParameters(parameters); - optimizer->SetTemperature(atof(argv[6])); + markovFilter->SetNumberOfClasses(nClass); markovFilter->SetMaximumNumberOfIterations(atoi(argv[5])); markovFilter->SetErrorTolerance(-1.0);