/usr/include/OTB-5.8/otbConnectedComponentMuParserFunctor.h is in libotb-dev 5.8.0+dfsg-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 | /*=========================================================================
Program: ORFEO Toolbox
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
See OTBCopyright.txt for details.
Some parts of this code are derived from ITK. See ITKCopyright.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 otbConnectedComponentMuParserFunctor_h
#define otbConnectedComponentMuParserFunctor_h
#include "otbParser.h"
#include "otbMacro.h"
#include <vnl/algo/vnl_lsqr.h>
#include <vnl/vnl_sparse_matrix_linear_system.h>
#include <vnl/vnl_least_squares_function.h>
namespace otb
{
/** \class ConnectedComponentMuParserFunctor
* \brief functor used as input to itk connected component segmentation module.
*
* This functor is based on the mathematical parser library muParser.
* The built in functions and operators list is available at:
* http://muparser.sourceforge.net/mup_features.html#idDef2
*
* image pixels :
* p1bX, p2bX, where X denote band index.
*
* Specific Functor additional input :
* distance : euclidian distance
* spectralAngle : NOT IMPLEMENTED
*
* OTB additional functions:
* ndvi(r, niri)
*
* OTB additional constants:
* e - log2e - log10e - ln2 - ln10 - pi - euler
*
*
* \sa Parser
*
*
* \ingroup OTBCCOBIA
*/
/** \class ConnectedComponentMuParserFunctor
* \brief This functor use MuParser as criteria for itk connected component module
*
* \ingroup OTBCCOBIA
*/
namespace Functor
{
template<class TInput>
class ITK_EXPORT ConnectedComponentMuParserFunctor
{
public:
typedef Parser ParserType;
typedef ConnectedComponentMuParserFunctor Self;
std::string GetNameOfClass()
{
return "ConnectedComponentMuParserFunctor";
}
inline bool operator()(const TInput &p1, const TInput &p2)
{
double value;
if (p1.GetSize() != m_NbOfBands)
{
this->SetNumberOfBands(p1.GetSize());
}
// we fill the buffer
for (unsigned int i = 0; i < m_NbOfBands; ++i)
{
m_AImageP1[i] = static_cast<double> (p1[i]);
m_AImageP2[i] = static_cast<double> (p2[i]);
}
m_Distance = 0.0;
m_IntensityP1 = 0.0;
m_IntensityP2 = 0.0;
for (unsigned int i = 0; i < m_NbOfBands; ++i)
{
m_Distance += (p1[i] - p2[i]) * (p1[i] - p2[i]);
m_IntensityP1 += p1[i];
m_IntensityP2 += p2[i];
}
m_IntensityP1 = m_IntensityP1 / (static_cast<double> (m_NbOfBands));
m_IntensityP2 = m_IntensityP2 / (static_cast<double> (m_NbOfBands));
m_Distance = vcl_sqrt(m_Distance);
//compute spectralAngle
double scalarProd = 0.0;
double normProd = 0.0;
double normProd1 = 0.0;
double normProd2 = 0.0;
for (unsigned int i = 0; i < p1.Size(); ++i)
{
scalarProd += p1[i] * p2[i];
normProd1 += p1[i] * p1[i];
normProd2 += p2[i] * p2[i];
}
normProd = normProd1 * normProd2;
if (normProd == 0.0)
{
m_SpectralAngle = 0.0;
}
else
{
m_SpectralAngle = vcl_acos(scalarProd / vcl_sqrt(normProd));
}
//
value = m_Parser->Eval();
return static_cast<bool> (value);
}
void SetExpression(const std::string expression)
{
m_Expression = expression;
m_Parser->SetExpr(m_Expression);
}
/** Return the expression to be parsed */
std::string GetExpression() const
{
return m_Expression;
}
/** Check the expression */
bool CheckExpression()
{
return m_Parser->CheckExpr();
}
void SetNumberOfBands(unsigned int NbOfBands)
{
m_NbOfBands = NbOfBands;
std::ostringstream varName;
m_AImageP1.resize(NbOfBands, 0.0);
m_AImageP2.resize(NbOfBands, 0.0);
for (unsigned int i = 0; i < NbOfBands; ++i)
{
varName << "p1b" << i + 1;
m_Parser->DefineVar(varName.str(), &(m_AImageP1[i]));
varName.str("");
varName << "p2b" << i + 1;
m_Parser->DefineVar(varName.str(), &(m_AImageP2[i]));
varName.str("");
}
// customized data
//m_NbVar++;
//this->SetDataSize(m_NbVar);
m_Parser->DefineVar("distance", &m_Distance);
m_Parser->DefineVar("spectralAngle", &m_SpectralAngle);
m_Parser->DefineVar("intensity_p1", &m_IntensityP1);
m_Parser->DefineVar("intensity_p2", &m_IntensityP2);
//this->SetVarName(m_NbVar-1,"spectralDistance");
}
const std::map<std::string, Parser::ValueType*>& GetVar() const
{
return this->m_Parser->GetVar();
}
Parser::FunctionMapType GetFunList() const
{
return this->m_Parser->GetFunList();
}
ConnectedComponentMuParserFunctor()
{
m_Parser = ParserType::New();
m_NbOfBands = 0;
}
~ConnectedComponentMuParserFunctor()
{
}
private:
ConnectedComponentMuParserFunctor(const Self &); //purposely not implemented
void operator =(const Self &); //purposely not implemented
std::string m_Expression;
ParserType::Pointer m_Parser;
std::vector<double> m_AImageP1;
std::vector<double> m_AImageP2;
double m_Distance;
double m_IntensityP1;
double m_IntensityP2;
double m_SpectralAngle;
std::vector<std::string> m_VarName;
unsigned int m_NbOfBands;
double m_ParserResult;
};
} // end of Functor namespace
}//end namespace otb
#endif
|