All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SegmenterRegionGrowingStrategy.h
Go to the documentation of this file.
1 /* Copyright (C) 2001-2009 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"
31 #include "SegmenterSegment.h"
32 #include "SegmenterSegmentsPool.h"
33 #include "SegmenterSegmentsBlock.h"
34 #include "Matrix.h"
35 #include "Config.h"
36 
37 #include <vector>
38 #include <list>
39 
40 namespace te
41 {
42  namespace rp
43  {
44  /*!
45  \class SegmenterRegionGrowingStrategy
46  \brief Raster region growing segmenter strategy.
47  */
49  {
50  public :
51 
52  /*!
53  \class Parameters
54  \brief Segmenter Parameters
55  */
57  {
58  public:
59 
60  /*! \enum SegmentFeaturesType Segment features types. */
62  {
63  InvalidFeaturesType, //!< Invalid features type.
64  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.
65  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.
66  };
67 
68  unsigned int m_minSegmentSize; //!< A positive minimum segment size (pixels number - default: 100).
69 
70  double m_segmentsSimilarityThreshold; //!< Segments similarity treshold - Segments with similarity values below this value will be merged; valid values range: [ 0, 1 ]; default:0.1.
71 
72  SegmentFeaturesType m_segmentFeatures; //!< What segment features will be used on the segmentation process (default:InvalidFeaturesType).
73 
74  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.
75 
76  double m_colorWeight; //!< The weight given to the color component, deafult:0.5, valid range: [0,1].
77 
78  double m_compactnessWeight; //!< The weight given to the compactness component, deafult:0.5, valid range: [0,1].
79 
80  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: 10.
81 
82  Parameters();
83 
84  ~Parameters();
85 
86  //overload
87  const Parameters& operator=( const Parameters& params );
88 
89  //overload
90  void reset() throw( te::rp::Exception );
91 
92  //overload
93  AbstractParameters* clone() const;
94  };
95 
97 
99 
100  //overload
101  bool initialize(
102  SegmenterStrategyParameters const* const strategyParams )
103  throw( te::rp::Exception );
104 
105  //overload
106  void reset();
107 
108  //overload
109  bool execute(
110  SegmenterIdsManager& segmenterIdsManager,
111  const te::rst::Raster& inputRaster,
112  const std::vector< unsigned int >& inputRasterBands,
113  const std::vector< double >& inputRasterGains,
114  const std::vector< double >& inputRasterOffsets,
115  te::rst::Raster& outputRaster,
116  const unsigned int outputRasterBand,
117  const bool enableProgressInterface ) throw( te::rp::Exception );
118 
119  //overload
120  double getMemUsageEstimation( const unsigned int bandsToProcess,
121  const unsigned int pixelsNumber ) const;
122 
123  //overload
124  unsigned int getOptimalBlocksOverlapSize() const;
125 
126  protected :
127 
128  /*!
129  \brief Internal segments ids matrix type definition.
130  */
131  typedef Matrix< SegmenterSegmentsBlock::SegmentIdDataType >
133 
134  /*!
135  \class SegmentFeatures
136  \brief Segment features base class
137  */
139  {
140  public:
141 
142  /*!
143  \brief Segment ID.
144  */
146 
147  /*!
148  \brief Segment area (pixels number).
149  */
150  unsigned int m_size;
151 
152  /*!
153  \brief Segment left X coordinate box over the label image.
154  */
155  unsigned int m_xStart;
156 
157  /*!
158  \brief Segment lower bound X coordinate box over the label image.
159  */
160  unsigned int m_xBound;
161 
162  /*!
163  \brief Segment upper Y coordinate box over the label image.
164  */
165  unsigned int m_yStart;
166 
167  /*!
168  \brief Segment lower bound Y coordinate box over the label image.
169  */
170  unsigned int m_yBound;
171 
172  SegmentFeatures();
173 
174  virtual ~SegmentFeatures();
175 
176  /*!
177  \brief Creat a clone of this object.
178  \return A clone of this object.
179  \note The caller of this method must take the ownership of the returned object and delete it when necessary.
180  */
181  virtual SegmentFeatures* clone() const = 0;
182 
183  /*!
184  \brief Copy the other instance state into this one.
185  \param otherPtr The other instance ponter.
186  */
187  virtual void copy( SegmenterRegionGrowingStrategy::SegmentFeatures const * const otherPtr ) = 0;
188 
189  protected :
190 
193 
194  private :
195 
197  };
198 
199  /*!
200  \class Segment
201  \brief Segment base class
202  */
204  {
205  public:
206 
207  /*!
208  \brief Neighborhood segments.
209  */
210  std::list< Segment* > m_neighborSegments;
211 
212  virtual ~Segment();
213 
214  /*!
215  \brief Returns the current segment internal features
216  \return Returns the current segment internal features.
217  */
218  virtual SegmenterRegionGrowingStrategy::SegmentFeatures* getFeatures() = 0;
219 
220  /*!
221  \brief Returns the current segment internal features
222  \return Returns the current segment internal features.
223  */
224  virtual SegmenterRegionGrowingStrategy::SegmentFeatures const* getFeatures() const = 0;
225 
226  protected :
227 
228  Segment();
229 
230  private :
231 
232  Segment( const Segment& ) {};
233 
234  const Segment& operator=( const Segment& other ) { return other; };
235  };
236 
237  /*!
238  \brief Internal segments indexer.
239  \note The indexer takes the ownership of the given segments.
240  \note All segment objects will be given back to the segments pool at this instance destruction time.
241  */
242  class SegmentsIndexer : public std::map<
243  SegmenterSegmentsBlock::SegmentIdDataType, Segment* >
244  {
245  public :
246 
247  SegmentsIndexer( SegmenterSegmentsPool& segmentsPool );
248 
249  ~SegmentsIndexer();
250 
251  //overload
252  void clear();
253 
254  protected :
255 
257  };
258 
259  /*!
260  \class MeanBasedSegment
261  \brief A segment based on pixel mean values
262  */
264  {
265  public:
266 
267  /*!
268  \class SegmentFeatures
269  \brief Mean based segment features.
270  */
272  {
273  public :
274 
275  std::vector< double > m_means; //!< Segment mean values (for each band), normalized between 0 and 1.
276 
277  SegmentFeatures();
278 
279  ~SegmentFeatures();
280 
281  //overload
283 
284  //overload
285  void copy( SegmenterRegionGrowingStrategy::SegmentFeatures const * const otherPtr );
286 
287  private :
288 
290 
291  const SegmentFeatures& operator=( const SegmentFeatures& other ) { return other; };
292  };
293 
295 
297 
298  ~MeanBasedSegment();
299 
300  //overload
302  {
303  return &m_features;
304  };
305 
306  //overload
308  {
309  return &m_features;
310  };
311  };
312 
313  /*!
314  \class BaatzBasedSegment
315  \brief A segment based on Baatz features values
316  */
318  {
319  public:
320 
321  /*!
322  \class SegmentFeatures
323  \brief Baatz based segment features.
324  */
326  {
327  public :
328 
329  std::vector< double > m_sums; //!< Segment sum of segment pixel velues.
330 
331  std::vector< double > m_squaresSum; //!< The sum of squares of pixel values (for each band).
332 
333  std::vector< double > m_stdDev; //!< Standard deviation of segment pixel velues.
334 
335  unsigned int m_edgeLength; //!< Segment edge length.
336 
337  double m_compactness; //!< Compactness of the current segment.
338 
339  double m_smoothness; //!< Smoothness of the current segment.
340 
341  SegmentFeatures();
342 
343  ~SegmentFeatures();
344 
345  //overload
347 
348  //overload
349  void copy( SegmenterRegionGrowingStrategy::SegmentFeatures const * const otherPtr );
350 
351  private :
352 
354 
355  const SegmentFeatures& operator=( const SegmentFeatures& other ) { return other; };
356  };
357 
359 
361 
363 
364  //overload
366  {
367  return &m_features;
368  };
369 
370  //overload
372  {
373  return &m_features;
374  };
375  };
376 
377 
378  /*!
379  \class Merger
380  \brief Segments merger
381  */
383  {
384  public:
385 
386  virtual ~Merger();
387 
388  /*!
389  \brief Returns a dissimilarity index between this and the
390  other segment (normalized between 0 and 1).
391  \param segment1Ptr A pointer to the first segment.
392  \param segment2Ptr A pointer to the second segment.
393  \param mergedFeatures A pointer to a valid segment features instance where the merged features values will be stored.
394  \return A dissimilarity index between this and the
395  other segment ( normalized between 0 and 1 ).
396  */
397  virtual double getDissimilarityIndex(
398  SegmenterRegionGrowingStrategy::Segment const * const segment1Ptr,
399  SegmenterRegionGrowingStrategy::Segment const * const segment2Ptr,
400  SegmenterRegionGrowingStrategy::SegmentFeatures* mergedFeatures ) const = 0;
401 
402  /*!
403  \brief Merge specific segment features from both segments into the first segment.
404  \param segment1Ptr The first segment.
405  \param segment2Ptr A pointer to the second segment.
406  \param mergedFeatures A pointer to a valid segment features instance where the merged features values are stored.
407  */
408  virtual void mergeFeatures( SegmenterRegionGrowingStrategy::Segment * const segment1Ptr,
409  Segment const * const segment2Ptr,
410  SegmenterRegionGrowingStrategy::SegmentFeatures const * const mergedFeatures ) const = 0;
411 
412  /*!
413  \brief Update the internal state.
414  */
415  virtual void update() = 0;
416 
417  protected :
418 
419  Merger();
420 
421  private :
422 
423  Merger( const Merger& ) {};
424 
425  const Merger& operator=( const Merger& other ) { return other; };
426  };
427 
428  /*!
429  \class MeanMerger
430  \brief Mean based Segments merger
431  */
433  {
434  public:
435 
436  MeanMerger();
437 
438  ~MeanMerger();
439 
440  //overload
441  double getDissimilarityIndex(
442  SegmenterRegionGrowingStrategy::Segment const * const segmen1tPtr,
443  SegmenterRegionGrowingStrategy::Segment const * const segmen2tPtr,
444  SegmenterRegionGrowingStrategy::SegmentFeatures* mergedFeatures ) const;
445 
446  //overload
447  void mergeFeatures( SegmenterRegionGrowingStrategy::Segment * const segmen1tPtr,
448  Segment const * const segmen2tPtr,
449  SegmenterRegionGrowingStrategy::SegmentFeatures const * const mergedFeatures ) const;
450 
451  //overload
452  void update() {};
453  };
454 
455  /*!
456  \class BaatzMerger
457  \brief Baatz based Segments merger
458  */
460  {
461  public:
462 
463  /*!
464  \brief Default constructor.
465  \param bandsWeights A reference to an external valid structure where each bands weight are stored.
466  \param segmentsIds //!< A reference to an external valid structure where all segments IDs are stored.
467  \param segments //!< A reference to an external valid segments indexer structure.
468  \param colorWeight //!< The weight given to the color component, deafult:0.5, valid range: [0,1].
469  \param compactnessWeight //!< The weight given to the compactness component, deafult:0.5, valid range: [0,1].
470  */
471  BaatzMerger( const double& colorWeight, const double& compactnessWeight,
472  const std::vector< double >& bandsWeights,
473  const SegmentsIdsMatrixT& segmentsIds,
475 
476  ~BaatzMerger();
477 
478  //overload
479  double getDissimilarityIndex(
480  SegmenterRegionGrowingStrategy::Segment const * const segmen1tPtr,
481  SegmenterRegionGrowingStrategy::Segment const * const segmen2tPtr,
482  SegmenterRegionGrowingStrategy::SegmentFeatures* mergedFeatures ) const;
483 
484  //overload
485  void mergeFeatures( SegmenterRegionGrowingStrategy::Segment * const segmen1tPtr,
486  Segment const * const segmen2tPtr,
487  SegmenterRegionGrowingStrategy::SegmentFeatures const * const mergedFeatures ) const;
488 
489  //overload
490  void update();
491 
492  protected :
493 
494  std::vector< double > m_allSegsStdDevOffsets; //!< The offsets applied to normalize the standard deviation value.
495 
496  std::vector< double > m_allSegsStdDevGain; //!< The gains applied to normalize the standard deviation value.
497 
498  double m_allSegsCompactnessOffset; //!< The offsets applied to normalize the compactness value.
499 
500  double m_allSegsCompactnessGain; //!< The gains applied to normalize the compactness value.
501 
502  double m_allSegsSmoothnessOffset; //!< The offsets applied to normalize the smoothness value.
503 
504  double m_allSegsSmoothnessGain; //!< The gains applied to normalize the smoothness value.
505 
506  double m_colorWeight; //!< The weight given to the color component, deafult:0.5, valid range: [0,1].
507 
508  double m_compactnessWeight; //!< The weight given to the compactness component, deafult:0.5, valid range: [0,1].
509 
510  std::vector< double > m_bandsWeights; //!< A vector where each bands weight are stored.
511 
512  const SegmentsIdsMatrixT& m_segmentsIds; //!< A reference to an external valid structure where each all segments IDs are stored.
513 
514  const SegmenterRegionGrowingStrategy::SegmentsIndexer& m_segments; //!< A reference to an external valid structure where each all segments are indexed.
515  };
516 
517  /*!
518  \brief true if this instance is initialized.
519  */
521 
522  /*!
523  \brief Internal execution parameters.
524  */
526 
527  /*! \brief A pool of segments that can be reused on each strategy execution. */
529 
530  /*! \brief A internal segments IDs matrix that can be reused on each strategy execution. */
532 
533  /*!
534  \brief Initialize the segment objects container and the segment
535  IDs container.
536  \param segmenterIdsManager A segments ids manager to acquire
537  unique segments ids.
538  \param inputRaster The input raster.
539  \param inputRasterBands Input raster bands to use.
540  \param segmentsIds The output segment ids container.
541  \param segments The output segments indexer.
542  \return true if OK, false on errors.
543  */
544  bool initializeSegments( SegmenterIdsManager& segmenterIdsManager,
545  const te::rst::Raster& inputRaster,
546  const std::vector< unsigned int >& inputRasterBands,
547  const std::vector< double >& inputRasterGains,
548  const std::vector< double >& inputRasterOffsets,
549  SegmentsIndexer& segments );
550 
551  /*!
552  \brief Merge closest segments.
553  \param similarityThreshold The minimum similarity value used
554  when deciding when to merge two segments.
555  \param segmenterIdsManager A segments ids manager to acquire
556  unique segments ids.
557  \param merger The merger instance to use.
558  \param enablelocalMutualBestFitting If enabled, a merge only occurs between two segments if the minimum dissimilarity criteria is best fulfilled mutually.
559  \param segsIndexer Segmenters indexer.
560  \return The number of merged segments.
561  */
562  unsigned int mergeSegments(
563  const double similarityThreshold,
564  SegmenterIdsManager& segmenterIdsManager,
565  Merger& merger,
566  const bool enablelocalMutualBestFitting,
567  SegmentsIndexer& segsIndexer );
568 
569  /*!
570  \brief Merge only small segments to their closest segment.
571  \param minSegmentSize The minimum segment size (pixels)
572  \param similarityThreshold The minimum similarity value used
573  when deciding when to merge two segments.
574  \param segmenterIdsManager A segments ids manager to acquire
575  unique segments ids.
576  \param merger The merger instance to use.
577  \param segsIndexer Segmenters indexer.
578  \return The number of merged segments.
579  */
580  unsigned int mergeSmallSegments(
581  const unsigned int minSegmentSize,
582  SegmenterIdsManager& segmenterIdsManager,
583  Merger& merger,
584  SegmentsIndexer& segsIndexer );
585 
586  /*!
587  \brief Export the segments IDs to a tif file.
588  \param segmentsIds The output segment ids container.
589  \param normto8bits If true, a 8 bits file will be generated.
590  \param fileName The output tif file name.
591  */
592  void exportSegs2Tif( const SegmentsIdsMatrixT& segmentsIds,
593  bool normto8bits, const std::string& fileName );
594 
595  /*!
596  \brief Returns the count of points from region 1 (with ID1) touching the region 2 (with ID2).
597  \param segsIds The segment ids container.
598  \param xStart The upper left X of the bounding box surrounding both regions.
599  \param yStart The upper left Y of the bounding box surrounding both regions.
600  \param xBound The lower right X bound of the bounding box surrounding both regions.
601  \param yBound The lower right Y bound of the bounding box surrounding both regions.
602  \param id1 Region 1 ID.
603  \param id2 Region 2 ID.
604  \param edgeLength1 The touching edge length for the region 1.
605  \param edgeLength2 The touching edge length for the region 2.
606  \return Returns the count of points from region 1 (with ID1) touching the region 2 (with ID2).
607  */
608  static void getTouchingEdgeLength( const SegmentsIdsMatrixT& segsIds,
609  const unsigned int& xStart, const unsigned int& yStart,
610  const unsigned int& xBound, const unsigned int& yBound,
613  unsigned int& edgeLength1,
614  unsigned int& edgeLength2 );
615  };
616 
617  /*!
618  \class SegmenterRegionGrowingStrategyFactory
619  \brief Raster region growing segmenter strategy factory.
620  \note Factory key: RegionGrowing
621  */
624  {
625  public:
626 
628 
630 
631  //overload
632  te::rp::SegmenterStrategy* build();
633 
634  };
635 
636  } // end namespace rp
637 } // end namespace te
638 
639 #endif // __TERRALIB_RP_INTERNAL_ALGORITHM_H
640 
Generic template matrix.
double m_compactnessWeight
The weight given to the compactness component, deafult:0.5, valid range: [0,1].
std::vector< double > m_allSegsStdDevGain
The gains applied to normalize the standard deviation value.
Raster segmenter strategy parameters base class.
SegmentFeatures(const SegmenterRegionGrowingStrategy::SegmentFeatures &)
Raster region growing segmenter strategy.
unsigned int m_xBound
Segment lower bound X coordinate box over the label image.
Segmenter segments block description class.
double m_compactnessWeight
The weight given to the compactness component, deafult:0.5, valid range: [0,1].
SegmenterSegmentsBlock::SegmentIdDataType m_id
Segment ID.
double m_allSegsSmoothnessGain
The gains applied to normalize the smoothness value.
std::vector< double > m_squaresSum
The sum of squares of pixel values (for each band).
double m_colorWeight
The weight given to the color component, deafult:0.5, valid range: [0,1].
std::vector< double > m_allSegsStdDevOffsets
The offsets applied to normalize the standard deviation value.
SegmenterSegmentsPool m_segmentsPool
A pool of segments that can be reused on each strategy execution.
std::list< Segment * > m_neighborSegments
Neighborhood segments.
unsigned int m_yStart
Segment upper Y coordinate box over the label image.
double m_allSegsCompactnessOffset
The offsets applied to normalize the compactness value.
Segmenter segments IDs manager.
unsigned int m_segmentsSimIncreaseSteps
The maximum number of steps to increment the similarity threshold value for the cases where no segmen...
SegmenterRegionGrowingStrategy::SegmentFeatures * getFeatures()
Returns the current segment internal features.
Segmenter segment base class.
The Baatz based features will be used - Reference: Baatz, M.; Schape, A. Multiresolution segmentation...
SegmenterRegionGrowingStrategy::SegmentFeatures * getFeatures()
Returns the current segment internal features.
Segmenter segment base class.
bool m_isInitialized
true if this instance is initialized.
double m_colorWeight
The weight given to the color component, deafult:0.5, valid range: [0,1].
const SegmenterRegionGrowingStrategy::SegmentsIndexer & m_segments
A reference to an external valid structure where each all segments are indexed.
Raster segmenter strategy base class.
double m_allSegsSmoothnessOffset
The offsets applied to normalize the smoothness value.
std::vector< double > m_stdDev
Standard deviation of segment pixel velues.
Raster segmenter strategy factory base class.
double m_allSegsCompactnessGain
The gains applied to normalize the compactness value.
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...
unsigned int m_minSegmentSize
A positive minimum segment size (pixels number - default: 100).
Raster segmenter strategy base class.
Configuration flags for the Raster Processing module of TerraLib.
SegmentFeaturesType m_segmentFeatures
What segment features will be used on the segmentation process (default:InvalidFeaturesType).
SegmentsIdsMatrixT m_segmentsIdsMatrix
A internal segments IDs matrix that can be reused on each strategy execution.
SegmenterRegionGrowingStrategy::SegmentFeatures const * getFeatures() const
Returns the current segment internal features.
A generic template matrix.
Definition: Matrix.h:50
double m_segmentsSimilarityThreshold
Segments similarity treshold - Segments with similarity values below this value will be merged; valid...
SegmenterRegionGrowingStrategy::SegmentFeatures const * getFeatures() const
Returns the current segment internal features.
const SegmentsIdsMatrixT & m_segmentsIds
A reference to an external valid structure where each all segments IDs are stored.
An abstract class for raster data strucutures.
Definition: Raster.h:70
std::vector< double > m_bandsWeights
A vector where each bands weight are stored.
Raster segmenter strategy factory base class.
std::vector< double > m_means
Segment mean values (for each band), normalized between 0 and 1.
The mean of segments pixel values will be used - Reference: S. A. Bins, L. M. G. Fonseca, G. J. Erthal e F. M. Ii, &quot;Satellite Imagery segmentation: a region growing approach&quot;, VIII Simposio Brasileiro de Sensoriamento Remoto, Salvador, BA, 14-19 abril 1996.
SegmenterRegionGrowingStrategy::Parameters m_parameters
Internal execution parameters.
Raster region growing segmenter strategy factory.
unsigned int m_xStart
Segment left X coordinate box over the label image.
Segments block.
#define TERPEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:91
unsigned int m_yBound
Segment lower bound Y coordinate box over the label image.