Commit 053c00d1 authored by Julien Malik's avatar Julien Malik
Browse files

COMP: remove MeanShiftKernel file, duplicates of SVMKernel file, and not used anywhere

parent 96b1fb92
/*=========================================================================
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 __otbMeanShiftKernels_h
#define __otbMeanShiftKernels_h
#include <vector>
#include <algorithm> // for std::find
#include <cstring> // for strpbrk
#include "itkNumericTraits.h"
#include "otbMath.h"
// Existing kernels
#include "otbSpectralAngleKernelFunctor.h"
#include "otbChangeProfileKernelFunctor.h"
#include "otbNonGaussianRBFKernelFunctor.h"
#include "otbMixturePolyRBFKernelFunctor.h"
/*Balls itk
*
*
*/
/* SVM kernels */
/*
//
* */
namespace otb
{
/** \class CustomKernelFunctor
* \brief Undocumented
*/
class CustomKernelFunctor : public GenericKernelFunctorBase
{
public:
typedef CustomKernelFunctor Self;
typedef GenericKernelFunctorBase Superclass;
CustomKernelFunctor() : GenericKernelFunctorBase()
{
this->SetName("custom");
}
virtual ~CustomKernelFunctor() {}
// Deep copy operator
virtual GenericKernelFunctorBase* Clone() const
{
return new Self(*this);
}
virtual double operator ()(const svm_node *x, const svm_node *y, const svm_parameter& /*param*/) const
{
return (dot(x, x) - 2.0 * dot(x, y) + dot(y, y));
}
protected:
CustomKernelFunctor(const Self& copy)
: Superclass(copy)
{
*this = copy;
}
CustomKernelFunctor& operator=(const Self& copy)
{
Superclass::operator =(copy);
return *this;
}
};
/** \class InvMultiQuadricKernelFunctor
* \brief Undocumented
*/
class InvMultiQuadricKernelFunctor : public GenericKernelFunctorBase
{
public:
typedef InvMultiQuadricKernelFunctor Self;
typedef GenericKernelFunctorBase Superclass;
InvMultiQuadricKernelFunctor() : GenericKernelFunctorBase()
{
this->SetName("invMultiQuadric");
this->SetValue<double>("const_coef", 1);
}
virtual ~InvMultiQuadricKernelFunctor() {}
// Deep copy operator
virtual GenericKernelFunctorBase* Clone() const
{
return new Self(*this);
}
virtual double operator ()(const svm_node *x, const svm_node *y, const svm_parameter& param) const
{
double mq = this->GetValue<double>("const_coef") + m_Custom(x, y, param);
if (mq == 0.0)
{
return itk::NumericTraits<double>::max();
}
return 1.0 / sqrt(mq);
}
protected:
InvMultiQuadricKernelFunctor(const Self& copy)
: Superclass(copy)
{
*this = copy;
}
InvMultiQuadricKernelFunctor& operator=(const Self& copy)
{
Superclass::operator =(copy);
return *this;
}
private:
CustomKernelFunctor m_Custom;
};
/** \class KModKernelFunctor
* \brief Undocumented
*/
class KModKernelFunctor : public GenericKernelFunctorBase
{
public:
typedef KModKernelFunctor Self;
typedef GenericKernelFunctorBase Superclass;
KModKernelFunctor() : GenericKernelFunctorBase()
{
this->SetName("KMod");
this->SetValue<double>("const_coef", 1);
}
virtual ~KModKernelFunctor() {}
virtual double operator ()(const svm_node *x, const svm_node *y, const svm_parameter& param) const
{
double mq = this->GetValue<double>("const_coef") + m_Custom(x, y, param);
if (mq == 0.0)
{
return itk::NumericTraits<double>::max();
}
return exp(param.gamma / mq) - 1.0;
}
// Deep copy operator
virtual GenericKernelFunctorBase* Clone() const
{
return new Self(*this);
}
protected:
KModKernelFunctor(const Self& copy)
: Superclass(copy)
{
*this = copy;
}
KModKernelFunctor& operator=(const Self& copy)
{
Superclass::operator =(copy);
return *this;
}
private:
CustomKernelFunctor m_Custom;
};
/** \class SAMKernelFunctor
* \brief Undocumented
*/
class SAMKernelFunctor : public GenericKernelFunctorBase
{
public:
typedef SAMKernelFunctor Self;
typedef GenericKernelFunctorBase Superclass;
SAMKernelFunctor() : GenericKernelFunctorBase()
{
this->SetName("SAM");
}
virtual ~SAMKernelFunctor() {}
virtual double operator ()(const svm_node *x, const svm_node *y, const svm_parameter& /*param*/) const
{
double den = dot(x, x) * dot(y, y);
if (den <= 0.)
{
return 0.0;
}
double ss = dot(x, y);
return vcl_acos(ss / vcl_sqrt(den));
}
// Deep copy operator
virtual GenericKernelFunctorBase* Clone() const
{
return new Self(*this);
}
protected:
SAMKernelFunctor(const Self& copy)
: Superclass(copy)
{
*this = copy;
}
SAMKernelFunctor& operator=(const Self& copy)
{
Superclass::operator =(copy);
return *this;
}
};
/** \class RadialSAMKernelFunctor
* \brief Undocumented
*/
class RadialSAMKernelFunctor : public GenericKernelFunctorBase
{
public:
typedef RadialSAMKernelFunctor Self;
typedef GenericKernelFunctorBase Superclass;
RadialSAMKernelFunctor() : GenericKernelFunctorBase()
{
this->SetName("RadialSAM");
}
virtual ~RadialSAMKernelFunctor() {}
virtual double operator ()(const svm_node *x, const svm_node *y, const svm_parameter& param) const
{
return vcl_exp(-param.gamma * m_Sam(x, y, param));
}
// Deep copy operator
virtual GenericKernelFunctorBase* Clone() const
{
return new Self(*this);
}
protected:
RadialSAMKernelFunctor(const Self& copy)
: Superclass(copy)
{
*this = copy;
}
RadialSAMKernelFunctor& operator=(const Self& copy)
{
Superclass::operator =(copy);
return *this;
}
private:
SAMKernelFunctor m_Sam;
};
/** \class InverseCosSAMKernelFunctor
* \brief Undocumented
*/
class InverseCosSAMKernelFunctor : public GenericKernelFunctorBase
{
public:
typedef InverseCosSAMKernelFunctor Self;
typedef GenericKernelFunctorBase Superclass;
InverseCosSAMKernelFunctor() : GenericKernelFunctorBase()
{
this->SetName("InverseCosSAM");
}
virtual ~InverseCosSAMKernelFunctor() {}
virtual double operator ()(const svm_node *x, const svm_node *y, const svm_parameter& param) const
{
return 1.0 - vcl_cos(m_Sam(x, y, param));
}
// Deep copy operator
virtual GenericKernelFunctorBase* Clone() const
{
return new Self(*this);
}
protected:
InverseCosSAMKernelFunctor(const Self& copy)
: Superclass(copy)
{
*this = copy;
}
InverseCosSAMKernelFunctor& operator=(const Self& copy)
{
Superclass::operator =(copy);
return *this;
}
private:
SAMKernelFunctor m_Sam;
};
/** \class InvMultiQuadraticSAMKernelFunctor
* \brief Undocumented
*/
class InvMultiQuadraticSAMKernelFunctor : public GenericKernelFunctorBase
{
public:
typedef InvMultiQuadraticSAMKernelFunctor Self;
typedef GenericKernelFunctorBase Superclass;
InvMultiQuadraticSAMKernelFunctor() : GenericKernelFunctorBase()
{
this->SetName("invMultiQuadraticSAM");
this->SetValue<double>("const_coef", 1);
}
virtual ~InvMultiQuadraticSAMKernelFunctor() {}
// Deep copy operator
virtual GenericKernelFunctorBase* Clone() const
{
return new Self(*this);
}
virtual double operator ()(const svm_node *x, const svm_node *y, const svm_parameter& param) const
{
double mq = this->GetValue<double>("const_coef") + m_Sam(x, y, param);
if (mq == 0.)
{
return itk::NumericTraits<double>::max();
}
return 1. / sqrt(mq);
}
protected:
InvMultiQuadraticSAMKernelFunctor(const Self& copy)
: Superclass(copy)
{
*this = copy;
}
InvMultiQuadraticSAMKernelFunctor& operator=(const Self& copy)
{
Superclass::operator =(copy);
return *this;
}
private:
SAMKernelFunctor m_Sam;
};
/** \class KModSAMKernelFunctor
* \brief Undocumented
*/
class KModSAMKernelFunctor : public GenericKernelFunctorBase
{
public:
typedef KModSAMKernelFunctor Self;
typedef GenericKernelFunctorBase Superclass;
KModSAMKernelFunctor() : GenericKernelFunctorBase()
{
this->SetName("KModSAM");
this->SetValue<double>("const_coef", 1);
}
virtual ~KModSAMKernelFunctor() {}
// Deep copy operator
virtual GenericKernelFunctorBase* Clone() const
{
return new Self(*this);
}
virtual double operator ()(const svm_node *x, const svm_node *y, const svm_parameter& param) const
{
double mq = this->GetValue<double>("const_coef") + m_Sam(x, y, param);
if (mq == 0.)
{
return itk::NumericTraits<double>::max();
}
return vcl_exp(param.gamma / mq) - 1.0;
}
protected:
KModSAMKernelFunctor(const Self& copy)
: Superclass(copy)
{
*this = copy;
}
KModSAMKernelFunctor& operator=(const Self& copy)
{
Superclass::operator =(copy);
return *this;
}
private:
SAMKernelFunctor m_Sam;
};
/** \class RBFKernelFunctor
* \brief Undocumented
*/
class RBFKernelFunctor : public GenericKernelFunctorBase
{
public:
typedef RBFKernelFunctor Self;
typedef GenericKernelFunctorBase Superclass;
RBFKernelFunctor() : GenericKernelFunctorBase()
{
this->SetName("RBF");
this->SetValue<double>("gamma_coef", 0.5);
}
virtual ~RBFKernelFunctor() {}
// Deep copy operator
virtual GenericKernelFunctorBase* Clone() const
{
return new Self(*this);
}
virtual double operator ()(const svm_node *x, const svm_node *y, const svm_parameter& param) const
{
double res = this->GetValue<double>("gamma_coef") * m_Custom(x, y, param);
return vcl_exp(-res);
}
virtual double derivative(const svm_node *x,
const svm_node *y,
const svm_parameter& param,
int degree,
int index,
bool isAtEnd,
double constValue) const
{
double gamma = this->GetValue<double>("gamma_coef");
double kernelValue = 0.;
double xval = 0.;
double yval = 0.;
int compt = 0;
const svm_node *xtemp = x;
const svm_node *ytemp = y;
bool stop = false;
while (xtemp->index != -1 && ytemp->index != -1 && stop == false)
{
if (xtemp->index == ytemp->index)
{
if (compt == index)
{
xval = xtemp->value;
yval = ytemp->value;
stop = true;
}
else
{
compt++;
++ytemp;
++xtemp;
}
}
else
{
if (xtemp->index > ytemp->index) ++ytemp;
else ++xtemp;
}
}
if (isAtEnd == true)
{
kernelValue = this->operator ()(x, y, param);
}
else
{
kernelValue = constValue;
}
if (degree < 0)
{
return 0;
}
switch (degree)
{
case 0:
return kernelValue;
break;
case 1:
return (-2 * gamma * (yval - xval) * kernelValue);
break;
default:
return (-2 * gamma *
((degree - 1) *
this->derivative(x, y, param, degree - 2, index, isAtEnd, constValue) +
(yval - xval) * derivative(x, y, param, degree - 1, index, isAtEnd, constValue)));
break;
}
}
protected:
RBFKernelFunctor(const Self& copy)
: Superclass(copy)
{
*this = copy;
}
RBFKernelFunctor& operator=(const Self& copy)
{
Superclass::operator =(copy);
return *this;
}
private:
CustomKernelFunctor m_Custom;
};
/** \class RBFRBFSAMKernelFunctor
* \brief Undocumented
*/
class RBFRBFSAMKernelFunctor : public GenericKernelFunctorBase
{
public:
typedef RBFRBFSAMKernelFunctor Self;
typedef GenericKernelFunctorBase Superclass;
RBFRBFSAMKernelFunctor() : GenericKernelFunctorBase()
{
this->SetName("RBFRBFSAM");
this->SetValue<double>("lin_coef", 1.);
}
virtual ~RBFRBFSAMKernelFunctor() {}
// Deep copy operator
virtual GenericKernelFunctorBase* Clone() const
{
return new Self(*this);
}
virtual double operator ()(const svm_node *x, const svm_node *y, const svm_parameter& param) const
{
return (this->GetValue<double>("lin_coef") * vcl_exp(-param.gamma * m_Custom(x, y, param))
+ (1.0 - this->GetValue<double>("lin_coef")) * m_RadialSam(x, y, param));
}
protected:
RBFRBFSAMKernelFunctor(const Self& copy)
: Superclass(copy)
{
*this = copy;
}
RBFRBFSAMKernelFunctor& operator=(const Self& copy)
{
Superclass::operator =(copy);
return *this;
}
private:
CustomKernelFunctor m_Custom;
RadialSAMKernelFunctor m_RadialSam;
};
/** \class PolyRBFSAMKernelFunctor
* \brief Undocumented
*/
class PolyRBFSAMKernelFunctor : public GenericKernelFunctorBase
{
public:
typedef PolyRBFSAMKernelFunctor Self;
typedef GenericKernelFunctorBase Superclass;
PolyRBFSAMKernelFunctor() : GenericKernelFunctorBase()
{
this->SetName("polyRBFSAM");
this->SetValue<double>("const_coef", 1);
this->SetValue<double>("lin_coef", 1);
}
virtual ~PolyRBFSAMKernelFunctor() {}
// Deep copy operator
virtual GenericKernelFunctorBase* Clone() const
{
return new Self(*this);
}
virtual double operator ()(const svm_node *x, const svm_node *y, const svm_parameter& param) const
{
return this->GetValue<double>("const_lin") * vcl_pow(dot(x, y) + this->GetValue<double>("const_coef"), param.degree)
+ (1.0 - this->GetValue<double>("const_coef"))
* m_RadialSam(x, y, param);
}
protected:
PolyRBFSAMKernelFunctor(const Self& copy)
: Superclass(copy)