Skip to content
Snippets Groups Projects
Commit a2378fb9 authored by Julien Malik's avatar Julien Malik
Browse files

ENH: add spectral angle in MaskMuParserFunctor

parent d2a06dc0
Branches
Tags
No related merge requests found
......@@ -29,6 +29,8 @@
#include "itkLightObject.h"
#include "itkObjectFactory.h"
#include "otbBinarySpectralAngleFunctor.h"
namespace otb
{
/** \class MaskMuParserFunctor
......@@ -54,104 +56,47 @@ namespace otb
namespace Functor
{
template<class TInput>
template<class TInputPixel>
class ITK_EXPORT MaskMuParserFunctor: public itk::LightObject
{
public:
typedef Parser ParserType;
typedef MaskMuParserFunctor Self;
typedef itk::LightObject Superclass;
typedef itk::SmartPointer<Self> Pointer;
typedef MaskMuParserFunctor Self;
typedef itk::LightObject Superclass;
typedef itk::SmartPointer<Self> Pointer;
typedef itk::SmartPointer<const Self> ConstPointer;
/** Method for creation through the object factory. */
itkNewMacro(Self)
;
itkNewMacro(Self);
/** Run-time type information (and related methods). */
itkTypeMacro(MaskMuParserFunctor, itk::LightObject)
;
inline bool operator()(const TInput &p)
{
double value;
itkTypeMacro(MaskMuParserFunctor, itk::LightObject);
if (p.Size() != m_NbOfBands)
{
this->SetNumberOfBands(p.GetSize());
}
// we fill the buffer
for (unsigned int i = 0; i < m_NbOfBands; i++)
{
m_AImage[i] = static_cast<double> (p[i]);
}
// user defined variables
m_Intensity = 0.0;
for (unsigned int i = 0; i < m_NbOfBands; ++i)
{
m_Intensity += p[i];
}
m_Intensity = m_Intensity / (static_cast<double> (m_NbOfBands));
value = m_Parser->Eval();
typedef TInputPixel PixelType;
typedef Parser ParserType;
return static_cast<bool> (value);
typedef BinarySpectralAngleFunctor<PixelType, PixelType, double> SpectralAngleFunctorType;
}
bool operator()(const PixelType &p);
const std::map<std::string, Parser::ValueType*>& GetVar() const
{
return this->m_Parser->GetVar();
}
const std::map<std::string, Parser::ValueType*>& GetVar() const;
void SetExpression(const std::string& expression)
{
m_Expression = expression;
m_Parser->SetExpr(m_Expression);
}
void SetExpression(const std::string& expression);
/** Return the expression to be parsed */
std::string GetExpression() const
{
return m_Expression;
}
std::string GetExpression() const;
void SetNumberOfBands(unsigned int NbOfBands)
{
void SetNumberOfBands(unsigned int NbOfBands);
m_NbOfBands = NbOfBands;
std::ostringstream varName;
m_AImage.resize(NbOfBands, 0.0);
for (unsigned int i = 0; i < NbOfBands; i++)
{
varName << "b" << i + 1;
m_Parser->DefineVar(varName.str(), &(m_AImage[i]));
varName.str("");
}
// customized data
m_Parser->DefineVar("intensity", &m_Intensity);
}
/** Set the reference pixel used to compute the "spectralangle" parser variable */
void SetSpectralAngleReferencePixel(const PixelType& refPixel);
/** Check the expression */
bool CheckExpression()
{
return m_Parser->CheckExpr();
}
bool CheckExpression();
protected:
MaskMuParserFunctor()
{
m_Parser = ParserType::New();
m_NbOfBands = 0;
}
MaskMuParserFunctor();
virtual ~MaskMuParserFunctor()
{
}
virtual ~MaskMuParserFunctor();
private:
......@@ -168,12 +113,19 @@ private:
//user defined variables
double m_Intensity;
double m_SpectralAngle;
};
} // end of Functor namespace
PixelType m_SpectralAngleReferencePixel;
SpectralAngleFunctorType m_SpectralAngleFunctor;
};
} // end of Functor namespace
}//end namespace otb
#ifndef OTB_MANUAL_INSTANTIATION
#include "otbMaskMuParserFunctor.txx"
#endif
#endif
/*=========================================================================
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 __otbMaskMuParserFunctor_txx
#define __otbMaskMuParserFunctor_txx
#include "otbMaskMuParserFunctor.h"
namespace otb
{
namespace Functor
{
// constructor
template<class TInputPixel>
MaskMuParserFunctor<TInputPixel>::MaskMuParserFunctor()
: m_Parser(ParserType::New()),
m_NbOfBands(0),
m_ParserResult(0),
m_Intensity(0),
m_SpectralAngle(0)
{
}
// Destructor
template<class TInputPixel>
MaskMuParserFunctor<TInputPixel>::~MaskMuParserFunctor()
{
}
template<class TInputPixel>
bool MaskMuParserFunctor<TInputPixel>::operator()(const PixelType &p)
{
double value;
if (p.Size() != m_NbOfBands)
{
this->SetNumberOfBands(p.GetSize());
}
// we fill the buffer
for (unsigned int i = 0; i < m_NbOfBands; i++)
{
m_AImage[i] = static_cast<double> (p[i]);
}
// user defined variables
m_Intensity = 0.0;
for (unsigned int i = 0; i < m_NbOfBands; ++i)
{
m_Intensity += p[i];
}
m_Intensity = m_Intensity / (static_cast<double> (m_NbOfBands));
m_SpectralAngle = m_SpectralAngleFunctor(p, m_SpectralAngleReferencePixel);
value = m_Parser->Eval();
return static_cast<bool> (value);
}
template<class TInputPixel>
const std::map<std::string, Parser::ValueType*>&
MaskMuParserFunctor<TInputPixel>::GetVar() const
{
return this->m_Parser->GetVar();
}
template<class TInputPixel>
void
MaskMuParserFunctor<TInputPixel>::SetExpression(const std::string& expression)
{
m_Expression = expression;
m_Parser->SetExpr(m_Expression);
}
/** Return the expression to be parsed */
template<class TInputPixel>
std::string
MaskMuParserFunctor<TInputPixel>::GetExpression() const
{
return m_Expression;
}
template<class TInputPixel>
void
MaskMuParserFunctor<TInputPixel>::SetNumberOfBands(unsigned int NbOfBands)
{
m_NbOfBands = NbOfBands;
std::ostringstream varName;
m_AImage.resize(NbOfBands, 0.0);
for (unsigned int i = 0; i < NbOfBands; i++)
{
varName << "b" << i + 1;
m_Parser->DefineVar(varName.str(), &(m_AImage[i]));
varName.str("");
}
// customized data
m_Parser->DefineVar("intensity", &m_Intensity);
m_Parser->DefineVar("spectralangle", &m_SpectralAngle);
}
/** Set the reference pixel used to compute the "spectralangle" parser variable */
template<class TInputPixel>
void
MaskMuParserFunctor<TInputPixel>::SetSpectralAngleReferencePixel(const PixelType& refPixel)
{
m_SpectralAngleReferencePixel = refPixel;
}
/** Check the expression */
template<class TInputPixel>
bool
MaskMuParserFunctor<TInputPixel>::CheckExpression()
{
return m_Parser->CheckExpr();
}
} // end namespace Functor
} // end namespace otb
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment