ClassifierMaxLikelihoodStrategy.h
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/rp/ClassifierMaxLikelihoodStrategy.h
22  \brief A maximum likelihood estimation strategy for classification (a.k.a. MaxVer in portuguese).
23  */
24 
25 #ifndef __TERRALIB_RP_INTERNAL_CLASSIFIERMAXLIKELIHOODSTRATEGY_H
26 #define __TERRALIB_RP_INTERNAL_CLASSIFIERMAXLIKELIHOODSTRATEGY_H
27 
28 #include "ClassifierStrategy.h"
30 #include "Matrix.h"
31 #include "Config.h"
32 #include "../classification/MaxLikelihood.h"
33 #include "../classification/Adaptors.h"
34 
35 #include <boost/shared_ptr.hpp>
36 
37 #include <map>
38 #include <vector>
39 
40 namespace te
41 {
42  namespace rp
43  {
44  /*!
45  \class ClassifierMaxLikelihoodStrategy
46 
47  \brief A maximum likelihood estimation strategy for classification (a.k.a. MaxVer in portuguese).
48 
49  \ingroup rp_class
50 
51  \see te::cl::MaxLikelihood class docs
52  */
54  {
55  public:
56 
57  /*!
58  \class Parameters
59 
60  \brief Classifier Parameters
61  */
63  {
64  public:
65 
66  typedef unsigned int ClassIDT; //!< Class ID type definition (zero means invalid ID).
67 
68  typedef std::vector< double > ClassSampleT; //!< Class sample type definition.
69 
70  typedef std::vector< ClassSampleT > ClassSamplesContainerT; //!< Class samples container type definition.
71 
72  typedef std::map< ClassIDT, ClassSamplesContainerT > MClassesSamplesCT; //!< Multi-classes samples container type definition.
73 
74  typedef boost::shared_ptr<MClassesSamplesCT> MClassesSamplesCTPtr; //!< A shared pointer to a multi classes samples container type definition.
75 
76  MClassesSamplesCTPtr m_trainSamplesPtr; //!< A shared pointer to a always-valid structure where trainning samples are stored.
77 
78  bool m_applyICM; //!< Apply a Iterated conditional modes iteractive filtering (default:false);
79 
80  float m_icmChangesThreshold; //!< Minimum porcentage of number of pixel that changes from one iteration to the other [valid values range:1-100, default:10)].
81 
82  unsigned int m_icmMaxIterations; //!< Maximum number of ICM iterations to perform (default 10).
83 
84  Parameters();
85 
86  ~Parameters();
87 
88  //overload
89  const Parameters& operator=(const Parameters& params);
90 
91  //overload
92  void reset() throw( te::rp::Exception );
93 
94  //overload
95  AbstractParameters* clone() const;
96  };
97 
99 
101 
102  //overload
103  bool initialize(ClassifierStrategyParameters const* const strategyParams) throw(te::rp::Exception);
104 
105  //overload
106  bool execute() throw(te::rp::Exception);
107 
108  unsigned int getOutputNumberBands(void);
109 
110  protected :
111 
112  bool m_isInitialized; //!< Is this instance initialized?
113 
114  Parameters::ClassIDT m_highestTrainClassID; //!< The highest train sample lable.
115 
116  ClassifierMaxLikelihoodStrategy::Parameters m_initParams; //!< Initialization parameters.
117 
118  te::cl::MaxLikelihood m_genClassInstance; //!< A generir classifier instance.
119 
120  /*!
121  \brief Apply an ICM filter
122  \param matrixPtr A pointer to a input/output classified matrix (the pointer may be reassigned if required).
123  \param classifiedMatrixNoDataValue Matrix no-data value.
124  \param errorMessage Error message.
125  \return true if OK, false on errors;
126  */
127  bool applyICM( std::unique_ptr< te::rp::Matrix< unsigned int > >& matrixPtr,
128  const unsigned int classifiedMatrixNoDataValue,
129  std::string& errorMessage ) const;
130 
131  /*!
132  \brief Iteration Beta value Calule.
133  \param matrix Current classified matrix.
134  \param matrixNoDataValue Matrix no-data value.
135  \param errorMessage Error message.
136  \return true if OK, false on errors;
137  */
138  bool getICMBeta( const te::rp::Matrix< unsigned int >& matrix,
139  const unsigned int matrixNoDataValue,
140  double& betaValue, std::string& errorMessage ) const;
141 
142  /*!
143  \brief Metodo da biseccao para achar os zeros de uma funcao de dupla precisao com
144  parametros. Numerical Recipes in C cap 9. Versao del 22 de agosto de 1990.
145  INPE. O vetor (double)param contem os seguintes valores: param[0] = o
146  minimo do intervalo de procura param[1] = o maximo do intervalo de procura
147  param[2] = a tolerancia param[3] ... param[n] = parametros da funcao
148 
149  \note Imported from SPRING
150  */
151  double rtbis(double (*func) (double x, double *param), double *param) const;
152 
153  static double plikehood_2(double , double * );
154 
155  static double plikehood_3(double , double * );
156 
157  static double plikehood_4(double , double * );
158 
159  static double sigmo( double a );
160 
161  /*! \brief Compute the common part in pseudo-likelihood equation.
162  \param img_box : image to process.
163  \param term : comom part
164  */
165  static void Estimacao_geral( double *term, const te::rp::Matrix< unsigned int >& matrix );
166 
167  static int compara_uchar_vesre(const void *x, const void *y);
168  };
169 
170  /*!
171  \class ClassifierMaxLikelihoodStrategyFactory
172 
173  \brief Maximum a posteriori probability strategy factory.
174 
175  \note Factory key: RegionGrowing
176  */
178  {
179  public:
180 
182 
184 
185  //overload
187  };
188 
189  } // end namespace rp
190 } // end namespace te
191 
192 #endif // __TERRALIB_RP_INTERNAL_CLASSIFIERMAXLIKELIHOODSTRATEGY_H
193 
std::map< ClassIDT, ClassSamplesContainerT > MClassesSamplesCT
Multi-classes samples container type definition.
Base exception class for plugin module.
Definition: Exception.h:42
A maximum likelihood estimation strategy for classification (a.k.a. MaxVer in portuguese).
unsigned int ClassIDT
Class ID type definition (zero means invalid ID).
std::vector< ClassSampleT > ClassSamplesContainerT
Class samples container type definition.
Raster classifier strategy factory base class.
boost::shared_ptr< MClassesSamplesCT > MClassesSamplesCTPtr
A shared pointer to a multi classes samples container type definition.
TerraLib.
#define TERPEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:139
std::vector< double > ClassSampleT
Class sample type definition.
Generic template matrix.
Raster classifier strategy factory base class.
Maximum a posteriori probability strategy factory.
Configuration flags for the Raster Processing module of TerraLib.
Raster classifier strategy base class.
A generic template matrix.
Definition: Matrix.h:55
bool m_applyICM
Apply a Iterated conditional modes iteractive filtering (default:false);.
float m_icmChangesThreshold
Minimum porcentage of number of pixel that changes from one iteration to the other [valid values rang...
Raster classifier strategy base class.
unsigned int m_icmMaxIterations
Maximum number of ICM iterations to perform (default 10).
MClassesSamplesCTPtr m_trainSamplesPtr
A shared pointer to a always-valid structure where trainning samples are stored.