All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
SegmenterRegionGrowingStrategy.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/SegmenterRegionGrowingStrategy.h
22  \brief Raster region growing segmenter strategy.
23  */
24 
25 #ifndef __TERRALIB_RP_INTERNAL_SEGMENTERREGIONGROWINGSTRATEGY_H
26 #define __TERRALIB_RP_INTERNAL_SEGMENTERREGIONGROWINGSTRATEGY_H
27 
29 #include "SegmenterStrategy.h"
33 #include "SegmenterSegmentsBlock.h"
34 #include "Matrix.h"
35 #include "Config.h"
36 
37 #include <vector>
38 #include <list>
39 #include <set>
40 
41 namespace te
42 {
43  namespace rp
44  {
45  /*!
46  \class SegmenterRegionGrowingStrategy
47  \brief Raster region growing segmenter strategy.
48  \ingroup rp_seg
49  */
51  {
52  public :
53 
54  /*!
55  \class Parameters
56  \brief Segmenter Parameters
57  */
59  {
60  public:
61 
62  /**
63  * \name Global parameters
64  */
65  /**@{*/
66 
67  /*! \enum SegmentFeaturesType Segment features types. */
69  {
70  InvalidFeaturesType, //!< Invalid features type.
71  MeanFeaturesType, //!< The mean of segments pixel values will be used - Reference: S. A. Bins, L. M. G. Fonseca, G. J. Erthal e F. M. Ii, "Satellite Imagery segmentation: a region growing approach", VIII Simposio Brasileiro de Sensoriamento Remoto, Salvador, BA, 14-19 abril 1996.
72  BaatzFeaturesType //!< The Baatz based features will be used - Reference: Baatz, M.; Schape, A. Multiresolution segmentation: an optimization approach for high quality multi-scale image segmentation. In: XII Angewandte Geographische Informationsverarbeitung, Wichmann-Verlag, Heidelberg, 2000.
73  };
74 
75  unsigned int m_minSegmentSize; //!< A positive minimum segment size (pixels number - default: 100).
76 
77  double m_segmentsSimilarityThreshold; //!< Segments similarity treshold - Use lower values to merge only those segments that are more similar - Higher values will allow more segments to be merged - valid values range: positive values - default: 0.03 ).
78 
79  SegmentFeaturesType m_segmentFeatures; //!< What segment features will be used on the segmentation process (default:InvalidFeaturesType).
80 
81  unsigned int m_segmentsSimIncreaseSteps; //!< The maximum number of steps to increment the similarity threshold value for the cases where no segment merge occurred - zero will disable segment similarity threshold increments - defaul: 2.
82 
83  bool m_enableLocalMutualBestFitting; //!< If enabled, a merge only occurs between two segments if the minimum dissimilarity criteria is best fulfilled mutually (default: false).
84 
85  bool m_enableSameIterationMerges; //!< If enabled, a merged segment could be merged with another within the same iteration (default:false).
86 
87  //@}
88 
89  /**
90  * \name Baatz specific parameters
91  */
92  /**@{*/
93 
94  std::vector< double > m_bandsWeights; //!< The weight given to each band, when applicable (note: the bands weights sum must always be 1) or an empty vector indicating that all bands have the same weight.
95 
96  double m_colorWeight; //!< The weight given to the color component, deafult:0.9, valid range: [0,1].
97 
98  double m_compactnessWeight; //!< The weight given to the compactness component, deafult:0.5, valid range: [0,1].
99 
100  //@}
101 
102  Parameters();
103 
104  ~Parameters();
105 
106  //overload
107  const Parameters& operator=( const Parameters& params );
108 
109  //overload
110  void reset() throw( te::rp::Exception );
111 
112  //overload
113  AbstractParameters* clone() const;
114  };
115 
117 
119 
120  //overload
121  bool initialize(
122  SegmenterStrategyParameters const* const strategyParams )
123  throw( te::rp::Exception );
124 
125  //overload
126  void reset();
127 
128  //overload
129  bool execute(
130  SegmenterIdsManager& segmenterIdsManager,
131  const te::rp::SegmenterSegmentsBlock& block2ProcessInfo,
132  const te::rst::Raster& inputRaster,
133  const std::vector< unsigned int >& inputRasterBands,
134  const std::vector< double >& inputRasterNoDataValues,
135  const std::vector< double >& inputRasterBandMinValues,
136  const std::vector< double >& inputRasterBandMaxValues,
137  te::rst::Raster& outputRaster,
138  const unsigned int outputRasterBand,
139  const bool enableProgressInterface ) throw( te::rp::Exception );
140 
141  //overload
142  double getMemUsageEstimation( const unsigned int bandsToProcess,
143  const unsigned int pixelsNumber ) const;
144 
145  //overload
146  unsigned int getOptimalBlocksOverlapSize() const;
147 
148  protected :
149 
150  /*!
151  \brief Internal segments ids matrix type definition.
152  */
153  typedef Matrix< SegmenterSegmentsBlock::SegmentIdDataType >
155 
156  /*!
157  \class Merger
158  \brief Segments merger
159  */
161  {
162  public:
163 
164  virtual ~Merger();
165 
166  /*!
167  \brief Returns a dimilarity index between this and the other segment.
168  \param segment1Ptr A pointer to the first segment.
169  \param segment2Ptr A pointer to the second segment.
170  \param mergePreviewSegPtr A pointer to a valid segment where the merged features values will be stored (when aplicable).
171  \return A similarity index between this and the other segment ( normalized between 0 and 1 ).
172  */
173  virtual SegmenterRegionGrowingSegment::FeatureType getDissimilarity(
174  SegmenterRegionGrowingSegment const * const segment1Ptr,
175  SegmenterRegionGrowingSegment const * const segment2Ptr,
176  SegmenterRegionGrowingSegment * const mergePreviewSegPtr ) const = 0;
177 
178  /*!
179  \brief Merge specific segment features from both segments into the first segment.
180  \param segment1Ptr The first segment.
181  \param segment2Ptr A pointer to the second segment.
182  \param mergePreviewSegPtr A pointer to a valid segment where the merged features values were be stored by calling getDissimilarityIndex (when aplicable).
183  */
184  virtual void mergeFeatures(
185  SegmenterRegionGrowingSegment * const segment1Ptr,
186  SegmenterRegionGrowingSegment const * const segment2Ptr,
187  SegmenterRegionGrowingSegment const * const mergePreviewSegPtr ) const = 0;
188 
189  /*!
190  \brief Update the internal state.
191  \param actSegsListHeadPtr A pointer the the active segments list head.
192  */
193  virtual void update( SegmenterRegionGrowingSegment* const actSegsListHeadPtr ) = 0;
194 
195  /*!
196  \brief Return the required segments features vector size (numer of elements).
197  \return Return the required segments features vector size (numer of elements).
198  */
199  virtual unsigned int getSegmentFeaturesSize() const = 0;
200 
201  protected :
202 
203  Merger();
204 
205  private :
206 
207  Merger( const Merger& ) {};
208 
209  const Merger& operator=( const Merger& other ) { return other; };
210  };
211 
212  /*!
213  \class MeanMerger
214  \brief Mean based Segments merger
215  */
217  {
218  public:
219 
220  MeanMerger( const unsigned int featuresNumber );
221 
222  ~MeanMerger();
223 
224  //overload
226  SegmenterRegionGrowingSegment const * const segment1Ptr,
227  SegmenterRegionGrowingSegment const * const segment2Ptr,
228  SegmenterRegionGrowingSegment * const mergePreviewSegPtr ) const;
229 
230  //overload
231  void mergeFeatures(
232  SegmenterRegionGrowingSegment * const segment1Ptr,
233  SegmenterRegionGrowingSegment const * const segment2Ptr,
234  SegmenterRegionGrowingSegment const * const mergePreviewSegPtr ) const;
235 
236  //overload
238 
239  //overload
240  inline unsigned int getSegmentFeaturesSize() const { return m_featuresNumber; };
241 
242  protected :
243 
244  unsigned int m_featuresNumber; //!< The number of features (bands).
246 
247  // variables used by the method getDissimilarity
250  mutable unsigned int m_getDissimilarity_meansIdx;
251  };
252 
253  /*!
254  \class BaatzMerger
255  \brief Baatz based Segments merger
256  */
258  {
259  public:
260 
261  /*!
262  \brief Default constructor.
263  \param bandsWeights A reference to an external valid structure where each bands weight are stored.
264  \param segmentsIds //!< A reference to an external valid structure where all segments IDs are stored.
265  \param segmentsMatrix //!< A reference to an external valid segments matrix.
266  \param colorWeight //!< The weight given to the color component, deafult:0.5, valid range: [0,1].
267  \param compactnessWeight //!< The weight given to the compactness component, deafult:0.5, valid range: [0,1].
268  */
269  BaatzMerger( const double& colorWeight, const double& compactnessWeight,
270  const std::vector< double >& bandsWeights,
271  const SegmentsIdsMatrixT& segmentsIds );
272 
273  ~BaatzMerger();
274 
275  //overload
277  SegmenterRegionGrowingSegment const * const segment1Ptr,
278  SegmenterRegionGrowingSegment const * const segment2Ptr,
279  SegmenterRegionGrowingSegment * const mergePreviewSegPtr ) const;
280 
281  //overload
282  void mergeFeatures(
283  SegmenterRegionGrowingSegment * const segment1Ptr,
284  SegmenterRegionGrowingSegment const * const segment2Ptr,
285  SegmenterRegionGrowingSegment const * const mergePreviewSegPtr ) const;
286 
287  //overload
288  void update( SegmenterRegionGrowingSegment* const actSegsListHeadPtr );
289 
290  //overload
291  inline unsigned int getSegmentFeaturesSize() const { return 3 + ( 3 * m_bandsNumber ); };
292 
293  protected :
294 
295  const SegmentsIdsMatrixT& m_segmentsIds; //!< A reference to an external valid structure where each all segments IDs are stored.
296 
297  unsigned int m_bandsNumber; //!< The number of features (bands).
298 
299  SegmenterRegionGrowingSegment::FeatureType m_allSegsCompactnessOffset; //!< The offsets applied to normalize the compactness value.
300 
301  SegmenterRegionGrowingSegment::FeatureType m_allSegsCompactnessGain; //!< The gains applied to normalize the compactness value.
302 
303  SegmenterRegionGrowingSegment::FeatureType m_allSegsSmoothnessOffset; //!< The offsets applied to normalize the smoothness value.
304 
305  SegmenterRegionGrowingSegment::FeatureType m_allSegsSmoothnessGain; //!< The gains applied to normalize the smoothness value.
306 
307  SegmenterRegionGrowingSegment::FeatureType m_allSegsStdDevOffset; //!< The offsets applied to normalize the standard deviation value.
308 
309  SegmenterRegionGrowingSegment::FeatureType m_allSegsStdDevGain; //!< The gains applied to normalize the standard deviation value.
310 
311  SegmenterRegionGrowingSegment::FeatureType m_colorWeight; //!< The weight given to the color component, deafult:0.5, valid range: [0,1].
312 
313  SegmenterRegionGrowingSegment::FeatureType m_compactnessWeight; //!< The weight given to the compactness component, deafult:0.5, valid range: [0,1].
314 
315  std::vector< SegmenterRegionGrowingSegment::FeatureType > m_bandsWeights; //!< A vector where each bands weight are stored.
316 
317  // Variables used by the method getDissimilarity
331  mutable unsigned int m_getDissimilarity_sumsIdx;
332 
333  // Variables used by the method update
342  mutable unsigned int m_update_band;
344  };
345 
346  /*!
347  \brief true if this instance is initialized.
348  */
350 
351  /*!
352  \brief Internal execution parameters.
353  */
355 
356  /*! \brief A pool of segments that can be reused on each strategy execution. */
358 
359  /*! \brief A internal segments IDs matrix that can be reused on each strategy execution. */
361 
362  /*!
363  \brief Initialize the segment objects container and the segment IDs container.
364  \param segmenterIdsManager A segments ids manager to acquire unique segments ids.
365  \param block2ProcessInfo Info about the block to process.
366  \param inputRaster The input raster.
367  \param inputRasterBands Input raster bands to use.
368  \param inputRasterNoDataValues A vector of values to be used as input raster no-data values.
369  \param inputRasterBandMinValues The minimum value present on each band.
370  \param inputRasterBandMinValues The maximum value present on each band.
371  \param actSegsListHeadPtr A pointer the the active segments list head.
372  \return true if OK, false on errors.
373  */
374  bool initializeSegments( SegmenterIdsManager& segmenterIdsManager,
375  const te::rp::SegmenterSegmentsBlock& block2ProcessInfo,
376  const te::rst::Raster& inputRaster,
377  const std::vector< unsigned int >& inputRasterBands,
378  const std::vector< double >& inputRasterNoDataValues,
379  const std::vector< double >& inputRasterBandMinValues,
380  const std::vector< double >& inputRasterBandMaxValues,
381  SegmenterRegionGrowingSegment** actSegsListHeadPtr );
382 
383  /*!
384  \brief Merge closest segments.
385  \param disimilarityThreshold The maximum similarity value allowed when deciding when to merge two segments.
386  \param maxSegSizeThreshold Segments with sizes smaller then this value will allways be merged with the closest segment (disimilarityThreshold will be ignored).
387  \param segmenterIdsManager A segments ids manager to acquire unique segments ids.
388  \param merger The merger instance to use.
389  \param enablelocalMutualBestFitting If enabled, a merge only occurs between two segments if the minimum dissimilarity criteria is best fulfilled mutually.
390  \param enableSameIterationMerges If enabled, a merged segment could be merged with another under the same iteration.
391  \param auxSeg1Ptr A pointer to a valid auxiliar segment that will be used by this method.
392  \param auxSeg2Ptr A pointer to a valid auxiliar segment that will be used by this method.
393  \param auxSeg3Ptr A pointer to a valid auxiliar segment that will be used by this method.
394  \param minFoundDissimilarity The minimum dissimilarity value found.
395  \param maxFoundDissimilarity The maximum dissimilarity value found.
396  \param totalMergesNumber The total number of merges.
397  \param mergeIterationCounter A reference to a iteration number counter (this variable will be only incremented, never zeroed. It never must be reset. ).
398  \param actSegsListHeadPtr A pointer the the active segments list head.
399  */
400  void mergeSegments(
401  const SegmenterRegionGrowingSegment::FeatureType disimilarityThreshold,
402  const unsigned int maxSegSizeThreshold,
403  SegmenterIdsManager& segmenterIdsManager,
404  Merger& merger,
405  const bool enablelocalMutualBestFitting,
406  const bool enableSameIterationMerges,
407  SegmenterRegionGrowingSegment* auxSeg1Ptr,
408  SegmenterRegionGrowingSegment* auxSeg2Ptr,
409  SegmenterRegionGrowingSegment* auxSeg3Ptr,
410  SegmenterRegionGrowingSegment::FeatureType& minFoundDissimilarity,
411  SegmenterRegionGrowingSegment::FeatureType& maxFoundDissimilarity,
412  unsigned int& totalMergesNumber,
413  SegmenterRegionGrowingSegment::IterationCounterType& globalMergeIterationsCounter,
414  SegmenterRegionGrowingSegment** const actSegsListHeadPtrPtr );
415 
416  /*!
417  \brief Export the segments IDs to a tif file.
418  \param segmentsIds The output segment ids container.
419  \param normto8bits If true, a 8 bits file will be generated.
420  \param fileName The output tif file name.
421  */
422  void exportSegs2Tif( const SegmentsIdsMatrixT& segmentsIds,
423  bool normto8bits, const std::string& fileName );
424 
425  /*!
426  \brief Returns the count of points from region 1 (with ID1) touching the region 2 (with ID2).
427  \param segsIds The segment ids container.
428  \param xStart The upper left X of the bounding box surrounding both regions.
429  \param yStart The upper left Y of the bounding box surrounding both regions.
430  \param xBound The lower right X bound of the bounding box surrounding both regions.
431  \param yBound The lower right Y bound of the bounding box surrounding both regions.
432  \param id1 Region 1 ID.
433  \param id2 Region 2 ID.
434  \param edgeLength1 The touching edge length for the region 1.
435  \param edgeLength2 The touching edge length for the region 2.
436  \return Returns the count of points from region 1 (with ID1) touching the region 2 (with ID2).
437  */
438  static void getTouchingEdgeLength( const SegmentsIdsMatrixT& segsIds,
439  const unsigned int& xStart, const unsigned int& yStart,
440  const unsigned int& xBound, const unsigned int& yBound,
443  unsigned int& edgeLength1,
444  unsigned int& edgeLength2 );
445 
446  /*!
447  \brief Returns the number of active segments.
448  \param actSegsListHeadPtr A pointer the the active segments list head.
449  \return Returns the number of active segments.
450  */
451  unsigned int getActiveSegmentsNumber( SegmenterRegionGrowingSegment* const actSegsListHeadPtr ) const;
452  };
453 
454  /*!
455  \class SegmenterRegionGrowingStrategyFactory
456  \brief Raster region growing segmenter strategy factory.
457  \note Factory key: RegionGrowing
458  */
461  {
462  public:
463 
465 
467 
468  //overload
469  te::rp::SegmenterStrategy* build();
470 
471  };
472 
473  } // end namespace rp
474 } // end namespace te
475 
476 #endif // __TERRALIB_RP_INTERNAL_ALGORITHM_H
477 
SegmenterRegionGrowingSegment::FeatureType m_getDissimilarity_sizeSeg1D
Segmenter segments IDs manager.
Raster segmenter strategy factory base class.
SegmenterRegionGrowingSegment::FeatureType const * m_update_stdDevPtr
SegmenterRegionGrowingSegment::FeatureType m_getDissimilarity_squaresSumUnion
SegmenterRegionGrowingSegment::FeatureType m_update_compactnessMin
void update(SegmenterRegionGrowingSegment *const )
Update the internal state.
SegmenterRegionGrowingSegment::FeatureType m_compactnessWeight
The weight given to the compactness component, deafult:0.5, valid range: [0,1].
SegmenterRegionGrowingSegment::FeatureType m_allSegsCompactnessGain
The gains applied to normalize the compactness value.
bool m_isInitialized
true if this instance is initialized.
Raster region growing segmenter strategy.
SegmenterRegionGrowingSegment::FeatureType m_update_compactnessMax
std::vector< SegmenterRegionGrowingSegment::FeatureType > m_bandsWeights
A vector where each bands weight are stored.
SegmenterRegionGrowingStrategy::Parameters m_parameters
Internal execution parameters.
SegmenterRegionGrowingSegment::FeatureType m_allSegsStdDevGain
The gains applied to normalize the standard deviation value.
SegmenterRegionGrowingSegment::FeatureType m_getDissimilarity_dissValue
SegmentsIdsMatrixT m_segmentsIdsMatrix
A internal segments IDs matrix that can be reused on each strategy execution.
SegmenterRegionGrowingSegment::FeatureType m_getDissimilarity_meanUnion
SegmenterRegionGrowingSegment::FeatureType m_getDissimilarity_sizeUnionD
SegmenterRegionGrowingSegment::FeatureType m_update_smoothnessMax
bool m_enableSameIterationMerges
If enabled, a merged segment could be merged with another within the same iteration (default:false)...
SegmenterRegionGrowingSegment::FeatureType m_allSegsCompactnessOffset
The offsets applied to normalize the compactness value.
Raster region growing segmenter strategy factory.
SegmenterRegionGrowingSegment::FeatureType m_getDissimilarity_hForm
SegmenterRegionGrowingSegment::FeatureType m_allSegsSmoothnessGain
The gains applied to normalize the smoothness value.
Raster segmenter strategy factory base class.
std::vector< double > m_bandsWeights
The weight given to each band, when applicable (note: the bands weights sum must always be 1) or an e...
SegmenterRegionGrowingSegment::FeatureType m_getDissimilarity_hColor
Region Growing segment.
SegmenterRegionGrowingSegment::FeatureType m_getDissimilarity_diffValue
SegmenterRegionGrowingSegmentsPool m_segmentsPool
A pool of segments that can be reused on each strategy execution.
The mean of segments pixel values will be used - Reference: S. A. Bins, L. M. G. Fonseca, G. J. Erthal e F. M. Ii, "Satellite Imagery segmentation: a region growing approach", VIII Simposio Brasileiro de Sensoriamento Remoto, Salvador, BA, 14-19 abril 1996.
An abstract class for raster data strucutures.
Definition: Raster.h:71
SegmenterRegionGrowingSegment::FeatureType m_allSegsStdDevOffset
The offsets applied to normalize the standard deviation value.
unsigned short int IterationCounterType
Feature type definition.
Raster segmenter strategy base class.
#define TERPEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:141
SegmentFeaturesType m_segmentFeatures
What segment features will be used on the segmentation process (default:InvalidFeaturesType).
Generic template matrix.
unsigned int m_minSegmentSize
A positive minimum segment size (pixels number - default: 100).
double m_segmentsSimilarityThreshold
Segments similarity treshold - Use lower values to merge only those segments that are more similar - ...
SegmenterRegionGrowingSegment::FeatureType * m_update_featuresPtr
Raster segmenter strategy base class.
Configuration flags for the Raster Processing module of TerraLib.
SegmenterRegionGrowingSegment::FeatureType m_getDissimilarity_hSmooth
unsigned int m_bandsNumber
The number of features (bands).
SegmenterRegionGrowingSegment::FeatureType m_update_stdDevMin
Segments block.
SegmenterRegionGrowingSegment::FeatureType m_getDissimilarity_stdDevUnion
Raster segmenter strategy parameters base class.
A generic template matrix.
Definition: Matrix.h:51
SegmenterRegionGrowingSegment::FeatureType m_colorWeight
The weight given to the color component, deafult:0.5, valid range: [0,1].
double m_colorWeight
The weight given to the color component, deafult:0.9, valid range: [0,1].
unsigned int m_segmentsSimIncreaseSteps
The maximum number of steps to increment the similarity threshold value for the cases where no segmen...
Segmenter segments block description class.
SegmenterRegionGrowingSegment::FeatureType m_getDissimilarity_sizeSeg2D
double m_compactnessWeight
The weight given to the compactness component, deafult:0.5, valid range: [0,1].
SegmenterRegionGrowingSegment::FeatureType m_getDissimilarity_hCompact
SegmenterRegionGrowingSegment::FeatureType m_allSegsSmoothnessOffset
The offsets applied to normalize the smoothness value.
bool m_enableLocalMutualBestFitting
If enabled, a merge only occurs between two segments if the minimum dissimilarity criteria is best fu...
SegmenterRegionGrowingSegment::FeatureType m_dissimilarityNormFactor
SegmenterRegionGrowingSegment::FeatureType m_getDissimilarity_sumUnion
SegmenterRegionGrowingSegment::FeatureType m_update_smoothnessMin
SegmenterRegionGrowingSegment::FeatureType m_update_stdDevMax