Commit cd3a8524 authored by Manuel Grizonnet's avatar Manuel Grizonnet

ENH:add MRFEnergyFisherClassification (contribution from Jan Wegner)

parent 58db7db5
/*=========================================================================
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 __otbMRFEnergyFisherClassification_h
#define __otbMRFEnergyFisherClassification_h
#include "otbMRFEnergy.h"
#include "otbMath.h"
#include "otbGamma.h"
namespace otb
{
/*
\* class MRFEnergyFisherClassification
\* This is the implementation of the Fisher model for Markov classification
\* to be used for the fidelity (likelihood) term for the classification.
\*
\* This class is meant to be used in the MRF framework with the otb:: MarkovRandomFieldFilter
*/
template< class TInput1, class TInput2>
class ITK_EXPORT MRFEnergyFisherClassification:public MRFEnergy< TInput1, TInput2>
{
public:
typedef MRFEnergyFisherClassification Self;
typedef MRFEnergy< TInput1, TInput2> Superclass;
typedef itk::SmartPointer<Self> Pointer;
typedef itk::SmartPointer<const Self> ConstPointer;
typedef TInput1 InputImageType;
typedef TInput2 LabelledImageType;
typedef typename InputImageType::PixelType InputImagePixelType;
typedef typename LabelledImageType::PixelType LabelledImagePixelType;
typedef itk::Array < double > ParametersType;
itkNewMacro(Self);
itkTypeMacro(MRFEnergyFisherClassification, MRFEnergy);
Gamma g;
void SetNumberOfParameters(const unsigned int nParameters)
{
Superclass::SetNumberOfParameters(nParameters);
this->m_Parameters.SetSize(nParameters);
this->Modified();
}
double GetSingleValue(const InputImagePixelType & value1, const LabelledImagePixelType & value2)
{
if ((unsigned int)value2 >= this->GetNumberOfParameters()/3)
{
itkExceptionMacro(<<"Number of parameters does not correspond to number of classes" );
}
double val1 = static_cast<double>(value1);
double mu = this->m_Parameters[3*static_cast<double>(value2)] ;
double l = this->m_Parameters[3*static_cast<double>(value2)+1] ;
double m = this->m_Parameters[3*static_cast<double>(value2)+2] ;
double result = -vcl_log((g.gamma(l+m)/(g.gamma(l)*g.gamma(m))) * (2/(mu)) * (vcl_sqrt(l/m)) *
((pow((vcl_sqrt(l/m)*(val1/mu)),((2*l)-1))) / (pow(1+(vcl_sqrt(l/m)*(val1/mu)*vcl_sqrt(l/m)*(val1/mu)),(l+m)))));
return static_cast<double>( result );
}
protected:
// The constructor and destructor.
MRFEnergyFisherClassification() {};
virtual ~MRFEnergyFisherClassification() {};
};
}
#endif
......@@ -12,6 +12,7 @@ SET(EPSILON 0.000000000001)
SET(EPSILON_10e6 0.000001)
SET(MARKOV_TESTS1 ${CXX_TEST_PATH}/otbMarkovTests1)
SET(MARKOV_TESTS2 ${CXX_TEST_PATH}/otbMarkovTests2)
SET(MARKOV_TESTS3 ${CXX_TEST_PATH}/otbMarkovTests3)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ otbMARKOV_TESTS1 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
......@@ -147,6 +148,19 @@ ADD_TEST(maTvMRFSamplerRandomMAP ${MARKOV_TESTS2}
${TEMP}/maTvMRFSamplerRandomMAP.txt
)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ otbMARKOV_TESTS3 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ------- otb::MRFEnergyFisherClassification ------------------------------
ADD_TEST(maTuMRFEnergyFisherClassificationNew ${MARKOV_TESTS3}
otbMRFEnergyFisherClassificationNew )
ADD_TEST(maTvMRFEnergyFisherClassification ${MARKOV_TESTS3}
--compare-ascii ${TOL} ${BASELINE_FILES}/maTvMRFEnergyFisherClassification.txt
${TEMP}/maTvMRFEnergyFisherClassification.txt
otbMRFEnergyFisherClassification
${TEMP}/maTvMRFEnergyFisherClassification.txt
)
# A enrichir
SET(Markov_SRCS1
......@@ -177,7 +191,13 @@ otbMRFSamplerRandomMAPNew.cxx
otbMRFSamplerRandomMAP.cxx
)
SET(Markov_SRCS3
otbMarkovTests3.cxx
otbMRFEnergyFisherClassification.cxx
)
OTB_ADD_EXECUTABLE(otbMarkovTests1 "${Markov_SRCS1}" "OTBMarkov;OTBIO;OTBTesting")
OTB_ADD_EXECUTABLE(otbMarkovTests2 "${Markov_SRCS2}" "OTBMarkov;OTBIO;OTBTesting")
OTB_ADD_EXECUTABLE(otbMarkovTests3 "${Markov_SRCS3}" "OTBMarkov;OTBIO;OTBTesting")
ENDIF( NOT OTB_DISABLE_CXX_TESTING AND BUILD_TESTING )
/*=========================================================================
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 "otbMRFEnergyFisherClassification.h"
#include "otbImage.h"
#include <fstream>
int otbMRFEnergyFisherClassificationNew(int argc, char * argv[])
{
typedef double PixelTypeInput;
typedef int PixelTypeLabel;
typedef otb::Image<PixelTypeInput, 2> ImageType;
typedef otb::Image<PixelTypeLabel, 2> LabelType;
typedef otb::MRFEnergyFisherClassification<ImageType, LabelType> MRFFisherType;
MRFFisherType::Pointer classif = MRFFisherType::New();
std::cout << classif << std::endl;
return EXIT_SUCCESS;
}
int otbMRFEnergyFisherClassification(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::MRFEnergyFisherClassification<ImageType, LabelType> MRFFisherType;
MRFFisherType::Pointer classif = MRFFisherType::New();
classif->SetNumberOfParameters(6);
MRFFisherType::ParametersType param(6);
param[0] = 0;
param[1] = 1;
param[2] = 2;
param[3] = 3;
param[4] = 4;
param[5] = 5;
classif->SetParameters(param);
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;
}
......@@ -49,7 +49,7 @@ int otbMRFEnergyGaussianClassification(int argc, char * argv[])
file.open(outputFile);
file << std::endl;
file << "Number of paramters: " << classif->GetNumberOfParameters() << std::endl;
file << "Number of parameters: " << classif->GetNumberOfParameters() << std::endl;
file << "GetSingleValue(10, 1): " << classif->GetSingleValue(10, 2) << std::endl;
file.close();
......
/*=========================================================================
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 "otbTestMain.h"
void RegisterTests()
{
REGISTER_TEST(otbMRFEnergyFisherClassificationNew);
REGISTER_TEST(otbMRFEnergyFisherClassification);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment