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  class AlgorithmParametersSerializer;
48 
49  /*!
50  \class ClassifierISOSegStrategy
51 
52  \brief ISOSeg strategy for OBIA classification. The algorithm orders regions by area (larger first),
53  and classify the largest region as Cluster 1. All regions similar to this cluster are inserted
54  in Cluster 1, otherwise new Clusters are created. After all regions belong to a cluster, the
55  algorithm merges similar clusters. The acceptance threshold is the only parameter given by
56  the user, and it indicates the maximum distance between two regions to be clustered togheter.
57 
58  \ingroup rp_class
59  */
61  {
62  public:
63 
64  /*!
65  \class Parameters
66 
67  \brief Classifier Parameters
68  */
70  {
71  public:
72 
73  /*!
74  \enum class DistanceType
75  \brief Used distance type.
76  */
78  {
79  InvalidDistanceType, //!< Invalid distance type.
80  MahalanobisDistanceType, //!< Mahalanobis Distance Type.
81  BhattacharyyaDistanceType //!< Bhattacharyya Distance Type.
82  };
83 
84  DistanceType m_distanceType; //!< Distance type.
85  double m_acceptanceThreshold; //!< The acceptance threshold (the closer to 100\%, few clusters are created).
86 
88 
90 
91  //overload
92  const Parameters& operator=(const Parameters& params);
93 
94  //overload
95  void reset() ;
96 
97  //overload
98  AbstractParameters* clone() const;
99 
100  //overload
101  bool serialize ( AlgorithmParametersSerializer& serializer ) const;
102  };
103 
105 
107 
108  //overload
109  bool initialize(ClassifierStrategyParameters const* const strategyParams) ;
110 
111  //overload
112  bool execute() ;
113 
114  protected:
115 
116  /*!
117  \class Pattern
118 
119  \brief Describes a region or a cluster (group of regions with similar properties) to be used by ISOSeg method.
120  */
122  {
123  public:
124 
126 
127  /*!
128  \brief Constructor.
129 
130  \param i The id of the initial region.
131  \param a The area of the region.
132  \param mv The vector of mean values, 1 value per band.
133  \param cm The covariance matrix between bands.
134  */
135  Pattern(int i, double a, std::vector<double> mv, boost::numeric::ublas::matrix<double> cm);
136 
137  /*!
138  \brief Copy constructor.
139 
140  \param rhs The right-hand-side copy that would be used to copy from.
141  */
142  Pattern( const Pattern& rhs);
143 
145 
146  /*! \brief Returns the Mahalanobis distance between two patterns */
147  double getMahalanobisDistance(Pattern const * const p) const;
148 
149  /*!
150  \brief Returns the Bhattacharyya distance between two patterns
151  \note Returns std::numeric_limits< double >::max() on errors.
152  */
153  double getBhattacharyyaDistance(Pattern const * const p) const;
154 
155  /*! \brief Return true if two clusters are equal. */
156  bool operator==( const Pattern& rhs) const;
157 
158  // overload
159  Pattern& operator=( const Pattern& rhs );
160 
161  // overload (by m_area comparison)
162  bool operator<(const Pattern& rhs) const;
163 
164  /*!
165  \brief Reset to initial state.
166  */
167  void reset();
168 
169  int m_id; //!< The id of the region of the pattern.
170  Pattern* m_myCluster; //!< The associated cluster of this pattern (optional).
171  double m_area; //!< The area of all regions inside a pattern.
172  double m_covarianceMatrixDet; //!< The covariance matrix determinant.
173  std::vector<double> m_meanVector; //!< The vector of mean values, 1 value per band;
174  boost::numeric::ublas::matrix<double> m_covarianceMatrix; //!< The covariance matrix between bands.
175  boost::numeric::ublas::matrix<double> m_covarianceInversion; //!< The inversion of covariance matrix between bands.
176 
177  protected :
178 
179  // Variables used by the getBhattacharyyaDistance method
180  mutable unsigned int m_getBhattacharyyaDistance_nBands;
184  mutable boost::numeric::ublas::matrix<double> m_getBhattacharyyaDistance_meansDif;
185  mutable boost::numeric::ublas::matrix<double> m_getBhattacharyyaDistance_covsMege;
186  mutable boost::numeric::ublas::matrix<double> m_getBhattacharyyaDistance_covsMegeInv;
187  mutable boost::numeric::ublas::matrix<double> m_getBhattacharyyaDistance_distanceTerm1;
188  };
189 
190  /*!
191  \brief The parameters passed to the fill regions thread entry.
192  */
194  {
195  public :
196  //! Pointer to the mutex.
197  std::mutex* m_globalMutexPtr;
198 
199  //! Pointer to the condition variable mutex.
200  std::mutex* m_condVarMutexPtr;
201 
202  //! Pointer to the condition variable.
203  std::condition_variable* m_condVarPtr;
204 
205  //! Pointer to the input raster sync.
207 
208  std::vector<te::gm::Polygon*> const * m_inputPolygonsPtr;
209 
210  //! Pointer to the input raster bands do be processed.
211  std::vector< unsigned int > const * m_inputRasterBandsPtr;
212 
213  //! Pointer to the output regions container.
214  std::multimap< double, Pattern >* m_outputRegionsPtr;
215 
216  //! Pointer to variable pointing the next polygon index to process.
218 
219  //! Pointer to variable pointing the current number of running threads.
221 
222  //! Pointer to variable pointing the variable to enable or disable the thread internal progress interface.
224 
225  //! Pointer to variable pointing the variable to allow the process to continue.
227 
229 
231  };
232 
233  bool m_isInitialized; //!< True if this instance is initialized.
234  ClassifierISOSegStrategy::Parameters m_parameters; //!< Internal execution parameters.
235 
236  double getThreshold(const double acceptanceThreshold, const unsigned nBands) const;
237 
238  /*!
239  \brief Fill regions thread entry.
240  \param paramsPtr A pointer to the thread parameters.
241  \note Regions with non-invertable covariance matrix will have zero determinants
242  \note Regions IDs have the same value as the input polygons indexes
243  */
245 
246  /*!
247  \brief Print a matrix to std::out.
248  */
249  static void printMatrix(const boost::numeric::ublas::matrix<double>& matrix);
250  };
251 
252  /*!
253  \class ClassifierISOSegStrategyFactory
254 
255  \brief Raster ISOSeg Classifier strategy factory.
256 
257  \note Factory key: RegionGrowing
258  */
260  {
261  public:
262 
264 
266 
267  //overload
269  };
270 
271  } // end namespace rp
272 } // end namespace te
273 
274 #endif // __TERRALIB_RP_INTERNAL_CLASSIFIERISOSEGSTRATEGY_H
Raster classifier strategy factory base class.
Raster classifier strategy base class.
A class to standardize algorithm parameters serialization.
Raster ISOSeg Classifier strategy factory.
te::rp::ClassifierStrategy * build()
Concrete factories (derived from this one) must implement this method in order to create objects.
The parameters passed to the fill regions thread entry.
std::multimap< double, Pattern > * m_outputRegionsPtr
Pointer to the output regions container.
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.
unsigned int * m_runningThreadsCounterPtr
Pointer to variable pointing the current number of running threads.
std::condition_variable * m_condVarPtr
Pointer to the condition variable.
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.
bool m_enableProgress
Pointer to variable pointing the variable to enable or disable the thread internal progress interface...
bool * m_continueProcessingFlagPtr
Pointer to variable pointing the variable to allow the process to continue.
bool serialize(AlgorithmParametersSerializer &serializer) const
Returns a parameter serialization object.
void reset()
Clear all internal allocated resources and reset the parameters instance to its initial state.
AbstractParameters * clone() const
Create a clone copy of this instance.
double m_acceptanceThreshold
The acceptance threshold (the closer to 100%, few clusters are created).
const Parameters & operator=(const Parameters &params)
Describes a region or a cluster (group of regions with similar properties) to be used by ISOSeg metho...
double getMahalanobisDistance(Pattern const *const p) const
Returns the Mahalanobis distance between two patterns.
boost::numeric::ublas::matrix< double > m_covarianceMatrix
The covariance matrix between bands.
int m_id
The id of the region of the pattern.
std::vector< double > m_meanVector
The vector of mean values, 1 value per band;.
double getBhattacharyyaDistance(Pattern const *const p) const
Returns the Bhattacharyya distance between two patterns.
void reset()
Reset to initial state.
double m_covarianceMatrixDet
The covariance matrix determinant.
boost::numeric::ublas::matrix< double > m_getBhattacharyyaDistance_meansDif
boost::numeric::ublas::matrix< double > m_covarianceInversion
The inversion of covariance matrix between bands.
Pattern & operator=(const Pattern &rhs)
Pattern * m_myCluster
The associated cluster of this pattern (optional).
Pattern(int i, double a, std::vector< double > mv, boost::numeric::ublas::matrix< double > cm)
Constructor.
double m_area
The area of all regions inside a pattern.
boost::numeric::ublas::matrix< double > m_getBhattacharyyaDistance_covsMege
boost::numeric::ublas::matrix< double > m_getBhattacharyyaDistance_covsMegeInv
Pattern(const Pattern &rhs)
Copy constructor.
bool operator==(const Pattern &rhs) const
Return true if two clusters are equal.
bool operator<(const Pattern &rhs) const
boost::numeric::ublas::matrix< double > m_getBhattacharyyaDistance_distanceTerm1
ISOSeg strategy for OBIA classification. The algorithm orders regions by area (larger first),...
bool initialize(ClassifierStrategyParameters const *const strategyParams)
Initialize the classification strategy.
bool m_isInitialized
True if this instance is initialized.
bool execute()
Executes the classification strategy.
static void fillRegionsThreadEntry(FillRegionsThreadEntryParams *paramsPtr)
Fill regions thread entry.
double getThreshold(const double acceptanceThreshold, const unsigned nBands) const
ClassifierISOSegStrategy::Parameters m_parameters
Internal execution parameters.
static void printMatrix(const boost::numeric::ublas::matrix< double > &matrix)
Print a matrix to std::out.
Raster classifier strategy factory base class.
Raster classifier strategy base class.
An access synchronizer to be used in SynchronizedRaster raster instances.
TerraLib.
#define TERPEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:139
Proxy configuration file for TerraView (see terraview_config.h).