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) 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"
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  /*! \enum SegmentFeaturesType Segment features types. */
64  {
65  InvalidFeaturesType, //!< Invalid features type.
66  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.
67  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.
68  };
69 
70  unsigned int m_minSegmentSize; //!< A positive minimum segment size (pixels number - default: 100).
71 
72  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.1 ).
73 
74  SegmentFeaturesType m_segmentFeatures; //!< What segment features will be used on the segmentation process (default:InvalidFeaturesType).
75 
76  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.
77 
78  double m_colorWeight; //!< The weight given to the color component, deafult:0.75, valid range: [0,1].
79 
80  double m_compactnessWeight; //!< The weight given to the compactness component, deafult:0.5, valid range: [0,1].
81 
82  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.
83 
84  Parameters();
85 
86  ~Parameters();
87 
88  //overload
89  const Parameters& operator=( const Parameters& params );
90 
91  //overload
92  void reset() throw( te::rp::Exception );
93 
94  //overload
95  AbstractParameters* clone() const;
96  };
97 
99 
101 
102  //overload
103  bool initialize(
104  SegmenterStrategyParameters const* const strategyParams )
105  throw( te::rp::Exception );
106 
107  //overload
108  void reset();
109 
110  //overload
111  bool execute(
112  SegmenterIdsManager& segmenterIdsManager,
113  const te::rst::Raster& inputRaster,
114  const std::vector< unsigned int >& inputRasterBands,
115  const std::vector< double >& inputRasterGains,
116  const std::vector< double >& inputRasterOffsets,
117  te::rst::Raster& outputRaster,
118  const unsigned int outputRasterBand,
119  const bool enableProgressInterface ) throw( te::rp::Exception );
120 
121  //overload
122  double getMemUsageEstimation( const unsigned int bandsToProcess,
123  const unsigned int pixelsNumber ) const;
124 
125  //overload
126  unsigned int getOptimalBlocksOverlapSize() const;
127 
128  protected :
129 
130  /*!
131  \brief Internal segments ids matrix type definition.
132  */
133  typedef Matrix< SegmenterSegmentsBlock::SegmentIdDataType >
135 
136  /*!
137  \class Merger
138  \brief Segments merger
139  */
141  {
142  public:
143 
144  virtual ~Merger();
145 
146  /*!
147  \brief Returns a dimilarity index between this and the other segment.
148  \param segment1Ptr A pointer to the first segment.
149  \param segment2Ptr A pointer to the second segment.
150  \param mergePreviewSegPtr A pointer to a valid segment where the merged features values will be stored (when aplicable).
151  \return A similarity index between this and the other segment ( normalized between 0 and 1 ).
152  */
153  virtual SegmenterRegionGrowingSegment::FeatureType getDissimilarity(
154  SegmenterRegionGrowingSegment const * const segment1Ptr,
155  SegmenterRegionGrowingSegment const * const segment2Ptr,
156  SegmenterRegionGrowingSegment * const mergePreviewSegPtr ) const = 0;
157 
158  /*!
159  \brief Merge specific segment features from both segments into the first segment.
160  \param segment1Ptr The first segment.
161  \param segment2Ptr A pointer to the second segment.
162  \param mergePreviewSegPtr A pointer to a valid segment where the merged features values were be stored by calling getDissimilarityIndex (when aplicable).
163  */
164  virtual void mergeFeatures(
165  SegmenterRegionGrowingSegment * const segment1Ptr,
166  SegmenterRegionGrowingSegment const * const segment2Ptr,
167  SegmenterRegionGrowingSegment const * const mergePreviewSegPtr ) const = 0;
168 
169  /*!
170  \brief Update the internal state.
171  */
172  virtual void update() = 0;
173 
174  protected :
175 
176  Merger();
177 
178  private :
179 
180  Merger( const Merger& ) {};
181 
182  const Merger& operator=( const Merger& other ) { return other; };
183  };
184 
185  /*!
186  \class MeanMerger
187  \brief Mean based Segments merger
188  */
190  {
191  public:
192 
193  MeanMerger( const unsigned int featuresNumber );
194 
195  ~MeanMerger();
196 
197  //overload
199  SegmenterRegionGrowingSegment const * const segment1Ptr,
200  SegmenterRegionGrowingSegment const * const segment2Ptr,
201  SegmenterRegionGrowingSegment * const mergePreviewSegPtr ) const;
202 
203  //overload
204  void mergeFeatures(
205  SegmenterRegionGrowingSegment * const segment1Ptr,
206  SegmenterRegionGrowingSegment const * const segment2Ptr,
207  SegmenterRegionGrowingSegment const * const mergePreviewSegPtr ) const;
208 
209  //overload
210  void update() {};
211 
212  protected :
213 
214  unsigned int m_featuresNumber; //!< The number of features (bands).
215 
216  // variables used by the method getDissimilarity
219  mutable unsigned int m_getDissimilarity_meansIdx;
220  };
221 
222  /*!
223  \class BaatzMerger
224  \brief Baatz based Segments merger
225  */
227  {
228  public:
229 
230  /*!
231  \brief Default constructor.
232  \param bandsWeights A reference to an external valid structure where each bands weight are stored.
233  \param segmentsIds //!< A reference to an external valid structure where all segments IDs are stored.
234  \param segmentsMatrix //!< A reference to an external valid segments matrix.
235  \param colorWeight //!< The weight given to the color component, deafult:0.5, valid range: [0,1].
236  \param compactnessWeight //!< The weight given to the compactness component, deafult:0.5, valid range: [0,1].
237  */
238  BaatzMerger( const double& colorWeight, const double& compactnessWeight,
239  const std::vector< double >& bandsWeights,
240  const SegmentsIdsMatrixT& segmentsIds,
241  Matrix< SegmenterRegionGrowingSegment >& segmentsMatrix );
242 
243  ~BaatzMerger();
244 
245  //overload
247  SegmenterRegionGrowingSegment const * const segment1Ptr,
248  SegmenterRegionGrowingSegment const * const segment2Ptr,
249  SegmenterRegionGrowingSegment * const mergePreviewSegPtr ) const;
250 
251  //overload
252  void mergeFeatures(
253  SegmenterRegionGrowingSegment * const segment1Ptr,
254  SegmenterRegionGrowingSegment const * const segment2Ptr,
255  SegmenterRegionGrowingSegment const * const mergePreviewSegPtr ) const;
256 
257  //overload
258  void update();
259 
260  protected :
261 
262  const SegmentsIdsMatrixT& m_segmentsIds; //!< A reference to an external valid structure where each all segments IDs are stored.
263 
264  Matrix< SegmenterRegionGrowingSegment >& m_segmentsMatrix; //!< A reference to an external valid segments matrix..
265 
266  unsigned int m_bandsNumber; //!< The number of features (bands).
267 
268  SegmenterRegionGrowingSegment::FeatureType m_allSegsCompactnessOffset; //!< The offsets applied to normalize the compactness value.
269 
270  SegmenterRegionGrowingSegment::FeatureType m_allSegsCompactnessGain; //!< The gains applied to normalize the compactness value.
271 
272  SegmenterRegionGrowingSegment::FeatureType m_allSegsSmoothnessOffset; //!< The offsets applied to normalize the smoothness value.
273 
274  SegmenterRegionGrowingSegment::FeatureType m_allSegsSmoothnessGain; //!< The gains applied to normalize the smoothness value.
275 
276  SegmenterRegionGrowingSegment::FeatureType m_colorWeight; //!< The weight given to the color component, deafult:0.5, valid range: [0,1].
277 
278  SegmenterRegionGrowingSegment::FeatureType m_compactnessWeight; //!< The weight given to the compactness component, deafult:0.5, valid range: [0,1].
279 
280  std::vector< SegmenterRegionGrowingSegment::FeatureType > m_bandsWeights; //!< A vector where each bands weight are stored.
281  };
282 
283  /*!
284  \brief true if this instance is initialized.
285  */
287 
288  /*!
289  \brief Internal execution parameters.
290  */
292 
293  /*! \brief A pool of segments that can be reused on each strategy execution. */
295 
296  /*! \brief A internal segments IDs matrix that can be reused on each strategy execution. */
298 
299  /*!
300  \brief Initialize the segment objects container and the segment
301  IDs container.
302  \param segmenterIdsManager A segments ids manager to acquire
303  unique segments ids.
304  \param inputRaster The input raster.
305  \param inputRasterBands Input raster bands to use.
306  \param segmentsIds The output segment ids container.
307  \return true if OK, false on errors.
308  */
309  bool initializeSegments( SegmenterIdsManager& segmenterIdsManager,
310  const te::rst::Raster& inputRaster,
311  const std::vector< unsigned int >& inputRasterBands,
312  const std::vector< double >& inputRasterGains,
313  const std::vector< double >& inputRasterOffsets );
314 
315  /*!
316  \brief Merge closest segments.
317  \param disimilarityThreshold The maximum similarity value allowed when deciding when to merge two segments.
318  \param segmenterIdsManager A segments ids manager to acquire unique segments ids.
319  \param merger The merger instance to use.
320  \param enablelocalMutualBestFitting If enabled, a merge only occurs between two segments if the minimum dissimilarity criteria is best fulfilled mutually.
321  \param auxSeg1Ptr A pointer to a valid auxiliar segment that will be used by this method.
322  \param auxSeg2Ptr A pointer to a valid auxiliar segment that will be used by this method.
323  \param auxSeg3Ptr A pointer to a valid auxiliar segment that will be used by this method.
324  \return The number of merged segments.
325  */
326  unsigned int mergeSegments(
327  const SegmenterRegionGrowingSegment::FeatureType disimilarityThreshold,
328  SegmenterIdsManager& segmenterIdsManager,
329  Merger& merger,
330  const bool enablelocalMutualBestFitting,
331  SegmenterRegionGrowingSegment* auxSeg1Ptr,
332  SegmenterRegionGrowingSegment* auxSeg2Ptr,
333  SegmenterRegionGrowingSegment* auxSeg3Ptr);
334 
335  /*!
336  \brief Merge only small segments to their closest segment.
337  \param minSegmentSize The minimum segment size (pixels)
338  \param similarityThreshold The minimum similarity value used
339  when deciding when to merge two segments.
340  \param segmenterIdsManager A segments ids manager to acquire
341  unique segments ids.
342  \param merger The merger instance to use.
343  \param auxSeg1Ptr A pointer to a valid auxiliar segment that will be used by this method.
344  \param auxSeg2Ptr A pointer to a valid auxiliar segment that will be used by this method.
345  \return The number of merged segments.
346  */
347  unsigned int mergeSmallSegments(
348  const unsigned int minSegmentSize,
349  SegmenterIdsManager& segmenterIdsManager,
350  Merger& merger,
351  SegmenterRegionGrowingSegment* auxSeg1Ptr,
352  SegmenterRegionGrowingSegment* auxSeg2Ptr);
353 
354  /*!
355  \brief Export the segments IDs to a tif file.
356  \param segmentsIds The output segment ids container.
357  \param normto8bits If true, a 8 bits file will be generated.
358  \param fileName The output tif file name.
359  */
360  void exportSegs2Tif( const SegmentsIdsMatrixT& segmentsIds,
361  bool normto8bits, const std::string& fileName );
362 
363  /*!
364  \brief Returns the count of points from region 1 (with ID1) touching the region 2 (with ID2).
365  \param segsIds The segment ids container.
366  \param xStart The upper left X of the bounding box surrounding both regions.
367  \param yStart The upper left Y of the bounding box surrounding both regions.
368  \param xBound The lower right X bound of the bounding box surrounding both regions.
369  \param yBound The lower right Y bound of the bounding box surrounding both regions.
370  \param id1 Region 1 ID.
371  \param id2 Region 2 ID.
372  \param edgeLength1 The touching edge length for the region 1.
373  \param edgeLength2 The touching edge length for the region 2.
374  \return Returns the count of points from region 1 (with ID1) touching the region 2 (with ID2).
375  */
376  static void getTouchingEdgeLength( const SegmentsIdsMatrixT& segsIds,
377  const unsigned int& xStart, const unsigned int& yStart,
378  const unsigned int& xBound, const unsigned int& yBound,
381  unsigned int& edgeLength1,
382  unsigned int& edgeLength2 );
383  };
384 
385  /*!
386  \class SegmenterRegionGrowingStrategyFactory
387  \brief Raster region growing segmenter strategy factory.
388  \note Factory key: RegionGrowing
389  */
392  {
393  public:
394 
396 
398 
399  //overload
400  te::rp::SegmenterStrategy* build();
401 
402  };
403 
404  } // end namespace rp
405 } // end namespace te
406 
407 #endif // __TERRALIB_RP_INTERNAL_ALGORITHM_H
408 
Segmenter segments IDs manager.
Raster segmenter strategy factory base class.
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.
std::vector< SegmenterRegionGrowingSegment::FeatureType > m_bandsWeights
A vector where each bands weight are stored.
SegmenterRegionGrowingStrategy::Parameters m_parameters
Internal execution parameters.
SegmenterRegionGrowingSegment::FeatureType m_getDissimilarity_dissValue
SegmentsIdsMatrixT m_segmentsIdsMatrix
A internal segments IDs matrix that can be reused on each strategy execution.
SegmenterRegionGrowingSegment::FeatureType m_allSegsCompactnessOffset
The offsets applied to normalize the compactness value.
Matrix< SegmenterRegionGrowingSegment > & m_segmentsMatrix
A reference to an external valid segments matrix..
Raster region growing segmenter strategy factory.
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...
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
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:111
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 - ...
Raster segmenter strategy base class.
Configuration flags for the Raster Processing module of TerraLib.
unsigned int m_bandsNumber
The number of features (bands).
Segments block.
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.75, 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.
double m_compactnessWeight
The weight given to the compactness component, deafult:0.5, valid range: [0,1].
SegmenterRegionGrowingSegment::FeatureType m_allSegsSmoothnessOffset
The offsets applied to normalize the smoothness value.
const SegmentsIdsMatrixT & m_segmentsIds
A reference to an external valid structure where each all segments IDs are stored.