From 19aa1ed788ddb99babd3548d5c61e9bebfc91c0d Mon Sep 17 00:00:00 2001 From: Cyrille Valladeau <cyrille.valladeau@c-s.fr> Date: Mon, 5 May 2008 13:48:41 +0000 Subject: [PATCH] Tests Markov, deuxieme vague --- Code/Markov/otbMRFEnergy.h | 74 +++-- Code/Markov/otbMRFEnergyEdgeFidelity.h | 96 +++--- .../otbMRFEnergyGaussianClassification.h | 29 +- Code/Markov/otbMRFOptimizerICM.h | 93 +++--- Code/Markov/otbMRFOptimizerMetropolis.h | 55 ++- Code/Markov/otbMRFSampler.h | 163 ++++----- Code/Markov/otbMRFSamplerMAP.h | 156 +++++---- Code/Markov/otbMRFSamplerRandom.h | 159 +++++---- Code/Markov/otbMRFSamplerRandomMAP.h | 314 ++++++++++-------- Testing/Code/Markov/CMakeLists.txt | 144 +++++++- Testing/Code/Markov/otbMRFEnergy.cxx | 97 ++++++ .../Code/Markov/otbMRFEnergyEdgeFidelity.cxx | 47 +++ .../Markov/otbMRFEnergyEdgeFidelityNew.cxx | 40 +++ .../otbMRFEnergyGaussianClassification.cxx | 52 +++ Testing/Code/Markov/otbMRFEnergyNew.cxx | 14 +- Testing/Code/Markov/otbMRFEnergyPotts.cxx | 56 ++++ Testing/Code/Markov/otbMRFOptimizerICM.cxx | 38 +++ Testing/Code/Markov/otbMRFOptimizerICMNew.cxx | 32 ++ .../Code/Markov/otbMRFOptimizerMetropolis.cxx | 50 +++ Testing/Code/Markov/otbMRFSamplerMAP.cxx | 88 +++++ Testing/Code/Markov/otbMRFSamplerMAPNew.cxx | 38 +++ Testing/Code/Markov/otbMRFSamplerRandom.cxx | 91 +++++ .../Code/Markov/otbMRFSamplerRandomMAP.cxx | 91 +++++ .../Code/Markov/otbMRFSamplerRandomMAPNew.cxx | 39 +++ .../Code/Markov/otbMRFSamplerRandomNew.cxx | 2 +- .../Markov/otbMarkovClassificationFilter.cxx | 129 +++++++ Testing/Code/Markov/otbMarkovTests1.cxx | 10 +- Testing/Code/Markov/otbMarkovTests2.cxx | 39 +++ 28 files changed, 1713 insertions(+), 523 deletions(-) create mode 100644 Testing/Code/Markov/otbMRFEnergy.cxx create mode 100644 Testing/Code/Markov/otbMRFEnergyEdgeFidelity.cxx create mode 100644 Testing/Code/Markov/otbMRFEnergyEdgeFidelityNew.cxx create mode 100644 Testing/Code/Markov/otbMRFEnergyGaussianClassification.cxx create mode 100644 Testing/Code/Markov/otbMRFEnergyPotts.cxx create mode 100644 Testing/Code/Markov/otbMRFOptimizerICM.cxx create mode 100644 Testing/Code/Markov/otbMRFOptimizerICMNew.cxx create mode 100644 Testing/Code/Markov/otbMRFOptimizerMetropolis.cxx create mode 100644 Testing/Code/Markov/otbMRFSamplerMAP.cxx create mode 100644 Testing/Code/Markov/otbMRFSamplerMAPNew.cxx create mode 100644 Testing/Code/Markov/otbMRFSamplerRandom.cxx create mode 100644 Testing/Code/Markov/otbMRFSamplerRandomMAP.cxx create mode 100644 Testing/Code/Markov/otbMRFSamplerRandomMAPNew.cxx create mode 100644 Testing/Code/Markov/otbMarkovClassificationFilter.cxx create mode 100644 Testing/Code/Markov/otbMarkovTests2.cxx diff --git a/Code/Markov/otbMRFEnergy.h b/Code/Markov/otbMRFEnergy.h index da316ac1d4..122d7d1993 100644 --- a/Code/Markov/otbMRFEnergy.h +++ b/Code/Markov/otbMRFEnergy.h @@ -24,15 +24,14 @@ namespace otb { -template< class TInput1, class TInput2> +template< class TInput1, class TInput2 > class ITK_EXPORT MRFEnergy : public itk::Object { public: - typedef MRFEnergy Self; - typedef itk::Object Superclass; - typedef itk::SmartPointer<Self> Pointer; - typedef itk::SmartPointer<const Self> ConstPointer; - + typedef MRFEnergy Self; + typedef itk::Object Superclass; + typedef itk::SmartPointer<Self> Pointer; + typedef itk::SmartPointer<const Self> ConstPointer; typedef TInput1 InputImageType; typedef TInput2 LabelledImageType; typedef typename InputImageType::PixelType InputImagePixelType; @@ -50,7 +49,9 @@ class ITK_EXPORT MRFEnergy : public itk::Object unsigned int GetNumberOfParameters(void) const { return m_NumberOfParameters; } - + void SetNumberOfParameters(unsigned int nb) + { m_NumberOfParameters = nb; this->Modified(); } + // Get the parameters const ParametersType& GetParameters( void ) const { @@ -58,20 +59,27 @@ class ITK_EXPORT MRFEnergy : public itk::Object } void SetParameters( const ParametersType & parameters ) - { - bool modified = false; - for( unsigned int i=0; i<m_NumberOfParameters; i++ ) + { + if( parameters.Size() != m_NumberOfParameters ) + { + itkExceptionMacro(<<"Invalid number of parameters"); + } + m_Parameters = parameters; + this->Modified(); + /* + for( unsigned int i=0; i<m_NumberOfParameters; i++ ) { - if (m_Parameters[i] != parameters[i]) - { - m_Parameters[i] = parameters[i]; - modified = true; - } + if (m_Parameters[i] != parameters[i]) + { + m_Parameters[i] = parameters[i]; + modified = true; + } } - if (modified) + if (modified) { - this->Modified(); + this->Modified(); } + */ } virtual double GetSingleValue(const InputImagePixelType & value1, const LabelledImagePixelType & value2){return 0.;}; @@ -128,7 +136,7 @@ class ITK_EXPORT MRFEnergy : public itk::Object // The constructor and destructor. MRFEnergy() {}; virtual ~MRFEnergy() {}; - int m_NumberOfParameters; + unsigned int m_NumberOfParameters; ParametersType m_Parameters; }; @@ -154,7 +162,9 @@ class ITK_EXPORT MRFEnergy<TInput2,TInput2> : public itk::Object unsigned int GetNumberOfParameters(void) const { return m_NumberOfParameters; } - + void SetNumberOfParameters(unsigned int nb) + { m_NumberOfParameters = nb; this->Modified(); } + // Get the parameters const ParametersType& GetParameters( void ) const { @@ -163,19 +173,27 @@ class ITK_EXPORT MRFEnergy<TInput2,TInput2> : public itk::Object void SetParameters( const ParametersType & parameters ) { - bool modified = false; - for( unsigned int i=0; i<m_NumberOfParameters; i++ ) + if( parameters.Size() != m_NumberOfParameters ) + { + itkExceptionMacro(<<"Invalid number of parameters"); + } + m_Parameters = parameters; + this->Modified(); + /* + bool modified = false; + for( unsigned int i=0; i<(unsigned int)m_NumberOfParameters; i++ ) { - if (m_Parameters[i] != parameters[i]) - { - m_Parameters[i] = parameters[i]; - modified = true; - } + if (m_Parameters[i] != parameters[i]) + { + m_Parameters[i] = parameters[i]; + modified = true; + } } - if (modified) + if (modified) { - this->Modified(); + this->Modified(); } + */ } virtual double GetSingleValue(const LabelledImagePixelType & value1, const LabelledImagePixelType & value2){return 0.;}; diff --git a/Code/Markov/otbMRFEnergyEdgeFidelity.h b/Code/Markov/otbMRFEnergyEdgeFidelity.h index bf3501a736..730be59c05 100644 --- a/Code/Markov/otbMRFEnergyEdgeFidelity.h +++ b/Code/Markov/otbMRFEnergyEdgeFidelity.h @@ -1,3 +1,20 @@ +/*========================================================================= + + Program: ORFEO Toolbox + Language: C++ + Date: $Date$ + Version: $Revision$ + + + Copyright (c) Centre National d'Etudes Spatiales. All rights reserved. + See OTBCopyright.txt for details. + + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the above copyright notices for more information. + +=========================================================================*/ #ifndef _otbMRFEnergyEdgeFidelity_h #define _otbMRFEnergyEdgeFidelity_h @@ -7,48 +24,51 @@ namespace otb { - /** - * \class MRFEnergyEdgeFidelity - * \brief This is the implementation of an edge preserving model for Markov denoising. - * - * This is the implementation of an edge fidelity model for Markov denoising, to be used for - * regularization. Energy is: - * \f[ \sum_{t \in \mathcal{V}_s} U(x_s,x_t) = \Phi(x_s-x_t) \f] - * with - * - \f$ x_s \f$ the label on site s - * - \f$ x_t \f$ the label on site t, a neighbor of s - * - \f$ \Phi \f$ an edge preserving function: - \f[ \Phi(u) = \frac{u^2}{1+u^2} \f] - */ +/** + * \class MRFEnergyEdgeFidelity + * \brief This is the implementation of an edge preserving model for Markov denoising. + * + * This is the implementation of an edge fidelity model for Markov denoising, to be used for + * regularization. Energy is: + * \f[ \sum_{t \in \mathcal{V}_s} U(x_s,x_t) = \Phi(x_s-x_t) \f] + * with + * - \f$ x_s \f$ the label on site s + * - \f$ x_t \f$ the label on site t, a neighbor of s + * - \f$ \Phi \f$ an edge preserving function: + \f[ \Phi(u) = \frac{u^2}{1+u^2} \f] +*/ - template< class TInput1, class TInput2> - class ITK_EXPORT MRFEnergyEdgeFidelity : public MRFEnergy< TInput1, TInput2> - { - public: - typedef MRFEnergyEdgeFidelity Self; - typedef itk::Object Superclass; - typedef itk::SmartPointer<Self> Pointer; - typedef itk::SmartPointer<const Self> ConstPointer; +template< class TInput1, class TInput2> +class ITK_EXPORT MRFEnergyEdgeFidelity : public MRFEnergy< TInput1, TInput2> + { + public: + typedef MRFEnergyEdgeFidelity Self; + typedef MRFEnergy< TInput1, TInput2> Superclass; + typedef itk::SmartPointer<Self> Pointer; + typedef itk::SmartPointer<const Self> ConstPointer; - typedef itk::ConstNeighborhoodIterator< TInput1 > NeighborhoodIterator; - typedef typename TInput2::PixelType LabelledImagePixelType; + typedef itk::ConstNeighborhoodIterator< TInput1 > NeighborhoodIterator; + typedef typename TInput1::PixelType InputImagePixelType; + typedef typename TInput2::PixelType LabelledImagePixelType; + + itkNewMacro(Self); + + itkTypeMacro(MRFEnergyEdgeFidelity, MRFEnergy); + + double GetSingleValue(const InputImagePixelType & value1, const LabelledImagePixelType & value2) + { + double val1 = static_cast<double>(value1); + double val2 = static_cast<double>(value2); + + return M_SQUARE((val1 - val2))/(1+M_SQUARE(val1 - val2)); + } - itkNewMacro(Self); - -// itkSetMacro(Beta, double); - itkTypeMacro(MRFEnergy,Object); - - double GetSingleValue(const InputImagePixelType & value1, const LabelledImagePixelType & value2) - { - return M_SQUARE((value1 - value2))/double(1+M_SQUARE((value1 - value2))); - } - - protected: - // The constructor and destructor. - MRFEnergyEdgeFidelity() {}; - virtual ~MRFEnergyEdgeFidelity() {}; - }; + protected: + // The constructor and destructor. + MRFEnergyEdgeFidelity() {}; + virtual ~MRFEnergyEdgeFidelity() {}; + }; } #endif diff --git a/Code/Markov/otbMRFEnergyGaussianClassification.h b/Code/Markov/otbMRFEnergyGaussianClassification.h index 057b0c6e2d..d374b0b08a 100644 --- a/Code/Markov/otbMRFEnergyGaussianClassification.h +++ b/Code/Markov/otbMRFEnergyGaussianClassification.h @@ -57,17 +57,20 @@ namespace otb itkTypeMacro(MRFEnergyGaussianClassification, MRFEnergy); - void SetNumberOfParameters(unsigned int nParameters){ - this->m_NumberOfParameters=nParameters; - this->m_Parameters.SetSize(this->m_NumberOfParameters); - this->Modified(); - } + void SetNumberOfParameters(unsigned int nParameters) + { + this->m_NumberOfParameters=nParameters; + this->m_Parameters.SetSize(this->m_NumberOfParameters); + this->Modified(); + } double GetSingleValue(const InputImagePixelType & value1, const LabelledImagePixelType & value2) { - if (value2 >= this->m_NumberOfParameters/2) { - itkExceptionMacro(<<"Number of parameters does not correspond to number of classes" ); - } + if ((unsigned int)value2 >= this->GetNumberOfParameters()/2) + { + itkExceptionMacro(<<"Number of parameters does not correspond to number of classes" ); + } + double val1 = static_cast<double>(value1); double result = M_SQUARE(val1-this->m_Parameters[2*static_cast<int>(value2)])/(2*M_SQUARE(this->m_Parameters[2*static_cast<int>(value2)+1])) @@ -80,14 +83,16 @@ namespace otb protected: // The constructor and destructor. - MRFEnergyGaussianClassification() { - this->m_NumberOfParameters = 4; - this->m_Parameters.SetSize(this->m_NumberOfParameters); + MRFEnergyGaussianClassification() + { + this->SetNumberOfParameters ( 4 ); + this->m_Parameters.SetSize(this->GetNumberOfParameters()); this->m_Parameters[0]=50.0; //Class 0 mean this->m_Parameters[1]=10.0; //Class 0 stdev this->m_Parameters[2]=140.0;//Class 1 mean this->m_Parameters[3]=10.0; //Class 1 stdev - }; + }; + virtual ~MRFEnergyGaussianClassification() {}; }; diff --git a/Code/Markov/otbMRFOptimizerICM.h b/Code/Markov/otbMRFOptimizerICM.h index 4bec79732c..1902ba4a61 100644 --- a/Code/Markov/otbMRFOptimizerICM.h +++ b/Code/Markov/otbMRFOptimizerICM.h @@ -1,5 +1,20 @@ +/*========================================================================= + Program: ORFEO Toolbox + Language: C++ + Date: $Date$ + Version: $Revision$ + + Copyright (c) Centre National d'Etudes Spatiales. All rights reserved. + See OTBCopyright.txt for details. + + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the above copyright notices for more information. + +=========================================================================*/ #ifndef _MRFOptimizerICM_h #define _MRFOptimizerICM_h @@ -7,47 +22,45 @@ 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); +/** + * \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, MRFOptimizer); + + inline bool operator()(double deltaEnergy) + { + if (deltaEnergy < 0) + { + return true; + } + else + { + return false; + } + } + + + protected: + MRFOptimizerICM() {} + virtual ~MRFOptimizerICM() {} - 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 a9954cd7c5..7ff5923453 100644 --- a/Code/Markov/otbMRFOptimizerMetropolis.h +++ b/Code/Markov/otbMRFOptimizerMetropolis.h @@ -20,10 +20,11 @@ #include "otbMRFOptimizer.h" #include "otbMath.h" +#include "itkNumericTraits.h" namespace otb { - /** + /** * \class MRFOptimizerMetropolis * \brief This is the optimizer class implementing the Metropolis algorithm * @@ -44,9 +45,17 @@ class ITK_EXPORT MRFOptimizerMetropolis : public MRFOptimizer itkTypeMacro(MRFOptimizerMetropolis,MRFOptimizer); -// itkSetMacro(Temperature, double); -// itkGetMacro(Temperature, double); - + itkSetMacro(Temperature, double); + itkGetMacro(Temperature, double); + + /** Store a value to be used instead of random value.. FOR TEST ONLY*/ + void SetValueInsteadRandom( double val ) + { + m_ValueInsteadRandom = val; + std::cout<<"The m_ValueInsteadRandom varaible has to be used only for tests..."<<std::endl; + this->Modified(); + }; + inline bool Compute(double deltaEnergy) { if (deltaEnergy < 0) @@ -58,25 +67,39 @@ class ITK_EXPORT MRFOptimizerMetropolis : public MRFOptimizer return false; } else - { - double proba = vcl_exp(-(deltaEnergy)/this->m_Parameters[0]); - if ( (rand() % 10000) < proba*10000) - { - return true; - } - } + { + double proba = vcl_exp(-(deltaEnergy)/this->m_Parameters[0]); + double val; + if( m_ValueInsteadRandom==itk::NumericTraits<double>::min() ) + { + val = (rand() % 10000); + } + else + { + val = m_ValueInsteadRandom; + } + if ( val < proba*10000) + { + return true; + } + } return false; } protected: - MRFOptimizerMetropolis() { - this->m_NumberOfParameters = 1; - this->m_Parameters.SetSize(this->m_NumberOfParameters); - this->m_Parameters[0]=1.0; - } + MRFOptimizerMetropolis() + { + this->m_NumberOfParameters = 1; + this->m_Parameters.SetSize(this->m_NumberOfParameters); + this->m_Parameters[0]=1.0; + + m_ValueInsteadRandom=itk::NumericTraits<double>::min(); + } virtual ~MRFOptimizerMetropolis() {} double m_Temperature; + /** Store a value to be used instead of random value.. FOR TEST ONLY*/ + double m_ValueInsteadRandom; }; } diff --git a/Code/Markov/otbMRFSampler.h b/Code/Markov/otbMRFSampler.h index c036b567db..720c481f85 100644 --- a/Code/Markov/otbMRFSampler.h +++ b/Code/Markov/otbMRFSampler.h @@ -24,83 +24,98 @@ namespace otb { - /** - * \class MRFSampler - * \brief This is the base class for sampler methods used in the MRF framework. - */ - template< class TInput1, class TInput2> - class ITK_EXPORT MRFSampler:public itk::Object - { - public: - - typedef MRFSampler Self; - typedef itk::Object Superclass; - typedef itk::SmartPointer<Self> Pointer; - typedef itk::SmartPointer<const Self> ConstPointer; - - typedef itk::ConstNeighborhoodIterator< TInput1 > InputImageNeighborhoodIterator; -// typedef itk::NeighborhoodIterator< TInput1 > InputImageNeighborhoodIterator; - typedef itk::NeighborhoodIterator< TInput2 > LabelledImageNeighborhoodIterator; - typedef typename TInput2::PixelType LabelledImagePixelType; - -// typedef MRFEnergyFunctorBase<TInput1, TInput2> EnergyFidelityFunctorType; -// typedef MRFEnergy<EnergyFidelityFunctorType> EnergyFidelityType; -// typedef MRFEnergyFunctorBase<TInput2, TInput2> EnergyRegularizationFunctorType; -// typedef MRFEnergy<EnergyRegularizationFunctorType> EnergyRegularizationType; - typedef MRFEnergy<TInput1, TInput2> EnergyFidelityType; - typedef MRFEnergy<TInput2, TInput2> EnergyRegularizationType; +/** + * \class MRFSampler + * \brief This is the base class for sampler methods used in the MRF framework. + */ +template< class TInput1, class TInput2> +class ITK_EXPORT MRFSampler:public itk::Object + { + public: + typedef MRFSampler Self; + typedef itk::Object Superclass; + typedef itk::SmartPointer<Self> Pointer; + typedef itk::SmartPointer<const Self> ConstPointer; - - typedef typename EnergyFidelityType::Pointer EnergyFidelityPointer; - typedef typename EnergyRegularizationType::Pointer EnergyRegularizationPointer; - - itkNewMacro(Self); + // typedef itk::NeighborhoodIterator< TInput1 > InputImageNeighborhoodIterator; + typedef itk::ConstNeighborhoodIterator< TInput1 > InputImageNeighborhoodIterator; + typedef itk::NeighborhoodIterator< TInput2 > LabelledImageNeighborhoodIterator; + typedef typename TInput2::PixelType LabelledImagePixelType; + typedef typename TInput1::PixelType InputImagePixelType; + + // typedef MRFEnergyFunctorBase<TInput1, TInput2> EnergyFidelityFunctorType; + // typedef MRFEnergy<EnergyFidelityFunctorType> EnergyFidelityType; + // typedef MRFEnergyFunctorBase<TInput2, TInput2> EnergyRegularizationFunctorType; + // typedef MRFEnergy<EnergyRegularizationFunctorType> EnergyRegularizationType; + typedef MRFEnergy<TInput1, TInput2> EnergyFidelityType; + typedef MRFEnergy<TInput2, TInput2> EnergyRegularizationType; + typedef typename EnergyFidelityType::Pointer EnergyFidelityPointer; + typedef typename EnergyRegularizationType::Pointer EnergyRegularizationPointer; + + itkNewMacro(Self); + + itkTypeMacro(MRFSampler,itk::Object); + + // Accessors + itkSetMacro(NumberOfClasses, unsigned int); + itkGetMacro(NumberOfClasses, unsigned int); + + itkSetMacro(Lambda, double); + itkGetMacro(Lambda, double); + itkSetMacro(EnergyBefore, double); + itkGetMacro(EnergyBefore, double); + itkSetMacro(EnergyAfter, double); + itkGetMacro(EnergyAfter, double); + itkSetMacro(DeltaEnergy, double); + itkGetMacro(DeltaEnergy, double); + itkSetMacro(EnergyCurrent, double); + itkGetMacro(EnergyCurrent, double); + + itkSetMacro(Value, LabelledImagePixelType); + itkGetMacro(Value, LabelledImagePixelType); + + void SetEnergyRegularization( EnergyRegularizationPointer enerReg ){ m_EnergyRegularization=enerReg; this->Modified(); }; + EnergyRegularizationPointer GetEnergyRegularization(){ return m_EnergyRegularization; }; + void SetEnergyFidelity( EnergyFidelityPointer enerFid ){ m_EnergyFidelity=enerFid; this->Modified(); }; + EnergyFidelityPointer GetEnergyFidelity(){ return m_EnergyFidelity; }; - itkTypeMacro(MRFSampler,Object); - - itkSetMacro(NumberOfClasses, unsigned int); - itkGetMacro(NumberOfClasses, unsigned int); - - itkSetMacro(Lambda, double); - itkGetMacro(Lambda, double); - - itkGetMacro(DeltaEnergy, double); - itkGetMacro(Value, LabelledImagePixelType); - - itkSetObjectMacro( EnergyRegularization, EnergyRegularizationType); - itkSetObjectMacro( EnergyFidelity, EnergyFidelityType); - - virtual int Compute( const InputImageNeighborhoodIterator & itData, - const LabelledImageNeighborhoodIterator & itRegul) - { - - m_DeltaEnergy= 0; - - return 0; - } - - - protected: - unsigned int m_NumberOfClasses; - double m_EnergyBefore; - double m_EnergyAfter; - double m_DeltaEnergy; - LabelledImagePixelType m_Value; - EnergyRegularizationPointer m_EnergyRegularization; - EnergyFidelityPointer m_EnergyFidelity; - LabelledImagePixelType m_ValueCurrent; - double m_EnergyCurrent; - double m_Lambda; - - - protected: - // The constructor and destructor. - MRFSampler() {} - virtual ~MRFSampler() {} + virtual int Compute( const InputImageNeighborhoodIterator & itData, const LabelledImageNeighborhoodIterator & itRegul) + { + m_DeltaEnergy = 0; + + return 0; + } + + + protected: + unsigned int m_NumberOfClasses; + double m_EnergyBefore; + double m_EnergyAfter; + double m_DeltaEnergy; + double m_EnergyCurrent; + double m_Lambda; + LabelledImagePixelType m_Value; + EnergyRegularizationPointer m_EnergyRegularization; + EnergyFidelityPointer m_EnergyFidelity; + LabelledImagePixelType m_ValueCurrent; + + + protected: + // The constructor and destructor. + MRFSampler() + { + m_EnergyRegularization = EnergyRegularizationType::New(); + m_EnergyFidelity = EnergyFidelityType::New(); + + m_Lambda = 1.; + m_EnergyCurrent = 1.; + m_DeltaEnergy = 1.; + m_EnergyAfter = 1.; + m_EnergyBefore = 1.; + } + virtual ~MRFSampler() {} - }; - - + }; } #endif diff --git a/Code/Markov/otbMRFSamplerMAP.h b/Code/Markov/otbMRFSamplerMAP.h index bcd3475abc..0a07dcc476 100644 --- a/Code/Markov/otbMRFSamplerMAP.h +++ b/Code/Markov/otbMRFSamplerMAP.h @@ -1,4 +1,20 @@ +/*========================================================================= + Program: ORFEO Toolbox + Language: C++ + Date: $Date$ + Version: $Revision$ + + + Copyright (c) Centre National d'Etudes Spatiales. All rights reserved. + See OTBCopyright.txt for details. + + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the above copyright notices for more information. + +=========================================================================*/ #ifndef _MRFSamplerMAP_h #define _MRFSamplerMAP_h @@ -6,76 +22,84 @@ namespace otb { - /** - * \class MRFSamplerMAP - * \brief This is the base class for sampler methods used in the MRF framework. - * - * This is one sampler to be used int he MRF framework. This sampler select the - * value which maximizes the apriori probability (minimum energy). - */ - - template< class TInput1, class TInput2> - class ITK_EXPORT MRFSamplerMAP : - public MRFSampler< TInput1, TInput2> - { - public: - - typedef MRFSamplerMAP Self; - typedef itk::Object Superclass; - typedef itk::SmartPointer<Self> Pointer; - typedef itk::SmartPointer<const Self> ConstPointer; - - typedef itk::ConstNeighborhoodIterator< TInput1 > InputImageNeighborhoodIterator; - typedef itk::NeighborhoodIterator< TInput2 > LabelledImageNeighborhoodIterator; - typedef typename TInput2::PixelType LabelledImagePixelType; - - typedef MRFEnergy<TInput1, TInput2> EnergyFidelityType; - typedef MRFEnergy<TInput2, TInput2> EnergyRegularizationType; - - typedef typename EnergyFidelityType::Pointer EnergyFidelityPointer; - typedef typename EnergyRegularizationType::Pointer EnergyRegularizationPointer; - - itkNewMacro(Self); - - itkTypeMacro(MRFSamplerMAP,Object); - - - inline int Compute( const InputImageNeighborhoodIterator & itData, - const LabelledImageNeighborhoodIterator & itRegul) - { - this->m_EnergyBefore=this->m_EnergyFidelity->GetValue(itData, itRegul.GetCenterPixel()) - + this->m_Lambda * this->m_EnergyRegularization->GetValue(itRegul, itRegul.GetCenterPixel()); - - //Try all possible value (how to be generic ?) - this->m_EnergyAfter = this->m_EnergyBefore; //default values to current one - this->m_Value = itRegul.GetCenterPixel(); - for (LabelledImagePixelType valueCurrent = 0; - valueCurrent< this->m_NumberOfClasses; ++valueCurrent) - { - this->m_EnergyCurrent = this->m_EnergyFidelity->GetValue(itData, valueCurrent) - + this->m_Lambda * this->m_EnergyRegularization->GetValue(itRegul, valueCurrent); - if (this->m_EnergyCurrent < this->m_EnergyAfter) - { - this->m_EnergyAfter = this->m_EnergyCurrent; - this->m_Value = valueCurrent; - } - if (valueCurrent == itk::NumericTraits<LabelledImagePixelType>::max()) break; - } - - this->m_DeltaEnergy= this->m_EnergyAfter - this->m_EnergyBefore; - - return 0; - } +/** + * \class MRFSamplerMAP + * \brief This is the base class for sampler methods used in the MRF framework. + * + * This is one sampler to be used int he MRF framework. This sampler select the + * value which maximizes the apriori probability (minimum energy). + */ + +template< class TInput1, class TInput2> +class ITK_EXPORT MRFSamplerMAP : public MRFSampler< TInput1, TInput2 > +{ + public: + + typedef MRFSamplerMAP Self; + typedef MRFSampler< TInput1, TInput2 > Superclass; + typedef itk::SmartPointer<Self> Pointer; + typedef itk::SmartPointer<const Self> ConstPointer; + + typedef typename Superclass::InputImageNeighborhoodIterator InputImageNeighborhoodIterator; + typedef typename Superclass::LabelledImageNeighborhoodIterator LabelledImageNeighborhoodIterator; + typedef typename Superclass::LabelledImagePixelType LabelledImagePixelType; + typedef typename Superclass::InputImagePixelType InputImagePixelType; + typedef typename Superclass::EnergyFidelityType EnergyFidelityType; + typedef typename Superclass::EnergyRegularizationType EnergyRegularizationType; + typedef typename Superclass::EnergyFidelityPointer EnergyFidelityPointer; + typedef typename Superclass::EnergyRegularizationPointer EnergyRegularizationPointer; + + + /* + typedef itk::ConstNeighborhoodIterator< TInput1 > InputImageNeighborhoodIterator; + typedef itk::NeighborhoodIterator< TInput2 > LabelledImageNeighborhoodIterator; + typedef typename TInput2::PixelType LabelledImagePixelType; + + typedef MRFEnergy<TInput1, TInput2> EnergyFidelityType; + typedef MRFEnergy<TInput2, TInput2> EnergyRegularizationType; + typedef typename EnergyFidelityType::Pointer EnergyFidelityPointer; + typedef typename EnergyRegularizationType::Pointer EnergyRegularizationPointer; + */ + itkNewMacro(Self); + + itkTypeMacro(MRFSamplerMAP,MRFSampler); + - protected: - // The constructor and destructor. - MRFSamplerMAP() {} - virtual ~MRFSamplerMAP() {} - - }; + inline int Compute( const InputImageNeighborhoodIterator & itData, const LabelledImageNeighborhoodIterator & itRegul) + { + this->SetEnergyBefore( this->GetEnergyFidelity()->GetValue(itData, itRegul.GetCenterPixel()) + + this->GetLambda() * this->GetEnergyRegularization()->GetValue(itRegul, itRegul.GetCenterPixel()) ); + + //Try all possible value (how to be generic ?) + this->SetEnergyAfter( this->GetEnergyBefore() ); //default values to current one + this->SetValue( itRegul.GetCenterPixel() ); + for (LabelledImagePixelType valueCurrent = 0; valueCurrent< this->m_NumberOfClasses; ++valueCurrent) + { + this->SetEnergyCurrent( this->GetEnergyFidelity()->GetValue(itData, valueCurrent) + + this->GetLambda() * this->GetEnergyRegularization()->GetValue(itRegul, valueCurrent) ); + if ( this->GetEnergyCurrent() < this->GetEnergyAfter() ) + { + this->SetEnergyAfter( this->GetEnergyCurrent() ); + this->SetValue( valueCurrent ); + } + if (valueCurrent == itk::NumericTraits<LabelledImagePixelType>::max()) break; + } + + this->SetDeltaEnergy( this->GetEnergyAfter() - this->GetEnergyBefore() ); + + return 0; + } + + + protected: + // The constructor and destructor. + MRFSamplerMAP() {} + virtual ~MRFSamplerMAP() {} +}; + } #endif diff --git a/Code/Markov/otbMRFSamplerRandom.h b/Code/Markov/otbMRFSamplerRandom.h index 7c99652da1..2c497edf39 100644 --- a/Code/Markov/otbMRFSamplerRandom.h +++ b/Code/Markov/otbMRFSamplerRandom.h @@ -18,97 +18,88 @@ #ifndef _MRFSamplerRandom_h #define _MRFSamplerRandom_h -// #include "otbMRFEnergy.h" #include "otbMRFSampler.h" +#include "itkNumericTraits.h" namespace otb { - /** - * \class MRFSamplerRandom - * \brief This is the base class for sampler methods used in the MRF framework. - * - * This is one sampler to be used int he MRF framework. This sampler select the - * value randomly according to a uniform probability. - * - */ +/** + * \class MRFSamplerRandom + * \brief This is the base class for sampler methods used in the MRF framework. + * + * This is one sampler to be used int he MRF framework. This sampler select the + * value randomly according to a uniform probability. + * + */ - template< class TInput1, class TInput2> - class ITK_EXPORT MRFSamplerRandom : - public MRFSampler< TInput1, TInput2> - { - public: - - typedef MRFSamplerRandom Self; - typedef itk::Object Superclass; - typedef itk::SmartPointer<Self> Pointer; - typedef itk::SmartPointer<const Self> ConstPointer; - - typedef itk::ConstNeighborhoodIterator< TInput1 > InputImageNeighborhoodIterator; - typedef itk::NeighborhoodIterator< TInput2 > LabelledImageNeighborhoodIterator; - typedef typename TInput2::PixelType LabelledImagePixelType; - -// typedef MRFEnergyFunctorBase<TInput1, TInput2> EnergyFidelityFunctorType; -// typedef MRFEnergy<EnergyFidelityFunctorType> EnergyFidelityType; -// typedef MRFEnergyFunctorBase<TInput2, TInput2> EnergyRegularizationFunctorType; -// typedef MRFEnergy<EnergyRegularizationFunctorType> EnergyRegularizationType; - typedef MRFEnergy<TInput1, TInput2> EnergyFidelityType; - typedef MRFEnergy<TInput2, TInput2> EnergyRegularizationType; - - typedef typename EnergyFidelityType::Pointer EnergyFidelityPointer; - typedef typename EnergyRegularizationType::Pointer EnergyRegularizationPointer; - - itkNewMacro(Self); +template< class TInput1, class TInput2> +class ITK_EXPORT MRFSamplerRandom : public MRFSampler< TInput1, TInput2> + { + public: - itkTypeMacro(MRFSamplerRandom,Object); - -// itkSetMacro(NumberOfClasses, unsigned int); -// itkGetMacro(NumberOfClasses, unsigned int); -// -// itkSetMacro(Lambda, double); -// itkGetMacro(Lambda, double); -// -// itkGetMacro(DeltaEnergy, double); -// itkGetMacro(Value, LabelledImagePixelType); -// -// itkSetObjectMacro( EnergyRegularization, EnergyRegularizationType); -// itkSetObjectMacro( EnergyFidelity, EnergyFidelityType); - - inline int Compute( const InputImageNeighborhoodIterator & itData, - const LabelledImageNeighborhoodIterator & itRegul) - { - this->m_EnergyBefore= this->m_EnergyFidelity->GetValue(itData, itRegul.GetCenterPixel()); - this->m_EnergyBefore += this->m_Lambda * this->m_EnergyRegularization->GetValue(itRegul, itRegul.GetCenterPixel()); - - this->m_Value = static_cast<LabelledImagePixelType> (rand() % this->m_NumberOfClasses); - - this->m_EnergyAfter = this->m_EnergyFidelity->GetValue(itData, this->m_Value); - this->m_EnergyAfter += this->m_Lambda * this->m_EnergyRegularization->GetValue(itRegul, this->m_Value); - - this->m_DeltaEnergy= this->m_EnergyAfter - this->m_EnergyBefore; - - return 0; - } - - -// private: -// unsigned int m_NumberOfClasses; -// double m_EnergyBefore; -// double m_EnergyAfter; -// double m_DeltaEnergy; -// LabelledImagePixelType m_Value; -// EnergyFidelityPointer m_EnergyFidelity; -// EnergyRegularizationPointer m_EnergyRegularization; -// double m_Lambda; - - - protected: - // The constructor and destructor. - MRFSamplerRandom() {} - virtual ~MRFSamplerRandom() {} + typedef MRFSamplerRandom Self; + typedef MRFSampler<TInput1, TInput2> Superclass; + typedef itk::SmartPointer<Self> Pointer; + typedef itk::SmartPointer<const Self> ConstPointer; - }; - - + typedef typename Superclass::InputImageNeighborhoodIterator InputImageNeighborhoodIterator; + typedef typename Superclass::LabelledImageNeighborhoodIterator LabelledImageNeighborhoodIterator; + typedef typename Superclass::LabelledImagePixelType LabelledImagePixelType; + typedef typename Superclass::InputImagePixelType InputImagePixelType; + typedef typename Superclass::EnergyFidelityType EnergyFidelityType; + typedef typename Superclass::EnergyRegularizationType EnergyRegularizationType; + typedef typename Superclass::EnergyFidelityPointer EnergyFidelityPointer; + typedef typename Superclass::EnergyRegularizationPointer EnergyRegularizationPointer; + // Now use Superclass tepedefs + //typedef itk::ConstNeighborhoodIterator< TInput1 > InputImageNeighborhoodIterator; + //typedef itk::NeighborhoodIterator< TInput2 > LabelledImageNeighborhoodIterator; + //typedef typename TInput2::PixelType LabelledImagePixelType; + //typedef MRFEnergy<TInput1, TInput2> EnergyFidelityType; + //typedef MRFEnergy<TInput2, TInput2> EnergyRegularizationType; + //typedef typename EnergyFidelityType::Pointer EnergyFidelityPointer; + //typedef typename EnergyRegularizationType::Pointer EnergyRegularizationPointer; + + itkNewMacro(Self); + + itkTypeMacro(MRFSamplerRandom,MRFSampler); + + /** Store a value to be used instead of random value.. FOR TEST ONLY*/ + void SetValueInsteadRandom( LabelledImagePixelType val ) + { + m_ValueInsteadRandom = val; + std::cout<<"The m_ValueInsteadRandom varaible has to be used only for tests..."<<std::endl; + this->Modified(); + }; + + inline int Compute( const InputImageNeighborhoodIterator & itData, const LabelledImageNeighborhoodIterator & itRegul) + { + this->SetEnergyBefore ( this->GetEnergyFidelity()->GetValue(itData, itRegul.GetCenterPixel()) ); + this->m_EnergyBefore += this->GetLambda() * this->GetEnergyRegularization()->GetValue(itRegul, itRegul.GetCenterPixel()); + LabelledImagePixelType val; + if (m_ValueInsteadRandom == itk::NumericTraits<LabelledImagePixelType>::min()) + { + val = static_cast<LabelledImagePixelType> (rand() % this->GetNumberOfClasses()); + } + else + { + val = m_ValueInsteadRandom; + } + + this->SetValue( val ); + this->SetEnergyAfter ( this->GetEnergyFidelity()->GetValue(itData, this->GetValue()) ); + this->m_EnergyAfter += this->GetLambda() * this->GetEnergyRegularization()->GetValue(itRegul, this->GetValue()); + this->SetDeltaEnergy( this->GetEnergyAfter() - this->GetEnergyBefore() ); + + return 0; + } + + protected: + // The constructor and destructor. + MRFSamplerRandom() { m_ValueInsteadRandom=itk::NumericTraits<LabelledImagePixelType>::min(); } + virtual ~MRFSamplerRandom() {} + /** Store a value to be used instead of random value.. FOR TEST ONLY*/ + LabelledImagePixelType m_ValueInsteadRandom; + }; } #endif diff --git a/Code/Markov/otbMRFSamplerRandomMAP.h b/Code/Markov/otbMRFSamplerRandomMAP.h index 4b5aaa3803..375d7f1ffa 100644 --- a/Code/Markov/otbMRFSamplerRandomMAP.h +++ b/Code/Markov/otbMRFSamplerRandomMAP.h @@ -1,153 +1,189 @@ +/*========================================================================= + Program: ORFEO Toolbox + Language: C++ + Date: $Date$ + Version: $Revision$ + + + Copyright (c) Centre National d'Etudes Spatiales. All rights reserved. + See OTBCopyright.txt for details. + + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the above copyright notices for more information. + +=========================================================================*/ #ifndef _MRFSamplerRandomMAP_h #define _MRFSamplerRandomMAP_h -// #include "otbMRFEnergy.h" #include "otbMRFSampler.h" namespace otb { - /** - * \class MRFSamplerRandomMAP - * \brief This is the base class for sampler methods used in the MRF framework. - * - * This is one sampler to be used int he MRF framework. This sampler select the - * value randomly according to the apriori probability. - * - * The probability is defined from the energy as: - * - * \f[ P(X=x)= \frac{1}{Z} \exp^{-U(x)} \f] - * - * where \f$ Z = \sum_x \exp^{-U(x)}\f$ - * - */ - - template< class TInput1, class TInput2> - class ITK_EXPORT MRFSamplerRandomMAP : - public MRFSampler< TInput1, TInput2> - { - public: - - typedef MRFSamplerRandomMAP Self; - typedef itk::Object Superclass; - typedef itk::SmartPointer<Self> Pointer; - typedef itk::SmartPointer<const Self> ConstPointer; - - typedef itk::ConstNeighborhoodIterator< TInput1 > InputImageNeighborhoodIterator; -// typedef itk::NeighborhoodIterator< TInput1 > InputImageNeighborhoodIterator; - typedef itk::NeighborhoodIterator< TInput2 > LabelledImageNeighborhoodIterator; - typedef typename TInput2::PixelType LabelledImagePixelType; - - typedef MRFEnergy<TInput1, TInput2> EnergyFidelityType; - typedef MRFEnergy<TInput2, TInput2> EnergyRegularizationType; - - typedef typename EnergyFidelityType::Pointer EnergyFidelityPointer; - typedef typename EnergyRegularizationType::Pointer EnergyRegularizationPointer; - - itkNewMacro(Self); +/** + * \class MRFSamplerRandomMAP + * \brief This is the base class for sampler methods used in the MRF framework. + * + * This is one sampler to be used int he MRF framework. This sampler select the + * value randomly according to the apriori probability. + * + * The probability is defined from the energy as: + * + * \f[ P(X=x)= \frac{1}{Z} \exp^{-U(x)} \f] + * + * where \f$ Z = \sum_x \exp^{-U(x)}\f$ + * + */ + +template< class TInput1, class TInput2> +class ITK_EXPORT MRFSamplerRandomMAP : public MRFSampler< TInput1, TInput2> + { + public: - itkTypeMacro(MRFSamplerRandomMAP,Object); - -// void SetNumberOfClasses(unsigned int arg) -// { -// otbDebugMacro("setting NumberOfClasses to " << arg); -// if (this->m_NumberOfClasses != arg) -// { -// this->m_NumberOfClasses = arg; -// repartitionFunction = (double *) calloc(m_NumberOfClasses, sizeof(double)); -// energy = (double *) calloc(m_NumberOfClasses, sizeof(double)); -// this->Modified(); -// } -// } -// itkGetMacro(NumberOfClasses, unsigned int); -// -// itkSetMacro(Lambda, double); -// itkGetMacro(Lambda, double); -// -// itkGetMacro(DeltaEnergy, double); -// itkGetMacro(Value, LabelledImagePixelType); -// -// itkSetObjectMacro( EnergyRegularization, EnergyRegularizationType); -// itkSetObjectMacro( EnergyFidelity, EnergyFidelityType); - - inline int Compute( const InputImageNeighborhoodIterator & itData, - const LabelledImageNeighborhoodIterator & itRegul) - { - this->m_EnergyBefore=this->m_EnergyFidelity->GetValue(itData, itRegul.GetCenterPixel()) - + this->m_Lambda * this->m_EnergyRegularization->GetValue(itRegul, itRegul.GetCenterPixel()); - - //Try all possible value (how to be generic ?) - this->m_EnergyAfter = this->m_EnergyBefore; //default values to current one - this->m_Value = itRegul.GetCenterPixel(); -// otbDebugMacro(<< "Computing MAP for pix " << itData.GetIndex()); - //Compute probability for each possibility - double totalProba=0.0; - for (LabelledImagePixelType valueCurrent = 0; - valueCurrent < this->m_NumberOfClasses; ++valueCurrent) - { -// otbDebugMacro(<< " --> Proposed value " << static_cast<double>(valueCurrent)); - this->m_EnergyCurrent = this->m_EnergyFidelity->GetValue(itData, valueCurrent) - + this->m_Lambda * this->m_EnergyRegularization->GetValue(itRegul, valueCurrent); - - energy[valueCurrent] = this->m_EnergyCurrent; - repartitionFunction[valueCurrent] = vcl_exp(-this->m_EnergyCurrent)+totalProba; - totalProba = repartitionFunction[valueCurrent]; -// otbDebugMacro("valueCurrent, repartitionFunction[valueCurrent] " << (unsigned int) valueCurrent << ", " << repartitionFunction[valueCurrent]); - - } - - //Pick a value according to probability - - double select = (rand()/(double(RAND_MAX)+1) * totalProba); -// otbDebugMacro("repartitionFunction " << repartitionFunction[0] << " " -// << repartitionFunction[1] << " " << repartitionFunction[2] << " " -// << repartitionFunction[3] << " "); - -// otbDebugMacro("select, totalProba " << select << ", " << totalProba); - LabelledImagePixelType valueCurrent = 0; - for (valueCurrent = 0; - valueCurrent < this->m_NumberOfClasses; ++valueCurrent) - { - if (repartitionFunction[valueCurrent] > select) break; - } - - if ( this->m_Value != valueCurrent) - { - this->m_Value = valueCurrent; - this->m_EnergyAfter = energy[valueCurrent]; - } - - this->m_DeltaEnergy= this->m_EnergyAfter - this->m_EnergyBefore; -// otbDebugMacro("Decision " << (unsigned int) valueCurrent); - return 0; - } - - - private: -// unsigned int m_NumberOfClasses; -// double m_EnergyBefore; -// double m_EnergyAfter; -// double m_DeltaEnergy; -// LabelledImagePixelType m_Value; -// EnergyRegularizationPointer m_EnergyRegularization; -// EnergyFidelityPointer m_EnergyFidelity; -// LabelledImagePixelType valueCurrent; -// double m_EnergyCurrent; - double * repartitionFunction ; - double * energy; -// double m_Lambda; - - - protected: - // The constructor and destructor. - MRFSamplerRandomMAP() { - energy = (double *) calloc(this->m_NumberOfClasses, sizeof(double)); - repartitionFunction = (double *) calloc(this->m_NumberOfClasses, sizeof(double)); - } - virtual ~MRFSamplerRandomMAP() {} + typedef MRFSamplerRandomMAP Self; + typedef MRFSampler< TInput1, TInput2> Superclass; + typedef itk::SmartPointer<Self> Pointer; + typedef itk::SmartPointer<const Self> ConstPointer; - }; + typedef typename Superclass::InputImageNeighborhoodIterator InputImageNeighborhoodIterator; + typedef typename Superclass::LabelledImageNeighborhoodIterator LabelledImageNeighborhoodIterator; + typedef typename Superclass::LabelledImagePixelType LabelledImagePixelType; + typedef typename Superclass::InputImagePixelType InputImagePixelType; + typedef typename Superclass::EnergyFidelityType EnergyFidelityType; + typedef typename Superclass::EnergyRegularizationType EnergyRegularizationType; + typedef typename Superclass::EnergyFidelityPointer EnergyFidelityPointer; + typedef typename Superclass::EnergyRegularizationPointer EnergyRegularizationPointer; + /* + typedef itk::ConstNeighborhoodIterator< TInput1 > InputImageNeighborhoodIterator; + //typedef itk::NeighborhoodIterator< TInput1 > InputImageNeighborhoodIterator; + typedef itk::NeighborhoodIterator< TInput2 > LabelledImageNeighborhoodIterator; + typedef typename TInput2::PixelType LabelledImagePixelType; + + typedef MRFEnergy<TInput1, TInput2> EnergyFidelityType; + typedef MRFEnergy<TInput2, TInput2> EnergyRegularizationType; + typedef typename EnergyFidelityType::Pointer EnergyFidelityPointer; + typedef typename EnergyRegularizationType::Pointer EnergyRegularizationPointer; + */ + itkNewMacro(Self); + + itkTypeMacro(MRFSamplerRandomMAP, MRFSampler); + + /* + void SetNumberOfClasses(unsigned int arg) + { + otbDebugMacro("setting NumberOfClasses to " << arg); + if (this->m_NumberOfClasses != arg) + { + this->m_NumberOfClasses = arg; + m_RepartitionFunction = (double *) calloc(m_NumberOfClasses, sizeof(double)); + m_Energy = (double *) calloc(m_NumberOfClasses, sizeof(double)); + this->Modified(); + } + } + itkGetMacro(NumberOfClasses, unsigned int); + + itkSetMacro(Lambda, double); + itkGetMacro(Lambda, double); + + itkGetMacro(DeltaEnergy, double); + itkGetMacro(Value, LabelledImagePixelType); + + itkSetObjectMacro( EnergyRegularization, EnergyRegularizationType); + itkSetObjectMacro( EnergyFidelity, EnergyFidelityType); + */ + + inline int Compute( const InputImageNeighborhoodIterator & itData, const LabelledImageNeighborhoodIterator & itRegul) + { + this->SetEnergyBefore( this->GetEnergyFidelity()->GetValue(itData, itRegul.GetCenterPixel()) + + this->GetLambda() * this->GetEnergyRegularization()->GetValue(itRegul, itRegul.GetCenterPixel()) ); + + //Try all possible value (how to be generic ?) + this->SetEnergyAfter( this->GetEnergyBefore() ); //default values to current one + this->SetValue( itRegul.GetCenterPixel() ); + // otbDebugMacro(<< "Computing MAP for pix " << itData.GetIndex()); + // Compute probability for each possibility + double totalProba=0.0; + for (unsigned int valueCurrent = 0; valueCurrent < this->GetNumberOfClasses(); ++valueCurrent) + { + // otbDebugMacro(<< " --> Proposed value " << static_cast<double>(valueCurrent)); + this->SetEnergyCurrent( this->GetEnergyFidelity()->GetValue(itData, valueCurrent) + + this->GetLambda() * this->GetEnergyRegularization()->GetValue(itRegul, valueCurrent) ); + + m_Energy[valueCurrent] = this->GetEnergyCurrent(); + m_RepartitionFunction[valueCurrent] = vcl_exp(-this->GetEnergyCurrent())+totalProba; + totalProba = m_RepartitionFunction[valueCurrent]; + // otbDebugMacro("valueCurrent, m_RepartitionFunction[valueCurrent] " << (unsigned int) valueCurrent << ", " << m_RepartitionFunction[valueCurrent]); + + } + + //Pick a value according to probability + + double select; + if (m_ValueInsteadRandom == itk::NumericTraits<double>::min()) + { + select = (rand()/(double(RAND_MAX)+1) * totalProba); + } + else + { + select = m_ValueInsteadRandom; + } + // otbDebugMacro("m_RepartitionFunction " << m_RepartitionFunction[0] << " " + // << m_RepartitionFunction[1] << " " << m_RepartitionFunction[2] << " " + // << m_RepartitionFunction[3] << " "); + + // otbDebugMacro("select, totalProba " << select << ", " << totalProba); + unsigned int valueCurrent = 0; + for (valueCurrent = 0; valueCurrent < this->m_NumberOfClasses; ++valueCurrent) + { + if (m_RepartitionFunction[valueCurrent] > select) break; + } + + if ( this->GetValue() != static_cast<LabelledImagePixelType>(valueCurrent)) + { + this->SetValue( valueCurrent ); + this->SetEnergyAfter( m_Energy[valueCurrent] ); + } + + this->SetDeltaEnergy( this->GetEnergyAfter() - this->GetEnergyBefore() ); + // otbDebugMacro("Decision " << (unsigned int) valueCurrent); + return 0; + } + + /** Store a value to be used instead of random value.. FOR TEST ONLY*/ + void SetValueInsteadRandom( double val ) + { + m_ValueInsteadRandom = val; + std::cout<<"The m_ValueInsteadRandom varaible has to be used only for tests..."<<std::endl; + this->Modified(); + }; + + private: + double * m_RepartitionFunction ; + double * m_Energy; + + + + protected: + // The constructor and destructor. + MRFSamplerRandomMAP() + { + m_Energy = (double *) calloc(this->GetNumberOfClasses(), sizeof(double)); + m_RepartitionFunction = (double *) calloc(this->GetNumberOfClasses(), sizeof(double)); + m_ValueInsteadRandom = itk::NumericTraits<double>::min(); + } + virtual ~MRFSamplerRandomMAP() + { + free(m_Energy); + free(m_RepartitionFunction); + } + + /** Store a value to be used instead of random value.. FOR TEST ONLY*/ + double m_ValueInsteadRandom; + + }; } diff --git a/Testing/Code/Markov/CMakeLists.txt b/Testing/Code/Markov/CMakeLists.txt index 0c1c184e40..0b180b6130 100755 --- a/Testing/Code/Markov/CMakeLists.txt +++ b/Testing/Code/Markov/CMakeLists.txt @@ -13,7 +13,7 @@ SET(TOL 0.0) SET(EPSILON 0.000000000001) SET(EPSILON_10e6 0.000001) SET(MARKOV_TESTS1 ${CXX_TEST_PATH}/otbMarkovTests1) - +SET(MARKOV_TESTS2 ${CXX_TEST_PATH}/otbMarkovTests2) # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ otbMARKOV_TESTS1 ~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -23,68 +23,176 @@ SET(MARKOV_TESTS1 ${CXX_TEST_PATH}/otbMarkovTests1) ADD_TEST(maTuMarkovClassificationFilterNew ${MARKOV_TESTS1} otbMarkovClassificationFilterNew ) -#ADD_TEST(raTvNDVI_RAndNIRVegetationIndexImageFilter ${RADIOMETRY_TESTS1} -# --compare-image ${TOL} ${BASELINE}/raRAndNIRVegetationIndex_NDVI_poupees_subc1c2.tif -# ${TEMP}/raRAndNIRVegetationIndex_NDVI_poupees_subc1c2.tif -# otbRAndNIRVegetationIndexImageFilter -# NDVI -# ${INPUTDATA}/poupees_sub_c1.png -# ${INPUTDATA}/poupees_sub_c2.png -# ${TEMP}/raRAndNIRVegetationIndex_NDVI_poupees_subc1c2.tif -# ) +# ADD_TEST(maTuMarkovClassificationFilter ${MARKOV_TESTS1} +# --compare-ascii ${TOL} ${BASELINE_FILES}/maTuMarkovClassificationFilter.txt +# ${TEMP}/maTuMarkovClassificationFilter.txt +# otbMarkovClassificationFilter +# ${TEMP}/maTuMarkovClassificationFilter.txt +# ) # ------- otb::MRFEnergy ------------------------------ ADD_TEST(maTuMRFEnergyNew ${MARKOV_TESTS1} otbMRFEnergyNew ) +ADD_TEST(maTvMRFEnergy ${MARKOV_TESTS1} +--compare-ascii ${TOL} ${BASELINE_FILES}/maTvMRFEnergy.txt + ${TEMP}/maTvMRFEnergy.txt + otbMRFEnergy + ${INPUTDATA}/poupees_sub_c1.png + ${INPUTDATA}/poupees_sub_c2.png + ${TEMP}/maTvMRFEnergy.txt + ) + +# ------- otb::MRFEnergyEdgeFidelity ------------------------------ +ADD_TEST(maTuMRFEnergyEdgeFidelityNew ${MARKOV_TESTS1} + otbMRFEnergyEdgeFidelityNew ) + +ADD_TEST(maTvMRFEnergyEdgeFidelity ${MARKOV_TESTS1} +--compare-ascii ${TOL} ${BASELINE_FILES}/maTvMRFEnergyEdgeFidelity.txt + ${TEMP}/maTvMRFEnergyEdgeFidelity.txt + otbMRFEnergyEdgeFidelity + ${TEMP}/maTvMRFEnergyEdgeFidelity.txt + ) + # ------- otb::MRFEnergyGaussianClassification ------------------------------ ADD_TEST(maTuMRFEnergyGaussianClassificationNew ${MARKOV_TESTS1} otbMRFEnergyGaussianClassificationNew ) +ADD_TEST(maTvMRFEnergyGaussianClassification ${MARKOV_TESTS1} +--compare-ascii ${TOL} ${BASELINE_FILES}/maTvMRFEnergyGaussianClassification.txt + ${TEMP}/maTvMRFEnergyGaussianClassification.txt + otbMRFEnergyGaussianClassification + ${TEMP}/maTvMRFEnergyGaussianClassification.txt + ) + # ------- otb::MRFEnergyPotts ------------------------------ ADD_TEST(maTuMRFEnergyPottsNew ${MARKOV_TESTS1} otbMRFEnergyPottsNew ) -# ------- otbMRFOptimizerNew ------------------------------ +ADD_TEST(maTvMRFEnergyPotts ${MARKOV_TESTS1} + --compare-ascii ${TOL} + ${BASELINE_FILES}/maTvMRFEnergyPotts.txt + ${TEMP}/maTvMRFEnergyPotts.txt + otbMRFEnergyPotts + ${TEMP}/maTvMRFEnergyPotts.txt + ) + +# ------- otbMRFOptimizer ------------------------------ ADD_TEST(maTuMRFOptimizerNew ${MARKOV_TESTS1} otbMRFOptimizerNew ) -# ------- otbMRFOptimizerMetropolisNew ------------------------------ -ADD_TEST(maTuMRFOptimizerMetropolisNew ${MARKOV_TESTS1} +# ------- otbMRFOptimizerICM ------------------------------ +ADD_TEST(maTuMRFOptimizerICMNew ${MARKOV_TESTS1} + otbMRFOptimizerICMNew ) + +ADD_TEST(maTuMRFOptimizerICM ${MARKOV_TESTS1} + otbMRFOptimizerICM + ${TEMP}/maTuMRFOptimizerICM.txt + ) + +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ otbMARKOV_TESTS2 ~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +# ------- otbMRFOptimizerMetropolis ------------------------------ +ADD_TEST(maTuMRFOptimizerMetropolisNew ${MARKOV_TESTS2} otbMRFOptimizerMetropolisNew ) -# ------- otbMRFSamplerNew ------------------------------ -ADD_TEST(maTuMRFSamplerNew ${MARKOV_TESTS1} +ADD_TEST(maTvMRFOptimizerMetropolis ${MARKOV_TESTS2} + --compare-ascii ${TOL} + ${BASELINE_FILES}/maTvMRFOptimizerMetropolis.txt + ${TEMP}/maTvMRFOptimizerMetropolis.txt + otbMRFOptimizerMetropolis + ${TEMP}/maTvMRFOptimizerMetropolis.txt + ) + + +# ------- otbMRFSampler ------------------------------ +ADD_TEST(maTuMRFSamplerNew ${MARKOV_TESTS2} otbMRFSamplerNew ) +# ------- otbMRFSamplerMAP ------------------------------ +ADD_TEST(maTuMRFSamplerMAPNew ${MARKOV_TESTS2} + otbMRFSamplerMAPNew ) +ADD_TEST(maTvMRFSamplerMAP ${MARKOV_TESTS2} + --compare-ascii ${TOL} + ${BASELINE_FILES}/maTvMRFSamplerMAP.txt + ${TEMP}/maTvMRFSamplerMAP.txt + otbMRFSamplerMAP + ${INPUTDATA}/poupees_sub_c1.png + ${INPUTDATA}/poupees_sub_c2.png + ${TEMP}/maTvMRFSamplerMAP.txt + ) + -# ------- otbMRFSamplerRandomNew ------------------------------ - ADD_TEST(maTuMRFSamplerRandomNew ${MARKOV_TESTS1} +# ------- otbMRFSamplerRandom ------------------------------ + ADD_TEST(maTuMRFSamplerRandomNew ${MARKOV_TESTS2} otbMRFSamplerRandomNew ) +ADD_TEST(maTvMRFSamplerRandom ${MARKOV_TESTS2} + --compare-ascii ${TOL} + ${BASELINE_FILES}/maTvMRFSamplerRandom.txt + ${TEMP}/maTvMRFSamplerRandom.txt + otbMRFSamplerRandom + ${INPUTDATA}/poupees_sub_c1.png + ${INPUTDATA}/poupees_sub_c2.png + ${TEMP}/maTvMRFSamplerRandom.txt + ) + +# ------- otbMRFSamplerRandomMAP ------------------------------ + ADD_TEST(maTuMRFSamplerRandomMAPNew ${MARKOV_TESTS2} + otbMRFSamplerRandomMAPNew ) + +ADD_TEST(maTvMRFSamplerRandomMAP ${MARKOV_TESTS2} + --compare-ascii ${TOL} + ${BASELINE_FILES}/maTvMRFSamplerRandomMAP.txt + ${TEMP}/maTvMRFSamplerRandomMAP.txt + otbMRFSamplerRandomMAP + ${INPUTDATA}/poupees_sub_c1.png + ${INPUTDATA}/poupees_sub_c2.png + ${TEMP}/maTvMRFSamplerRandomMAP.txt + ) # A enrichir SET(Markov_SRCS1 otbMarkovClassificationFilterNew.cxx otbMRFEnergyNew.cxx +otbMRFEnergy.cxx otbMRFEnergyPottsNew.cxx +otbMRFEnergyPotts.cxx +otbMRFEnergyEdgeFidelityNew.cxx +otbMRFEnergyEdgeFidelity.cxx otbMRFEnergyGaussianClassificationNew.cxx +otbMRFEnergyGaussianClassification.cxx otbMRFOptimizerNew.cxx +otbMRFOptimizerICMNew.cxx +otbMRFOptimizerICM.cxx +) + +SET(Markov_SRCS2 otbMRFOptimizerMetropolisNew.cxx +otbMRFOptimizerMetropolis.cxx otbMRFSamplerNew.cxx +otbMRFSamplerMAPNew.cxx +otbMRFSamplerMAP.cxx otbMRFSamplerRandomNew.cxx +otbMRFSamplerRandom.cxx +otbMRFSamplerRandomMAPNew.cxx +otbMRFSamplerRandomMAP.cxx ) - INCLUDE_DIRECTORIES("${OTBTesting_BINARY_DIR}") ADD_EXECUTABLE(otbMarkovTests1 otbMarkovTests1.cxx ${Markov_SRCS1}) TARGET_LINK_LIBRARIES(otbMarkovTests1 OTBMarkov OTBCommon OTBIO gdal ITKIO ITKCommon ITKBasicFilters) +ADD_EXECUTABLE(otbMarkovTests2 otbMarkovTests2.cxx ${Markov_SRCS2}) +TARGET_LINK_LIBRARIES(otbMarkovTests2 OTBMarkov OTBCommon OTBIO gdal ITKIO ITKCommon ITKBasicFilters) + ENDIF( NOT OTB_DISABLE_CXX_TESTING ) diff --git a/Testing/Code/Markov/otbMRFEnergy.cxx b/Testing/Code/Markov/otbMRFEnergy.cxx new file mode 100644 index 0000000000..60e2c55a2b --- /dev/null +++ b/Testing/Code/Markov/otbMRFEnergy.cxx @@ -0,0 +1,97 @@ +/*========================================================================= + + Program: ORFEO Toolbox + Language: C++ + Date: $Date$ + Version: $Revision$ + + + Copyright (c) Centre National d'Etudes Spatiales. All rights reserved. + See OTBCopyright.txt for details. + + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the above copyright notices for more information. + +=========================================================================*/ +#if defined(_MSC_VER) +#pragma warning ( disable : 4786 ) +#endif + +#include "otbMRFEnergy.h" +#include "otbImage.h" +#include "otbImageFileReader.h" +#include <fstream> + +int otbMRFEnergy(int argc, char * argv[]) +{ + const char * inputImage = argv[1]; + const char * labelImage = argv[2]; + const char * outputFile = argv[3]; + + typedef double PixelTypeInput; + typedef int PixelTypeLabel; + typedef otb::Image<PixelTypeInput, 2> ImageType; + typedef otb::Image<PixelTypeLabel, 2> LabelType; + typedef otb::MRFEnergy< ImageType, LabelType> MRFEnergyType; + typedef otb::ImageFileReader< ImageType > ReaderInputType; + typedef otb::ImageFileReader< LabelType > ReaderLabelType; + typedef MRFEnergyType::InputImagePixelType InputImagePixelType; + typedef MRFEnergyType::LabelledImagePixelType LabelledImagePixelType; + typedef MRFEnergyType::LabelledNeighborhoodIterator LabelledNeighborhoodIterator; + typedef MRFEnergyType::InputNeighborhoodIterator InputNeighborhoodIterator; + typedef MRFEnergyType::ParametersType ParametersType; + + MRFEnergyType::Pointer object = MRFEnergyType::New(); + ReaderInputType::Pointer readerIn = ReaderInputType::New(); + ReaderLabelType::Pointer readerLab = ReaderLabelType::New(); + + readerIn->SetFileName( inputImage ); + readerLab->SetFileName( labelImage ); + readerIn->Update(); + readerLab->Update(); + + object->SetNumberOfParameters( 4 ); + + ParametersType parameters; + parameters.SetSize(4); + parameters[0]=10.0; //Class 0 mean + parameters[1]=10.0; //Class 0 stdev + parameters[2]=80.0; //Class 1 mean + parameters[3]=10.0; //Class 1 stdev + + object->SetParameters( parameters ); + + ImageType::IndexType idIn; + LabelType::IndexType idLab; + idIn[0] = 50; + idIn[1] = 50; + idLab[0] = 100; + idLab[1] = 100; + InputImagePixelType inPix = readerIn->GetOutput()->GetPixel( idIn ); + LabelledImagePixelType inLab = readerLab->GetOutput()->GetPixel( idLab ); + + InputNeighborhoodIterator::RadiusType radIn; + radIn.Fill(3); + LabelledNeighborhoodIterator::RadiusType radLab; + radLab.Fill(3); + + InputNeighborhoodIterator iterIn = InputNeighborhoodIterator( radIn, readerIn->GetOutput(), readerIn->GetOutput()->GetLargestPossibleRegion()); + + LabelledNeighborhoodIterator iterLab = LabelledNeighborhoodIterator( radLab, readerLab->GetOutput(), readerLab->GetOutput()->GetLargestPossibleRegion()); + + std::ofstream file; + file.open(outputFile); + file<<"Used pixels: (50, 50) -> "<<inPix<<" , (100, 100) -> "<<inLab<<std::endl; + file<<std::endl; + file<<"GetSingleValue(const InputImagePixelType, const LabelledImagePixelType): "<< object->GetValue(inPix, inLab)<<std::endl; + file<<"GetValue(const InputImagePixelType, const LabelledImagePixelType): "<< object->GetValue(inPix, inLab)<<std::endl; + file<<"GetValue(const LabelledNeighborhoodIterator, const LabelledImagePixelType): "<< object->GetValue(iterLab, inLab)<<std::endl; + file<<"GetValue(const InputNeighborhoodIterator, const LabelledImagePixelType): "<< object->GetValue(iterIn, inLab)<<std::endl; + + file.close(); + + return EXIT_SUCCESS; +} + diff --git a/Testing/Code/Markov/otbMRFEnergyEdgeFidelity.cxx b/Testing/Code/Markov/otbMRFEnergyEdgeFidelity.cxx new file mode 100644 index 0000000000..091fffd843 --- /dev/null +++ b/Testing/Code/Markov/otbMRFEnergyEdgeFidelity.cxx @@ -0,0 +1,47 @@ +/*========================================================================= + + Program: ORFEO Toolbox + Language: C++ + Date: $Date$ + Version: $Revision$ + + + Copyright (c) Centre National d'Etudes Spatiales. All rights reserved. + See OTBCopyright.txt for details. + + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the above copyright notices for more information. + +=========================================================================*/ +#if defined(_MSC_VER) +#pragma warning ( disable : 4786 ) +#endif + +#include "otbMRFEnergyEdgeFidelity.h" +#include "otbImage.h" +#include <fstream> + +int otbMRFEnergyEdgeFidelity(int argc, char * argv[]) +{ + const char * outputFile = argv[1]; + + typedef double PixelTypeInput; + typedef int PixelTypeLabel; + typedef otb::Image<PixelTypeInput, 2> ImageType; + typedef otb::Image<PixelTypeLabel, 2> LabelType; + typedef otb::MRFEnergyEdgeFidelity< ImageType, LabelType> MRFEnergyType; + + MRFEnergyType::Pointer object = MRFEnergyType::New(); + + std::ofstream file; + file.open(outputFile); + + file<<"GetSimngleValue(10, 50): "<< object->GetSingleValue(10, 50)<<std::endl; + + file.close(); + + return EXIT_SUCCESS; +} + diff --git a/Testing/Code/Markov/otbMRFEnergyEdgeFidelityNew.cxx b/Testing/Code/Markov/otbMRFEnergyEdgeFidelityNew.cxx new file mode 100644 index 0000000000..b33d8f0fcc --- /dev/null +++ b/Testing/Code/Markov/otbMRFEnergyEdgeFidelityNew.cxx @@ -0,0 +1,40 @@ +/*========================================================================= + + Program: ORFEO Toolbox + Language: C++ + Date: $Date$ + Version: $Revision$ + + + Copyright (c) Centre National d'Etudes Spatiales. All rights reserved. + See OTBCopyright.txt for details. + + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the above copyright notices for more information. + +=========================================================================*/ +#if defined(_MSC_VER) +#pragma warning ( disable : 4786 ) +#endif + +#include "otbMRFEnergyEdgeFidelity.h" +#include "otbImage.h" + + +int otbMRFEnergyEdgeFidelityNew(int argc, char * argv[]) +{ + typedef double PixelType; + typedef int PixelType2; + + typedef otb::Image<PixelType, 2> ImageType; + typedef otb::Image<PixelType2, 2> ImageType2; + typedef otb::MRFEnergyEdgeFidelity< ImageType, ImageType > MRFEnergyType; + + MRFEnergyType::Pointer object = MRFEnergyType::New(); + + + return EXIT_SUCCESS; +} + diff --git a/Testing/Code/Markov/otbMRFEnergyGaussianClassification.cxx b/Testing/Code/Markov/otbMRFEnergyGaussianClassification.cxx new file mode 100644 index 0000000000..5d11fe4fdb --- /dev/null +++ b/Testing/Code/Markov/otbMRFEnergyGaussianClassification.cxx @@ -0,0 +1,52 @@ +/*========================================================================= + + Program: ORFEO Toolbox + Language: C++ + Date: $Date$ + Version: $Revision$ + + + Copyright (c) Centre National d'Etudes Spatiales. All rights reserved. + See OTBCopyright.txt for details. + + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the above copyright notices for more information. + +=========================================================================*/ +#if defined(_MSC_VER) +#pragma warning ( disable : 4786 ) +#endif + +#include "otbMRFEnergyGaussianClassification.h" +#include "otbImage.h" +#include <fstream> + + +int otbMRFEnergyGaussianClassification(int argc, char * argv[]) +{ + const char * outputFile = argv[1]; + + typedef double PixelTypeInput; + typedef int PixelTypeLabel; + typedef otb::Image<PixelTypeInput, 2> ImageType; + typedef otb::Image<PixelTypeLabel, 2> LabelType; + + typedef otb::MRFEnergyGaussianClassification<ImageType, LabelType> MRFGaussianType; + + MRFGaussianType::Pointer classif = MRFGaussianType::New(); + + + std::ofstream file; + file.open(outputFile); + + file<<std::endl; + file<<"Number of paramters: "<<classif->GetNumberOfParameters()<<std::endl; + file<<"GetSingleValue(10, 1): "<<classif->GetSingleValue(10, 1)<<std::endl; + + file.close(); + + return EXIT_SUCCESS; +} + diff --git a/Testing/Code/Markov/otbMRFEnergyNew.cxx b/Testing/Code/Markov/otbMRFEnergyNew.cxx index a63c3ef33f..5243993471 100644 --- a/Testing/Code/Markov/otbMRFEnergyNew.cxx +++ b/Testing/Code/Markov/otbMRFEnergyNew.cxx @@ -26,11 +26,17 @@ int otbMRFEnergyNew(int argc, char * argv[]) { typedef double PixelType; - typedef otb::Image<PixelType, 2> ImageType; - typedef otb::MRFEnergy< ImageType, ImageType> MRFEnergyType; + typedef int PixelType2; + + typedef otb::Image<PixelType, 2> ImageType; + typedef otb::Image<PixelType2, 2> ImageType2; + typedef otb::MRFEnergy< ImageType, ImageType > MRFEnergyType; + typedef otb::MRFEnergy< ImageType, ImageType2 > MRFEnergyType2; + + MRFEnergyType::Pointer object = MRFEnergyType::New(); + MRFEnergyType2::Pointer object2 = MRFEnergyType2::New(); + - MRFEnergyType::Pointer object = MRFEnergyType::New(); - return EXIT_SUCCESS; } diff --git a/Testing/Code/Markov/otbMRFEnergyPotts.cxx b/Testing/Code/Markov/otbMRFEnergyPotts.cxx new file mode 100644 index 0000000000..4aa8279e46 --- /dev/null +++ b/Testing/Code/Markov/otbMRFEnergyPotts.cxx @@ -0,0 +1,56 @@ +/*========================================================================= + + Program: ORFEO Toolbox + Language: C++ + Date: $Date$ + Version: $Revision$ + + + Copyright (c) Centre National d'Etudes Spatiales. All rights reserved. + See OTBCopyright.txt for details. + + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the above copyright notices for more information. + +=========================================================================*/ +#if defined(_MSC_VER) +#pragma warning ( disable : 4786 ) +#endif + +#include "otbMRFEnergyPotts.h" +#include "otbImage.h" +#include <fstream> + +int otbMRFEnergyPotts(int argc, char * argv[]) +{ + const char * outputFile = argv[1]; + + typedef double PixelType; + typedef int PixelType2; + typedef otb::Image<PixelType, 2> ImageType; + typedef otb::Image<PixelType2, 2> LabelType; + + typedef otb::MRFEnergyPotts< ImageType, LabelType> MRFEnergyPottsType; + typedef MRFEnergyPottsType::InputImagePixelType InputImagePixelType; + typedef MRFEnergyPottsType::LabelledImagePixelType LabelledImagePixelType; + + MRFEnergyPottsType::Pointer object = MRFEnergyPottsType::New(); + + InputImagePixelType inPix = 10; + LabelledImagePixelType inLab = 5; + + std::ofstream file; + file.open(outputFile); + + file<<"GetValue(const InputImagePixelType = 10, const LabelledImagePixelType = 5): "<< object->GetValue(inPix, inLab)<<std::endl; + inPix = 5; + file<<"GetValue(const InputImagePixelType = 5, const LabelledImagePixelType = 5) : "<< object->GetValue(inPix, inLab)<<std::endl; + + file.close(); + + + return EXIT_SUCCESS; +} + diff --git a/Testing/Code/Markov/otbMRFOptimizerICM.cxx b/Testing/Code/Markov/otbMRFOptimizerICM.cxx new file mode 100644 index 0000000000..add252e83c --- /dev/null +++ b/Testing/Code/Markov/otbMRFOptimizerICM.cxx @@ -0,0 +1,38 @@ +/*========================================================================= + + Program: ORFEO Toolbox + Language: C++ + Date: $Date$ + Version: $Revision$ + + + Copyright (c) Centre National d'Etudes Spatiales. All rights reserved. + See OTBCopyright.txt for details. + + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the above copyright notices for more information. + +=========================================================================*/ +#if defined(_MSC_VER) +#pragma warning ( disable : 4786 ) +#endif + +#include "otbMRFOptimizerICM.h" + +int otbMRFOptimizerICM(int argc, char * argv[]) +{ + typedef otb::MRFOptimizerICM MRFOptimizerICMType; + + MRFOptimizerICMType::Pointer object = MRFOptimizerICMType::New(); + + if ( object->operator()(-1) != true || object->operator()(1) != false || object->operator()(0) != false ) + { + return EXIT_FAILURE; + } + + + return EXIT_SUCCESS; +} + diff --git a/Testing/Code/Markov/otbMRFOptimizerICMNew.cxx b/Testing/Code/Markov/otbMRFOptimizerICMNew.cxx new file mode 100644 index 0000000000..267b3fd65b --- /dev/null +++ b/Testing/Code/Markov/otbMRFOptimizerICMNew.cxx @@ -0,0 +1,32 @@ +/*========================================================================= + + Program: ORFEO Toolbox + Language: C++ + Date: $Date$ + Version: $Revision$ + + + Copyright (c) Centre National d'Etudes Spatiales. All rights reserved. + See OTBCopyright.txt for details. + + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the above copyright notices for more information. + +=========================================================================*/ +#if defined(_MSC_VER) +#pragma warning ( disable : 4786 ) +#endif + +#include "otbMRFOptimizerICM.h" + +int otbMRFOptimizerICMNew(int argc, char * argv[]) +{ + typedef otb::MRFOptimizerICM MRFOptimizerType; + + MRFOptimizerType::Pointer object = MRFOptimizerType::New(); + + return EXIT_SUCCESS; +} + diff --git a/Testing/Code/Markov/otbMRFOptimizerMetropolis.cxx b/Testing/Code/Markov/otbMRFOptimizerMetropolis.cxx new file mode 100644 index 0000000000..9c164406f3 --- /dev/null +++ b/Testing/Code/Markov/otbMRFOptimizerMetropolis.cxx @@ -0,0 +1,50 @@ +/*========================================================================= + + Program: ORFEO Toolbox + Language: C++ + Date: $Date$ + Version: $Revision$ + + + Copyright (c) Centre National d'Etudes Spatiales. All rights reserved. + See OTBCopyright.txt for details. + + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the above copyright notices for more information. + +=========================================================================*/ +#if defined(_MSC_VER) +#pragma warning ( disable : 4786 ) +#endif + +#include "otbMRFOptimizerMetropolis.h" +#include <fstream> + +int otbMRFOptimizerMetropolis(int argc, char * argv[]) +{ + const char * outputFile = argv[1]; + + typedef otb::MRFOptimizerMetropolis MRFOptimizerType; + + MRFOptimizerType::Pointer object = MRFOptimizerType::New(); + + object->SetTemperature( 10. ); + // USED TO OVERPASS RANDOM CALCULATION + object->SetValueInsteadRandom(1); + + std::ofstream file; + file.open(outputFile); + + file<<"Compute(0) : "<<object->Compute(0.)<<std::endl; + file<<"Compute(-1.) : "<<object->Compute(-1.)<<std::endl; + file<<"Compute(1.) avec m_ValueInsteadRandom<proba*1000 : "<<object->Compute(1.)<<std::endl; + object->SetValueInsteadRandom( 10000 ); + file<<"Compute(1.) avec m_ValueInsteadRandom>=proba*1000: "<<object->Compute(1.)<<std::endl; + + file.close(); + + return EXIT_SUCCESS; +} + diff --git a/Testing/Code/Markov/otbMRFSamplerMAP.cxx b/Testing/Code/Markov/otbMRFSamplerMAP.cxx new file mode 100644 index 0000000000..f24abf2031 --- /dev/null +++ b/Testing/Code/Markov/otbMRFSamplerMAP.cxx @@ -0,0 +1,88 @@ +/*========================================================================= + + Program: ORFEO Toolbox + Language: C++ + Date: $Date$ + Version: $Revision$ + + + Copyright (c) Centre National d'Etudes Spatiales. All rights reserved. + See OTBCopyright.txt for details. + + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the above copyright notices for more information. + +=========================================================================*/ +#if defined(_MSC_VER) +#pragma warning ( disable : 4786 ) +#endif + +#include "otbMRFSamplerMAP.h" +#include "otbImageFileReader.h" +#include "otbImage.h" +#include <fstream> + + +int otbMRFSamplerMAP(int argc, char * argv[]) +{ + const char * inputImage = argv[1]; + const char * labelImage = argv[2]; + const char * outputFile = argv[3]; + + typedef double PixelTypeInput; + typedef int PixelTypeLabel; + typedef otb::Image<PixelTypeInput, 2> ImageType; + typedef otb::Image<PixelTypeLabel, 2> LabelType; + typedef otb::ImageFileReader< ImageType > ReaderInputType; + typedef otb::ImageFileReader< LabelType > ReaderLabelType; + typedef otb::MRFSamplerMAP< ImageType, LabelType> MRFSamplerMAPType; + typedef MRFSamplerMAPType::LabelledImageNeighborhoodIterator LabelledNeighborhoodIterator; + typedef MRFSamplerMAPType::InputImageNeighborhoodIterator InputNeighborhoodIterator; + + + MRFSamplerMAPType::Pointer object = MRFSamplerMAPType::New(); + ReaderInputType::Pointer readerIn = ReaderInputType::New(); + ReaderLabelType::Pointer readerLab = ReaderLabelType::New(); + + readerIn->SetFileName( inputImage ); + readerLab->SetFileName( labelImage ); + readerIn->Update(); + readerLab->Update(); + + ImageType::IndexType idIn; + LabelType::IndexType idLab; + idIn[0] = 50; + idIn[1] = 50; + idLab[0] = 70; + idLab[1] = 70; + ImageType::PixelType inPix = readerIn->GetOutput()->GetPixel( idIn ); + LabelType::PixelType inLab = readerLab->GetOutput()->GetPixel( idLab ); + + InputNeighborhoodIterator::RadiusType radIn; + LabelledNeighborhoodIterator::RadiusType radLab; + radIn.Fill(3); + radLab.Fill(3); + + InputNeighborhoodIterator iterIn = InputNeighborhoodIterator( radIn, readerIn->GetOutput(), readerIn->GetOutput()->GetLargestPossibleRegion()); + LabelledNeighborhoodIterator iterLab = LabelledNeighborhoodIterator( radLab, readerLab->GetOutput(), readerLab->GetOutput()->GetLargestPossibleRegion()); + + std::ofstream file; + file.open(outputFile); + file<<"Used pixels: (50, 50) -> "<<inPix<<" , (70, 70) -> "<<inLab<<std::endl; + file<<std::endl; + file<<"Compute(const InputNeighborhoodIterator, const LabelledNeighborhoodIterator) "<<object->Compute(iterIn, iterLab)<<std::endl; + + // All values (exept m_Value) are null : SingleValue return 0... + file<<"m_EnergyBefore: "<<object->GetEnergyBefore()<<std::endl; + file<<"m_EnergyAfter : "<<object->GetEnergyAfter()<<std::endl; + file<<"m_Value : "<<object->GetValue()<<std::endl; + file<<"m_DeltaEnergy : "<<object->GetDeltaEnergy()<<std::endl; + + + file.close(); + + return EXIT_SUCCESS; +} + diff --git a/Testing/Code/Markov/otbMRFSamplerMAPNew.cxx b/Testing/Code/Markov/otbMRFSamplerMAPNew.cxx new file mode 100644 index 0000000000..ed5e7a2a03 --- /dev/null +++ b/Testing/Code/Markov/otbMRFSamplerMAPNew.cxx @@ -0,0 +1,38 @@ +/*========================================================================= + + Program: ORFEO Toolbox + Language: C++ + Date: $Date$ + Version: $Revision$ + + + Copyright (c) Centre National d'Etudes Spatiales. All rights reserved. + See OTBCopyright.txt for details. + + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the above copyright notices for more information. + +=========================================================================*/ +#if defined(_MSC_VER) +#pragma warning ( disable : 4786 ) +#endif + +#include "itkExceptionObject.h" + +#include "otbMRFSamplerMAP.h" +#include "otbImage.h" + + +int otbMRFSamplerMAPNew(int argc, char * argv[]) +{ + typedef double PixelType; + typedef otb::Image<PixelType, 2> ImageType; + typedef otb::MRFSamplerMAP< ImageType, ImageType> MRFSamplerType; + + MRFSamplerType::Pointer object = MRFSamplerType::New(); + + return EXIT_SUCCESS; +} + diff --git a/Testing/Code/Markov/otbMRFSamplerRandom.cxx b/Testing/Code/Markov/otbMRFSamplerRandom.cxx new file mode 100644 index 0000000000..8ac4e198ad --- /dev/null +++ b/Testing/Code/Markov/otbMRFSamplerRandom.cxx @@ -0,0 +1,91 @@ +/*========================================================================= + + Program: ORFEO Toolbox + Language: C++ + Date: $Date$ + Version: $Revision$ + + + Copyright (c) Centre National d'Etudes Spatiales. All rights reserved. + See OTBCopyright.txt for details. + + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the above copyright notices for more information. + +=========================================================================*/ +#if defined(_MSC_VER) +#pragma warning ( disable : 4786 ) +#endif + +#include "otbMRFSamplerRandom.h" +#include "otbImageFileReader.h" +#include "otbImage.h" +#include <fstream> + + +int otbMRFSamplerRandom(int argc, char * argv[]) +{ + const char * inputImage = argv[1]; + const char * labelImage = argv[2]; + const char * outputFile = argv[3]; + + typedef double PixelTypeInput; + typedef int PixelTypeLabel; + typedef otb::Image<PixelTypeInput, 2> ImageType; + typedef otb::Image<PixelTypeLabel, 2> LabelType; + typedef otb::ImageFileReader< ImageType > ReaderInputType; + typedef otb::ImageFileReader< LabelType > ReaderLabelType; + typedef otb::MRFSamplerRandom< ImageType, LabelType> MRFSamplerRandomType; + typedef MRFSamplerRandomType::LabelledImageNeighborhoodIterator LabelledNeighborhoodIterator; + typedef MRFSamplerRandomType::InputImageNeighborhoodIterator InputNeighborhoodIterator; + + + MRFSamplerRandomType::Pointer object = MRFSamplerRandomType::New(); + ReaderInputType::Pointer readerIn = ReaderInputType::New(); + ReaderLabelType::Pointer readerLab = ReaderLabelType::New(); + + readerIn->SetFileName( inputImage ); + readerLab->SetFileName( labelImage ); + readerIn->Update(); + readerLab->Update(); + + // USED TO OVERPASS RANDOM CALCULATION + object->SetValueInsteadRandom(300); + + ImageType::IndexType idIn; + LabelType::IndexType idLab; + idIn[0] = 50; + idIn[1] = 50; + idLab[0] = 70; + idLab[1] = 70; + ImageType::PixelType inPix = readerIn->GetOutput()->GetPixel( idIn ); + LabelType::PixelType inLab = readerLab->GetOutput()->GetPixel( idLab ); + + InputNeighborhoodIterator::RadiusType radIn; + LabelledNeighborhoodIterator::RadiusType radLab; + radIn.Fill(3); + radLab.Fill(3); + + InputNeighborhoodIterator iterIn = InputNeighborhoodIterator( radIn, readerIn->GetOutput(), readerIn->GetOutput()->GetLargestPossibleRegion()); + LabelledNeighborhoodIterator iterLab = LabelledNeighborhoodIterator( radLab, readerLab->GetOutput(), readerLab->GetOutput()->GetLargestPossibleRegion()); + + std::ofstream file; + file.open(outputFile); + file<<"Used pixels: (50, 50) -> "<<inPix<<" , (70, 70) -> "<<inLab<<std::endl; + file<<std::endl; + file<<"Compute(const InputNeighborhoodIterator, const LabelledNeighborhoodIterator) "<<object->Compute(iterIn, iterLab)<<std::endl; + + // All values (exept m_Value) are null : SingleValue return 0... + file<<"m_EnergyBefore: "<<object->GetEnergyBefore()<<std::endl; + file<<"m_EnergyAfter : "<<object->GetEnergyAfter()<<std::endl; + file<<"m_Value : "<<object->GetValue()<<std::endl; + file<<"m_DeltaEnergy : "<<object->GetDeltaEnergy()<<std::endl; + + + file.close(); + + return EXIT_SUCCESS; +} + diff --git a/Testing/Code/Markov/otbMRFSamplerRandomMAP.cxx b/Testing/Code/Markov/otbMRFSamplerRandomMAP.cxx new file mode 100644 index 0000000000..a1c62aca7d --- /dev/null +++ b/Testing/Code/Markov/otbMRFSamplerRandomMAP.cxx @@ -0,0 +1,91 @@ +/*========================================================================= + + Program: ORFEO Toolbox + Language: C++ + Date: $Date$ + Version: $Revision$ + + + Copyright (c) Centre National d'Etudes Spatiales. All rights reserved. + See OTBCopyright.txt for details. + + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the above copyright notices for more information. + +=========================================================================*/ +#if defined(_MSC_VER) +#pragma warning ( disable : 4786 ) +#endif + +#include "otbMRFSamplerRandomMAP.h" +#include "otbImageFileReader.h" +#include "otbImage.h" +#include <fstream> + + +int otbMRFSamplerRandomMAP(int argc, char * argv[]) +{ + const char * inputImage = argv[1]; + const char * labelImage = argv[2]; + const char * outputFile = argv[3]; + + typedef double PixelTypeInput; + typedef int PixelTypeLabel; + typedef otb::Image<PixelTypeInput, 2> ImageType; + typedef otb::Image<PixelTypeLabel, 2> LabelType; + typedef otb::ImageFileReader< ImageType > ReaderInputType; + typedef otb::ImageFileReader< LabelType > ReaderLabelType; + typedef otb::MRFSamplerRandomMAP< ImageType, LabelType> MRFSamplerRandomMAPType; + typedef MRFSamplerRandomMAPType::LabelledImageNeighborhoodIterator LabelledNeighborhoodIterator; + typedef MRFSamplerRandomMAPType::InputImageNeighborhoodIterator InputNeighborhoodIterator; + + + MRFSamplerRandomMAPType::Pointer object = MRFSamplerRandomMAPType::New(); + ReaderInputType::Pointer readerIn = ReaderInputType::New(); + ReaderLabelType::Pointer readerLab = ReaderLabelType::New(); + + readerIn->SetFileName( inputImage ); + readerLab->SetFileName( labelImage ); + readerIn->Update(); + readerLab->Update(); + + // USED TO OVERPASS RANDOM CALCULATION + object->SetValueInsteadRandom(300); + + ImageType::IndexType idIn; + LabelType::IndexType idLab; + idIn[0] = 50; + idIn[1] = 50; + idLab[0] = 70; + idLab[1] = 70; + ImageType::PixelType inPix = readerIn->GetOutput()->GetPixel( idIn ); + LabelType::PixelType inLab = readerLab->GetOutput()->GetPixel( idLab ); + + InputNeighborhoodIterator::RadiusType radIn; + LabelledNeighborhoodIterator::RadiusType radLab; + radIn.Fill(3); + radLab.Fill(3); + + InputNeighborhoodIterator iterIn = InputNeighborhoodIterator( radIn, readerIn->GetOutput(), readerIn->GetOutput()->GetLargestPossibleRegion()); + LabelledNeighborhoodIterator iterLab = LabelledNeighborhoodIterator( radLab, readerLab->GetOutput(), readerLab->GetOutput()->GetLargestPossibleRegion()); + + std::ofstream file; + file.open(outputFile); + file<<"Used pixels: (50, 50) -> "<<inPix<<" , (70, 70) -> "<<inLab<<std::endl; + file<<std::endl; + file<<"Compute( Compute( const InputImageNeighborhoodIterator, const LabelledImageNeighborhoodIterator) "<<object->Compute(iterIn, iterLab)<<std::endl; + + // All values (exept m_Value) are null : SingleValue return 0... + file<<"m_EnergyBefore: "<<object->GetEnergyBefore()<<std::endl; + file<<"m_EnergyAfter: "<<object->GetEnergyAfter()<<std::endl; + file<<"m_Value: "<<object->GetValue()<<std::endl; + file<<"m_DeltaEnergy: "<<object->GetDeltaEnergy()<<std::endl; + + + file.close(); + + return EXIT_SUCCESS; +} + diff --git a/Testing/Code/Markov/otbMRFSamplerRandomMAPNew.cxx b/Testing/Code/Markov/otbMRFSamplerRandomMAPNew.cxx new file mode 100644 index 0000000000..4bac023ce6 --- /dev/null +++ b/Testing/Code/Markov/otbMRFSamplerRandomMAPNew.cxx @@ -0,0 +1,39 @@ +/*========================================================================= + + Program: ORFEO Toolbox + Language: C++ + Date: $Date$ + Version: $Revision$ + + + Copyright (c) Centre National d'Etudes Spatiales. All rights reserved. + See OTBCopyright.txt for details. + + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the above copyright notices for more information. + +=========================================================================*/ +#if defined(_MSC_VER) +#pragma warning ( disable : 4786 ) +#endif + +#include "otbMRFSamplerRandomMAP.h" +#include "otbImage.h" + + +int otbMRFSamplerRandomMAPNew(int argc, char * argv[]) +{ + typedef double PixelType; + typedef otb::Image<PixelType, 2> ImageType; + typedef otb::MRFSamplerRandomMAP< ImageType, ImageType> MRFSamplerRandomType; + + MRFSamplerRandomType::Pointer object = MRFSamplerRandomType::New(); + + return EXIT_SUCCESS; +} + + + + diff --git a/Testing/Code/Markov/otbMRFSamplerRandomNew.cxx b/Testing/Code/Markov/otbMRFSamplerRandomNew.cxx index 2b473471e3..7e4be91964 100644 --- a/Testing/Code/Markov/otbMRFSamplerRandomNew.cxx +++ b/Testing/Code/Markov/otbMRFSamplerRandomNew.cxx @@ -26,7 +26,7 @@ int otbMRFSamplerRandomNew(int argc, char * argv[]) { typedef double PixelType; - typedef otb::Image<PixelType, 2> ImageType; + typedef otb::Image<PixelType, 2> ImageType; typedef otb::MRFSamplerRandom< ImageType, ImageType> MRFSamplerRandomType; MRFSamplerRandomType::Pointer object = MRFSamplerRandomType::New(); diff --git a/Testing/Code/Markov/otbMarkovClassificationFilter.cxx b/Testing/Code/Markov/otbMarkovClassificationFilter.cxx new file mode 100644 index 0000000000..4f360d97d7 --- /dev/null +++ b/Testing/Code/Markov/otbMarkovClassificationFilter.cxx @@ -0,0 +1,129 @@ + +/*========================================================================= + + Program: ORFEO Toolbox + Language: C++ + Date: $Date$ + Version: $Revision$ + + + Copyright (c) Centre National d'Etudes Spatiales. All rights reserved. + See OTBCopyright.txt for details. + + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the above copyright notices for more information. + +=========================================================================*/ +#if defined(_MSC_VER) +#pragma warning ( disable : 4786 ) +#endif + +#ifdef __BORLANDC__ +#define ITK_LEAN_AND_MEAN +#endif + +#include "otbImageFileReader.h" +#include "otbImageFileWriter.h" +#include "otbImage.h" +#include "otbMarkovClassificationFilter.h" +#include "itkRescaleIntensityImageFilter.h" +#include "otbMRFEnergy.h" +#include "otbMRFEnergyPotts.h" +#include "otbMRFEnergyGaussianClassification.h" +#include "otbMRFOptimizerMetropolis.h" +#include "otbMRFSamplerRandom.h" + +int otbMarkovClassificationFilter( int argc, char* argv[] ) +{ + //std::cerr << " inputImage inputInitialization output lambda iterations temperature" << std::endl; + + + const unsigned int Dimension = 2; + + typedef double InternalPixelType; + typedef unsigned char LabelledPixelType; + typedef otb::Image<InternalPixelType, Dimension> InputImageType; + typedef otb::Image<LabelledPixelType, Dimension> LabelledImageType; + typedef otb::ImageFileReader< InputImageType > ReaderType; + typedef otb::ImageFileReader< LabelledImageType > ReaderLabelledType; + typedef otb::ImageFileWriter< LabelledImageType > WriterType; + + ReaderType::Pointer reader = ReaderType::New(); + ReaderLabelledType::Pointer reader2 = ReaderLabelledType::New(); + WriterType::Pointer writer = WriterType::New(); + + const char * inputFilename = argv[1]; + const char * labelledFilename = argv[2]; + const char * outputFilename = argv[3]; + + reader->SetFileName( inputFilename ); + reader2->SetFileName( labelledFilename ); + writer->SetFileName( outputFilename ); + + typedef otb::MarkovClassificationFilter<InputImageType, + LabelledImageType> MarkovClassificationFilterType; + typedef otb::MRFSamplerRandom< InputImageType, LabelledImageType> SamplerType; + typedef otb::MRFOptimizerMetropolis OptimizerType; + typedef otb::MRFEnergyPotts<LabelledImageType, LabelledImageType> EnergyRegularizationType; + typedef otb::MRFEnergyGaussianClassification<InputImageType, + LabelledImageType> EnergyFidelityType; + + MarkovClassificationFilterType::Pointer markovFilter = MarkovClassificationFilterType::New(); + EnergyRegularizationType::Pointer energyRegularization = EnergyRegularizationType::New(); + EnergyFidelityType::Pointer energyFidelity = EnergyFidelityType::New(); + OptimizerType::Pointer optimizer = OptimizerType::New(); + SamplerType::Pointer sampler = SamplerType::New(); + + + unsigned int nClass = 4; + itk::VariableLengthVector<double> mean; + itk::VariableLengthVector<double> stdDev; + mean.SetSize(nClass); + stdDev.SetSize(nClass); + mean[0]=10;mean[1]=80;mean[2]=150;mean[3]=220; + stdDev[0]=10;stdDev[1]=10;stdDev[2]=10;stdDev[3]=10; + + energyFidelity->SetNumberOfParameters(2*nClass); + EnergyFidelityType::ParametersType parameters; + parameters.SetSize(energyFidelity->GetNumberOfParameters()); + parameters[0]=10.0; //Class 0 mean + parameters[1]=10.0; //Class 0 stdev + parameters[2]=80.0;//Class 1 mean + parameters[3]=10.0; //Class 1 stdev + parameters[4]=150.0; //Class 2 mean + parameters[5]=10.0; //Class 2 stdev + 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); + markovFilter->SetLambda(atof(argv[4])); + markovFilter->SetNeighborhoodRadius(1); + + markovFilter->SetEnergyRegularization(energyRegularization); + markovFilter->SetEnergyFidelity(energyFidelity); + markovFilter->SetOptimizer(optimizer); + markovFilter->SetSampler(sampler); + + markovFilter->SetTrainingInput(reader2->GetOutput()); + markovFilter->SetInput(reader->GetOutput()); + + typedef itk::RescaleIntensityImageFilter< LabelledImageType, LabelledImageType > RescaleType; + RescaleType::Pointer rescaleFilter = RescaleType::New(); + rescaleFilter->SetOutputMinimum(0); + rescaleFilter->SetOutputMaximum(255); + + rescaleFilter->SetInput( markovFilter->GetOutput() ); + + writer->SetInput( rescaleFilter->GetOutput() ); + writer->Update(); + + + return 0; + +} + diff --git a/Testing/Code/Markov/otbMarkovTests1.cxx b/Testing/Code/Markov/otbMarkovTests1.cxx index 3cab5266ef..ee0dc9b1f1 100755 --- a/Testing/Code/Markov/otbMarkovTests1.cxx +++ b/Testing/Code/Markov/otbMarkovTests1.cxx @@ -29,11 +29,15 @@ void RegisterTests() { REGISTER_TEST(otbMarkovClassificationFilterNew); REGISTER_TEST(otbMRFEnergyNew); +REGISTER_TEST(otbMRFEnergy); +REGISTER_TEST(otbMRFEnergyEdgeFidelityNew); +REGISTER_TEST(otbMRFEnergyEdgeFidelity); REGISTER_TEST(otbMRFEnergyPottsNew); +REGISTER_TEST(otbMRFEnergyPotts); REGISTER_TEST(otbMRFEnergyGaussianClassificationNew); +REGISTER_TEST(otbMRFEnergyGaussianClassification); REGISTER_TEST(otbMRFOptimizerNew); -REGISTER_TEST(otbMRFOptimizerMetropolisNew); -REGISTER_TEST(otbMRFSamplerNew); -REGISTER_TEST(otbMRFSamplerRandomNew); +REGISTER_TEST(otbMRFOptimizerICMNew); +REGISTER_TEST(otbMRFOptimizerICM); } diff --git a/Testing/Code/Markov/otbMarkovTests2.cxx b/Testing/Code/Markov/otbMarkovTests2.cxx new file mode 100644 index 0000000000..2488ca7a57 --- /dev/null +++ b/Testing/Code/Markov/otbMarkovTests2.cxx @@ -0,0 +1,39 @@ +/*========================================================================= + + Program: ORFEO Toolbox + Language: C++ + Date: $Date$ + Version: $Revision$ + + + Copyright (c) Centre National d'Etudes Spatiales. All rights reserved. + See OTBCopyright.txt for details. + + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the above copyright notices for more information. + +=========================================================================*/ + +// this file defines the otbCommonTest for the test driver +// and all it expects is that you have a function called RegisterTests +#if defined(_MSC_VER) +#pragma warning ( disable : 4786 ) +#endif + +#include <iostream> +#include "otbTestMain.h" + +void RegisterTests() +{ +REGISTER_TEST(otbMRFOptimizerMetropolisNew); +REGISTER_TEST(otbMRFOptimizerMetropolis); +REGISTER_TEST(otbMRFSamplerNew); +REGISTER_TEST(otbMRFSamplerMAPNew); +REGISTER_TEST(otbMRFSamplerMAP); +REGISTER_TEST(otbMRFSamplerRandomNew); +REGISTER_TEST(otbMRFSamplerRandom); +REGISTER_TEST(otbMRFSamplerRandomMAPNew); +REGISTER_TEST(otbMRFSamplerRandomMAP); +} -- GitLab