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 
86 
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 
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 
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  */
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
te::rp::ClassifierISOSegStrategy::Pattern::m_covarianceMatrix
boost::numeric::ublas::matrix< double > m_covarianceMatrix
The covariance matrix between bands.
Definition: ClassifierISOSegStrategy.h:169
te::rp::ClassifierISOSegStrategy::Pattern::reset
void reset()
Reset to initial state.
te::rp::ClassifierISOSegStrategy::Pattern::m_myCluster
Pattern * m_myCluster
The associated cluster of this pattern (optional).
Definition: ClassifierISOSegStrategy.h:165
te
TerraLib.
Definition: AddressGeocodingOp.h:52
te::rp::ClassifierISOSegStrategy::Pattern::m_covarianceMatrixDet
double m_covarianceMatrixDet
The covariance matrix determinant.
Definition: ClassifierISOSegStrategy.h:167
te::rp::ClassifierISOSegStrategy::Parameters::m_acceptanceThreshold
double m_acceptanceThreshold
The acceptance threshold (the closer to 100%, few clusters are created).
Definition: ClassifierISOSegStrategy.h:83
te::rp::ClassifierISOSegStrategy::FillRegionsThreadEntryParams::~FillRegionsThreadEntryParams
~FillRegionsThreadEntryParams()
Definition: ClassifierISOSegStrategy.h:225
te::rp::ClassifierISOSegStrategy::Pattern::m_getBhattacharyyaDistance_meansDif
boost::numeric::ublas::matrix< double > m_getBhattacharyyaDistance_meansDif
Definition: ClassifierISOSegStrategy.h:179
te::rp::ClassifierISOSegStrategy::FillRegionsThreadEntryParams::m_continueProcessingFlagPtr
bool * m_continueProcessingFlagPtr
Pointer to variable pointing the variable to allow the process to continue.
Definition: ClassifierISOSegStrategy.h:221
te::rp::ClassifierISOSegStrategy::Pattern::m_covarianceInversion
boost::numeric::ublas::matrix< double > m_covarianceInversion
The inversion of covariance matrix between bands.
Definition: ClassifierISOSegStrategy.h:170
te::rp::ClassifierISOSegStrategy::FillRegionsThreadEntryParams::FillRegionsThreadEntryParams
FillRegionsThreadEntryParams()
Definition: ClassifierISOSegStrategy.h:223
te::rp::ClassifierISOSegStrategy::getThreshold
double getThreshold(const double acceptanceThreshold, const unsigned nBands) const
te::rp::ClassifierISOSegStrategy::Pattern::m_getBhattacharyyaDistance_distanceTerm1
boost::numeric::ublas::matrix< double > m_getBhattacharyyaDistance_distanceTerm1
Definition: ClassifierISOSegStrategy.h:182
te::rp::ClassifierISOSegStrategy::Pattern::getBhattacharyyaDistance
double getBhattacharyyaDistance(Pattern const *const p) const
Returns the Bhattacharyya distance between two patterns.
te::rp::ClassifierISOSegStrategy::Parameters::InvalidDistanceType
@ InvalidDistanceType
Invalid distance type.
Definition: ClassifierISOSegStrategy.h:77
te::rp::ClassifierISOSegStrategy::Pattern::m_getBhattacharyyaDistance_covsMege
boost::numeric::ublas::matrix< double > m_getBhattacharyyaDistance_covsMege
Definition: ClassifierISOSegStrategy.h:180
te::rp::ClassifierISOSegStrategy::Parameters::reset
void reset()
Clear all internal allocated resources and reset the parameters instance to its initial state.
te::rp::ClassifierISOSegStrategy::Pattern::m_getBhattacharyyaDistance_covsMegeDet
double m_getBhattacharyyaDistance_covsMegeDet
Definition: ClassifierISOSegStrategy.h:177
te::rst::RasterSynchronizer
An access synchronizer to be used in SynchronizedRaster raster instances.
Definition: RasterSynchronizer.h:55
te::rp::ClassifierStrategyParameters
Classifier Strategy Parameters.
Definition: ClassifierStrategyParameters.h:42
te::rp::ClassifierISOSegStrategy::Parameters::~Parameters
~Parameters()
te::rp::ClassifierISOSegStrategy::Pattern::m_getBhattacharyyaDistance_distanceValue
double m_getBhattacharyyaDistance_distanceValue
Definition: ClassifierISOSegStrategy.h:178
te::rp::ClassifierISOSegStrategy::Pattern::Pattern
Pattern(const Pattern &rhs)
Copy constructor.
te::rp::ClassifierISOSegStrategy::Pattern::getMahalanobisDistance
double getMahalanobisDistance(Pattern const *const p) const
Returns the Mahalanobis distance between two patterns.
te::rp::ClassifierISOSegStrategy
ISOSeg strategy for OBIA classification. The algorithm orders regions by area (larger first),...
Definition: ClassifierISOSegStrategy.h:59
ClassifierStrategy.h
Raster classifier strategy base class.
te::rp::ClassifierISOSegStrategy::Pattern::operator=
Pattern & operator=(const Pattern &rhs)
te::rp::ClassifierISOSegStrategy::m_parameters
ClassifierISOSegStrategy::Parameters m_parameters
Internal execution parameters.
Definition: ClassifierISOSegStrategy.h:229
te::rp::ClassifierISOSegStrategy::Pattern
Describes a region or a cluster (group of regions with similar properties) to be used by ISOSeg metho...
Definition: ClassifierISOSegStrategy.h:117
te::rp::ClassifierISOSegStrategyFactory::ClassifierISOSegStrategyFactory
ClassifierISOSegStrategyFactory()
te::rp::ClassifierISOSegStrategy::FillRegionsThreadEntryParams::m_globalMutexPtr
std::mutex * m_globalMutexPtr
Pointer to the mutex.
Definition: ClassifierISOSegStrategy.h:192
te::rp::ClassifierISOSegStrategy::FillRegionsThreadEntryParams::m_inputPolygonsPtr
std::vector< te::gm::Polygon * > const * m_inputPolygonsPtr
Definition: ClassifierISOSegStrategy.h:203
te::rp::ClassifierISOSegStrategy::Pattern::m_getBhattacharyyaDistance_covsMegeInv
boost::numeric::ublas::matrix< double > m_getBhattacharyyaDistance_covsMegeInv
Definition: ClassifierISOSegStrategy.h:181
te::rp::ClassifierISOSegStrategy::Pattern::operator==
bool operator==(const Pattern &rhs) const
Return true if two clusters are equal.
te::rp::ClassifierISOSegStrategy::Parameters::Parameters
Parameters()
te::rp::ClassifierISOSegStrategy::Pattern::m_id
int m_id
The id of the region of the pattern.
Definition: ClassifierISOSegStrategy.h:164
te::rp::ClassifierISOSegStrategy::Pattern::Pattern
Pattern()
te::Exception
Base exception class for plugin module.
Definition: Exception.h:42
te::rp::ClassifierISOSegStrategy::FillRegionsThreadEntryParams::m_inputRasterSyncPtr
te::rst::RasterSynchronizer * m_inputRasterSyncPtr
Pointer to the input raster sync.
Definition: ClassifierISOSegStrategy.h:201
te::rp::ClassifierISOSegStrategy::Pattern::~Pattern
~Pattern()
TERPEXPORT
#define TERPEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:139
te::rp::ClassifierISOSegStrategy::FillRegionsThreadEntryParams::m_inputRasterBandsPtr
std::vector< unsigned int > const * m_inputRasterBandsPtr
Pointer to the input raster bands do be processed.
Definition: ClassifierISOSegStrategy.h:206
te::rp::ClassifierISOSegStrategy::Parameters::operator=
const Parameters & operator=(const Parameters &params)
te::rp::ClassifierISOSegStrategy::Parameters::m_distanceType
DistanceType m_distanceType
Distance type.
Definition: ClassifierISOSegStrategy.h:82
te::rp::ClassifierISOSegStrategy::Parameters::DistanceType
DistanceType
Definition: ClassifierISOSegStrategy.h:76
te::rp::ClassifierISOSegStrategy::FillRegionsThreadEntryParams::m_enableProgress
bool m_enableProgress
Pointer to variable pointing the variable to enable or disable the thread internal progress interface...
Definition: ClassifierISOSegStrategy.h:218
te::rp::ClassifierISOSegStrategy::fillRegionsThreadEntry
static void fillRegionsThreadEntry(FillRegionsThreadEntryParams *paramsPtr)
Fill regions thread entry.
te::rp::ClassifierISOSegStrategy::printMatrix
static void printMatrix(const boost::numeric::ublas::matrix< double > &matrix)
Print a matrix to std::out.
te::rp::ClassifierISOSegStrategyFactory::~ClassifierISOSegStrategyFactory
~ClassifierISOSegStrategyFactory()
te::rp::ClassifierStrategyFactory
Raster classifier strategy factory base class.
Definition: ClassifierStrategyFactory.h:47
te::rp::ClassifierISOSegStrategy::FillRegionsThreadEntryParams
The parameters passed to the fill regions thread entry.
Definition: ClassifierISOSegStrategy.h:189
te::rp::ClassifierISOSegStrategy::Pattern::m_getBhattacharyyaDistance_nBands
unsigned int m_getBhattacharyyaDistance_nBands
Definition: ClassifierISOSegStrategy.h:175
te::rp::ClassifierISOSegStrategy::Pattern::operator<
bool operator<(const Pattern &rhs) const
te::rp::ClassifierISOSegStrategy::FillRegionsThreadEntryParams::m_runningThreadsCounterPtr
unsigned int * m_runningThreadsCounterPtr
Pointer to variable pointing the current number of running threads.
Definition: ClassifierISOSegStrategy.h:215
te::rp::ClassifierISOSegStrategy::Pattern::m_area
double m_area
The area of all regions inside a pattern.
Definition: ClassifierISOSegStrategy.h:166
te::rp::ClassifierISOSegStrategy::FillRegionsThreadEntryParams::m_condVarPtr
std::condition_variable * m_condVarPtr
Pointer to the condition variable.
Definition: ClassifierISOSegStrategy.h:198
Config.h
Proxy configuration file for TerraView (see terraview_config.h).
te::rp::ClassifierStrategy
Raster classifier strategy base class.
Definition: ClassifierStrategy.h:49
te::rp::ClassifierISOSegStrategy::FillRegionsThreadEntryParams::m_outputRegionsPtr
std::multimap< double, Pattern > * m_outputRegionsPtr
Pointer to the output regions container.
Definition: ClassifierISOSegStrategy.h:209
te::rp::ClassifierISOSegStrategy::m_isInitialized
bool m_isInitialized
True if this instance is initialized.
Definition: ClassifierISOSegStrategy.h:228
te::rp::ClassifierISOSegStrategy::Parameters::MahalanobisDistanceType
@ MahalanobisDistanceType
Mahalanobis Distance Type.
Definition: ClassifierISOSegStrategy.h:78
te::rp::ClassifierISOSegStrategy::Pattern::Pattern
Pattern(int i, double a, std::vector< double > mv, boost::numeric::ublas::matrix< double > cm)
Constructor.
te::rp::ClassifierISOSegStrategy::FillRegionsThreadEntryParams::m_condVarMutexPtr
std::mutex * m_condVarMutexPtr
Pointer to the condition variable mutex.
Definition: ClassifierISOSegStrategy.h:195
te::rp::ClassifierISOSegStrategyFactory::build
te::rp::ClassifierStrategy * build()
Concrete factories (derived from this one) must implement this method in order to create objects.
te::rp::ClassifierISOSegStrategy::FillRegionsThreadEntryParams::m_nextInputPolIdx2ProcessPtr
std::size_t * m_nextInputPolIdx2ProcessPtr
Pointer to variable pointing the next polygon index to process.
Definition: ClassifierISOSegStrategy.h:212
te::rp::ClassifierISOSegStrategy::Pattern::m_getBhattacharyyaDistance_bandIdx
unsigned int m_getBhattacharyyaDistance_bandIdx
Definition: ClassifierISOSegStrategy.h:176
ClassifierStrategyFactory.h
Raster classifier strategy factory base class.
te::rp::ClassifierISOSegStrategy::Pattern::m_meanVector
std::vector< double > m_meanVector
The vector of mean values, 1 value per band;.
Definition: ClassifierISOSegStrategy.h:168
te::rp::ClassifierISOSegStrategy::Parameters
Classifier Parameters.
Definition: ClassifierISOSegStrategy.h:68
te::rp::ClassifierISOSegStrategyFactory
Raster ISOSeg Classifier strategy factory.
Definition: ClassifierISOSegStrategy.h:255