ClassifierISOSegStrategy.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/ClassifierISOSegStrategy.h
22  \brief ISOSeg strategy for segmentation-based classification.
23  */
24 
25 #ifndef __TERRALIB_RP_INTERNAL_CLASSIFIERISOSEGSTRATEGY_H
26 #define __TERRALIB_RP_INTERNAL_CLASSIFIERISOSEGSTRATEGY_H
27 
28 // TerraLib
29 #include "ClassifierStrategy.h"
31 #include "Config.h"
32 #include "../raster/RasterSynchronizer.h"
33 
34 // STL
35 #include <map>
36 #include <memory>
37 #include <mutex>
38 #include <condition_variable>
39 
40 // Boost
41 #include <boost/numeric/ublas/matrix.hpp>
42 
43 namespace te
44 {
45  namespace rp
46  {
47  /*!
48  \class ClassifierISOSegStrategy
49 
50  \brief ISOSeg strategy for OBIA classification. The algorithm orders regions by area (larger first),
51  and classify the largest region as Cluster 1. All regions similar to this cluster are inserted
52  in Cluster 1, otherwise new Clusters are created. After all regions belong to a cluster, the
53  algorithm merges similar clusters. The acceptance threshold is the only parameter given by
54  the user, and it indicates the maximum distance between two regions to be clustered togheter.
55 
56  \ingroup rp_class
57  */
59  {
60  public:
61 
62  /*!
63  \class Parameters
64 
65  \brief Classifier Parameters
66  */
68  {
69  public:
70 
71  /*!
72  \enum class DistanceType
73  \brief Used distance type.
74  */
76  {
77  InvalidDistanceType, //!< Invalid distance type.
78  MahalanobisDistanceType, //!< Mahalanobis Distance Type.
79  BhattacharyyaDistanceType //!< Bhattacharyya Distance Type.
80  };
81 
82  DistanceType m_distanceType; //!< Distance type.
83  double m_acceptanceThreshold; //!< The acceptance threshold (the closer to 100\%, few clusters are created).
84 
85  Parameters();
86 
87  ~Parameters();
88 
89  //overload
90  const Parameters& operator=(const Parameters& params);
91 
92  //overload
93  void reset() throw( te::rp::Exception );
94 
95  //overload
96  AbstractParameters* clone() const;
97  };
98 
100 
102 
103  //overload
104  bool initialize(ClassifierStrategyParameters const* const strategyParams) throw(te::rp::Exception);
105 
106  //overload
107  bool execute() throw(te::rp::Exception);
108 
109  protected:
110 
111  /*!
112  \class Pattern
113 
114  \brief Describes a region or a cluster (group of regions with similar properties) to be used by ISOSeg method.
115  */
117  {
118  public:
119 
120  Pattern();
121 
122  /*!
123  \brief Constructor.
124 
125  \param i The id of the initial region.
126  \param a The area of the region.
127  \param mv The vector of mean values, 1 value per band.
128  \param cm The covariance matrix between bands.
129  */
130  Pattern(int i, double a, std::vector<double> mv, boost::numeric::ublas::matrix<double> cm);
131 
132  /*!
133  \brief Copy constructor.
134 
135  \param rhs The right-hand-side copy that would be used to copy from.
136  */
137  Pattern( const Pattern& rhs);
138 
139  ~Pattern();
140 
141  /*! \brief Returns the Mahalanobis distance between two patterns */
142  double getMahalanobisDistance(Pattern const * const p) const;
143 
144  /*!
145  \brief Returns the Bhattacharyya distance between two patterns
146  \note Returns std::numeric_limits< double >::max() on errors.
147  */
148  double getBhattacharyyaDistance(Pattern const * const p) const;
149 
150  /*! \brief Return true if two clusters are equal. */
151  bool operator==( const Pattern& rhs) const;
152 
153  // overload
154  Pattern& operator=( const Pattern& rhs );
155 
156  // overload (by m_area comparison)
157  bool operator<(const Pattern& rhs) const;
158 
159  /*!
160  \brief Reset to initial state.
161  */
162  void reset();
163 
164  int m_id; //!< The id of the region of the pattern.
165  Pattern* m_myCluster; //!< The associated cluster of this pattern (optional).
166  double m_area; //!< The area of all regions inside a pattern.
167  double m_covarianceMatrixDet; //!< The covariance matrix determinant.
168  std::vector<double> m_meanVector; //!< The vector of mean values, 1 value per band;
169  boost::numeric::ublas::matrix<double> m_covarianceMatrix; //!< The covariance matrix between bands.
170  boost::numeric::ublas::matrix<double> m_covarianceInversion; //!< The inversion of covariance matrix between bands.
171 
172  protected :
173 
174  // Variables used by the getBhattacharyyaDistance method
175  mutable unsigned int m_getBhattacharyyaDistance_nBands;
179  mutable boost::numeric::ublas::matrix<double> m_getBhattacharyyaDistance_meansDif;
180  mutable boost::numeric::ublas::matrix<double> m_getBhattacharyyaDistance_covsMege;
181  mutable boost::numeric::ublas::matrix<double> m_getBhattacharyyaDistance_covsMegeInv;
182  mutable boost::numeric::ublas::matrix<double> m_getBhattacharyyaDistance_distanceTerm1;
183  };
184 
185  /*!
186  \brief The parameters passed to the fill regions thread entry.
187  */
189  {
190  public :
191  //! Pointer to the mutex.
192  std::mutex* m_globalMutexPtr;
193 
194  //! Pointer to the condition variable mutex.
195  std::mutex* m_condVarMutexPtr;
196 
197  //! Pointer to the condition variable.
198  std::condition_variable* m_condVarPtr;
199 
200  //! Pointer to the input raster sync.
202 
203  std::vector<te::gm::Polygon*> const * m_inputPolygonsPtr;
204 
205  //! Pointer to the input raster bands do be processed.
206  std::vector< unsigned int > const * m_inputRasterBandsPtr;
207 
208  //! Pointer to the output regions container.
209  std::multimap< double, Pattern >* m_outputRegionsPtr;
210 
211  //! Pointer to variable pointing the next polygon index to process.
213 
214  //! Pointer to variable pointing the current number of running threads.
216 
217  //! Pointer to variable pointing the variable to enable or disable the thread internal progress interface.
219 
220  //! Pointer to variable pointing the variable to allow the process to continue.
222 
224 
226  };
227 
228  bool m_isInitialized; //!< True if this instance is initialized.
229  ClassifierISOSegStrategy::Parameters m_parameters; //!< Internal execution parameters.
230 
231  double getThreshold(const double acceptanceThreshold, const unsigned nBands) const;
232 
233  /*!
234  \brief Fill regions thread entry.
235  \param paramsPtr A pointer to the thread parameters.
236  \note Regions with non-invertable covariance matrix will have zero determinants
237  \note Regions IDs have the same value as the input polygons indexes
238  */
239  static void fillRegionsThreadEntry(FillRegionsThreadEntryParams* paramsPtr);
240 
241  /*!
242  \brief Print a matrix to std::out.
243  */
244  static void printMatrix(const boost::numeric::ublas::matrix<double>& matrix);
245  };
246 
247  /*!
248  \class ClassifierISOSegStrategyFactory
249 
250  \brief Raster ISOSeg Classifier strategy factory.
251 
252  \note Factory key: RegionGrowing
253  */
255  {
256  public:
257 
259 
261 
262  //overload
264  };
265 
266  } // end namespace rp
267 } // end namespace te
268 
269 #endif // __TERRALIB_RP_INTERNAL_CLASSIFIERISOSEGSTRATEGY_H
TEDATAACCESSEXPORT te::da::Expression * operator<(const te::da::Expression &e1, const te::da::Expression &e2)
Raster ISOSeg Classifier strategy factory.
Base exception class for plugin module.
Definition: Exception.h:42
boost::numeric::ublas::matrix< double > m_getBhattacharyyaDistance_distanceTerm1
boost::numeric::ublas::matrix< double > m_covarianceInversion
The inversion of covariance matrix between bands.
std::size_t * m_nextInputPolIdx2ProcessPtr
Pointer to variable pointing the next polygon index to process.
std::mutex * m_condVarMutexPtr
Pointer to the condition variable mutex.
boost::numeric::ublas::matrix< double > m_getBhattacharyyaDistance_meansDif
TEDATAACCESSEXPORT te::da::Expression * operator==(const te::da::Expression &e1, const te::da::Expression &e2)
An access synchronizer to be used in SynchronizedRaster raster instances.
boost::numeric::ublas::matrix< double > m_getBhattacharyyaDistance_covsMege
bool m_isInitialized
True if this instance is initialized.
boost::numeric::ublas::matrix< double > m_getBhattacharyyaDistance_covsMegeInv
The parameters passed to the fill regions thread entry.
bool m_enableProgress
Pointer to variable pointing the variable to enable or disable the thread internal progress interface...
std::vector< unsigned int > const * m_inputRasterBandsPtr
Pointer to the input raster bands do be processed.
te::rst::RasterSynchronizer * m_inputRasterSyncPtr
Pointer to the input raster sync.
Raster classifier strategy factory base class.
ISOSeg strategy for OBIA classification. The algorithm orders regions by area (larger first)...
TerraLib.
#define TERPEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:139
std::condition_variable * m_condVarPtr
Pointer to the condition variable.
Raster classifier strategy factory base class.
int m_id
The id of the region of the pattern.
Configuration flags for the Raster Processing module of TerraLib.
Describes a region or a cluster (group of regions with similar properties) to be used by ISOSeg metho...
bool * m_continueProcessingFlagPtr
Pointer to variable pointing the variable to allow the process to continue.
unsigned int * m_runningThreadsCounterPtr
Pointer to variable pointing the current number of running threads.
Raster classifier strategy base class.
std::multimap< double, Pattern > * m_outputRegionsPtr
Pointer to the output regions container.
ClassifierISOSegStrategy::Parameters m_parameters
Internal execution parameters.
double m_area
The area of all regions inside a pattern.
std::vector< double > m_meanVector
The vector of mean values, 1 value per band;.
Raster classifier strategy base class.
double m_acceptanceThreshold
The acceptance threshold (the closer to 100%, few clusters are created).
Pattern * m_myCluster
The associated cluster of this pattern (optional).
double m_covarianceMatrixDet
The covariance matrix determinant.
boost::numeric::ublas::matrix< double > m_covarianceMatrix
The covariance matrix between bands.