Segmenter.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/Segmenter.h
22  \brief Raster segmentation.
23  */
24 
25 #ifndef __TERRALIB_RP_INTERNAL_SEGMENTER_H
26 #define __TERRALIB_RP_INTERNAL_SEGMENTER_H
27 
29 #include "SegmenterStrategy.h"
30 #include "SegmenterSegmentsBlock.h"
31 #include "SegmenterIdsManager.h"
32 #include "Algorithm.h"
33 #include "Matrix.h"
34 #include "Config.h"
35 #include "../raster/RasterSynchronizer.h"
36 #include "../common/progress/TaskProgress.h"
37 
38 #include <vector>
39 #include <string>
40 #include <map>
41 #include <memory>
42 
43 namespace te
44 {
45  namespace da
46  {
47  class DataSource;
48  }
49 
50  namespace rst
51  {
52  class Raster;
53  class Band;
54  }
55 
56  namespace rp
57  {
58  /*!
59  \class Segmenter
60  \brief Raster segmentation.
61  \details Raster segmentation following the choosed segmentation
62  strategy.
63  \note The created label image will be written to the raster instance
64  pointed by m_outRasterPtr (in this case the output band must also be
65  passed by m_outRasterBand ); or written to a new raster instance created
66  inside the given data source pointed by m_outDataSourcePtr (in this case
67  the data set name must be supplied - m_outDataSetName ).
68  \sa te::rp::Segmenter::Parameters For detailed parameters description.
69  \sa te::rp::SegmenterStrategy For detailed parameters description.
70 
71  \ingroup rp_seg
72  */
74  {
75  public:
76 
77  /*!
78  \class InputParameters
79  \brief Segmenter Input Parameters
80  */
82  {
83  public:
84 
85  te::rst::Raster const* m_inputRasterPtr; //!< Input raster.
86 
87  std::vector< unsigned int > m_inputRasterBands; //!< Bands to be processed from the input raster.
88 
89  std::vector< std::complex< double > > m_inputRasterNoDataValues; //!< A vector of values to be used as input raster no-data values or an empty vector indicating to use the default values from the input raster..
90 
91  bool m_enableThreadedProcessing; //!< If true, threaded processing will be performed (best with multi-core or multi-processor systems (default:true).
92 
93  unsigned int m_maxSegThreads; //!< The maximum number of concurrent segmenter threads (default:0 - automatically found).
94 
95  bool m_enableBlockProcessing; //!< If true, the original raster will be splitted into small blocks, each one will be segmented independently and the result will be merged (if possible) at the end (default:true).
96 
97  unsigned int m_maxBlockSize; //!< The input image will be split into blocks with this width for processing, this parameter tells the maximum block lateral size (width or height), the default: 0 - the size will be defined following the current system resources and physical processors number).
98 
99  unsigned char m_blocksOverlapPercent; //!< The percentage of blocks overlapped area (valid range:0-25, defaul:10).
100 
101  std::string m_strategyName; //!< The segmenter strategy name see each te::rp::SegmenterStrategyFactory inherited classes documentation for reference (default:RegionGrowingMean).
102 
103  bool m_enableProgress; //!< Enable/Disable the progress interface (default:false).
104 
105  bool m_enableRasterCache; //!< Enable/Disable the use of raster data cache (default:true).
106 
107  InputParameters();
108 
109  InputParameters( const InputParameters& other );
110 
111  ~InputParameters();
112 
113  /*!
114  \brief Set specific segmenter strategy parameters.
115  \param params The specific segmenter strategy parameters.
116  */
117  void setSegStrategyParams(
118  const SegmenterStrategyParameters& segStratParams );
119 
120  /*!
121  \brief Returns a pointer to the internal specific segmenter strategy parameters.
122  \return A pointer to the internal specific segmenter strategy parameters, or
123  null if no parameters are present.
124  */
125  SegmenterStrategyParameters const* getSegStrategyParams() const;
126 
127  //overload
128  void reset() throw( te::rp::Exception );
129 
130  //overload
131  const Segmenter::InputParameters& operator=( const Segmenter::InputParameters& params );
132 
133  //overload
134  AbstractParameters* clone() const;
135 
136  protected :
137 
138  /*!
139  \brief A pointer to the internal specific segmenter strategy parameters or
140  NULL if no parameters are present.
141  */
142  std::auto_ptr< SegmenterStrategyParameters > m_segStratParamsPtr;
143  };
144 
145  /*!
146  \class OutputParameters
147  \brief Segmenter Output Parameters
148  */
150  {
151  public:
152 
153  std::string m_rType; //!< Output raster data source type (as described in te::raster::RasterFactory ).
154 
155  std::map< std::string, std::string > m_rInfo; //!< The necessary information to create the raster (as described in te::raster::RasterFactory).
156 
157  std::auto_ptr< te::rst::Raster > m_outputRasterPtr; //!< A pointer the ge generated output raster (label image).
158 
160 
161  OutputParameters( const OutputParameters& other );
162 
163  ~OutputParameters();
164 
165  //overload
166  void reset() throw( te::rp::Exception );
167 
168  //overload
169  const Segmenter::OutputParameters& operator=( const Segmenter::OutputParameters& params );
170 
171  //overload
172  AbstractParameters* clone() const;
173  };
174 
175  Segmenter();
176 
177  ~Segmenter();
178 
179  //overload
180  bool execute( AlgorithmOutputParameters& outputParams ) throw( te::rp::Exception );
181 
182  //overload
183  void reset() throw( te::rp::Exception );
184 
185  //overload
186  bool initialize( const AlgorithmInputParameters& inputParams )
187  throw( te::rp::Exception );
188 
189  bool isInitialized() const;
190 
191  protected :
192 
193  /*! Segments blocks matrix type definition */
195 
196  /*!
197  \brief The parameters passed to the Segmenter::segmenterthreadEntry method.
198  */
200  {
201  public :
202  //! The global segmenter input execution parameters.
204 
205  //! A pointer to the global segmenter input execution parameters (default:0).
207 
208  //! Pointer to the segments blocks matrix (default:0).
209  SegmentsBlocksMatrixT* m_segsBlocksMatrixPtr;
210 
211  //! Pointer to a general global mutex (default:0).
212  boost::mutex* m_generalMutexPtr;
213 
214  //! Pointer to the input raster synchronizer (default:0).
216 
217  //! Pointer to the output raster synchronizer (default:0).
219 
220  //! Pointer to the mutex used by the block processed signal (default:0).
222 
223  //! Pointer to the abort segmentation flag (default:0).
225 
226  //! Pointer to the segments Ids manager - (default 0).
228 
229  //! Pointer to a signal to be emited when a segments block was processed (default:0).
230  boost::condition_variable* m_blockProcessedSignalPtr;
231 
232  //! Pointer to the running threads counter - default 0).
233  unsigned int volatile* m_runningThreadsCounterPtr;
234 
235  //! A vector of input raster bands minimum values.
236  std::vector< std::complex< double > > m_inputRasterBandMinValues;
237 
238  //! A vector of input raster bands maximum values.
239  std::vector< std::complex< double > > m_inputRasterBandMaxValues;
240 
241  //! A vector of values to be used as input raster no-data values.
242  std::vector< std::complex< double > > m_inputRasterNoDataValues;
243 
244  //! Enable/Disable the segmentation strategy to use its own progress interface (default:false).
246 
247  //! The maximum number of input raster cached blocks per-thread.
249 
250  SegmenterThreadEntryParams();
251 
252  ~SegmenterThreadEntryParams();
253  };
254 
255  bool m_instanceInitialized; //"< Is this instance already initialized ?
256 
257  Segmenter::InputParameters m_inputParameters; //!< Segmenter execution parameters.
258 
259  /*!
260  \brief Calc the best sub-image block size for each thread to
261  process.
262  \param minBlockPixels The minimun allowed pixels number for each block (expanded block).
263  \param maxBlockPixels The maximum allowed pixels number for each block (expanded block).
264  \param blocksHOverlapSize The blocks horizontal overlap size (number of columns).
265  \param blocksVOverlapSize The blocks vertical overlap size (number of rows).
266  \param nonExpandedBlockWidth The calculated non-expanded block width (non-expanded block).
267  \param nonExpandedBlockHeight The calculated non-expanded block height (non-expanded block).
268  \param expandedBlockWidth The calculated expanded block width (non-expanded block).
269  \param expandedBlockHeight The calculated expanded block height (non-expanded block).
270  \return true if OK, false on errors.
271  */
272  bool calcBestBlockSize(
273  const unsigned int minExapandedBlockPixels,
274  const unsigned int maxExapandedBlockPixels,
275  unsigned int& blocksHOverlapSize,
276  unsigned int& blocksVOverlapSize,
277  unsigned int& nonExpandedBlockWidth,
278  unsigned int& nonExpandedBlockHeight,
279  unsigned int& expandedBlockWidth,
280  unsigned int& expandedBlockHeight ) const;
281 
282  /*!
283  \brief Segmenter thread entry.
284  \param paramsPtr A pointer to the segmenter thread parameters.
285  */
286  static void segmenterThreadEntry(SegmenterThreadEntryParams* paramsPtr);
287 
288  /*!
289  \brief Generate the horizontal cutOff prifles for the entire image..
290  \param profileCenter The profile center line.
291  \param inRaster The input raster.
292  \param inRasterBands The input raster bands.
293  \param pixelNeighborhoodSize The pixel neighborhood size over the line transverse to each tile line.
294  \param tileNeighborhoodSize The buffer size around each tile.
295  \param profileAntiSmoothingFactor A positive profile anti-smoothing factor.
296  \param profile The generated profile (each element is a inRaster line index ).
297  \return true if OK, false on errors.
298  */
299  bool genImageHCutOffProfile( const unsigned int profileCenter,
300  const te::rst::Raster& inRaster,
301  const std::vector< unsigned int >& inRasterBands,
302  const unsigned int pixelNeighborhoodSize,
303  const unsigned int tileNeighborhoodSize,
304  const unsigned int profileAntiSmoothingFactor,
305  std::vector< unsigned int>& profile ) const;
306 
307  /*!
308  \brief Generate the vertical cutOff prifles for the entire image..
309  \param profileCenter The profile center column.
310  \param inRaster The input raster.
311  \param inRasterBands The input raster bands.
312  \param pixelNeighborhoodSize The pixel neighborhood size over the line transverse to each tile line.
313  \param tileNeighborhoodSize The buffer size around each tile.
314  \param profileAntiSmoothingFactor A positive profile anti-smoothing factor.
315  \param profile The generated profile (each element is a inRaster column index )
316  \return true if OK, false on errors.
317  */
318  bool genImageVCutOffProfile( const unsigned int profileCenter,
319  const te::rst::Raster& inRaster,
320  const std::vector< unsigned int >& inRasterBands,
321  const unsigned int pixelNeighborhoodSize,
322  const unsigned int tileNeighborhoodSize,
323  const unsigned int profileAntiSmoothingFactor,
324  std::vector< unsigned int>& profile ) const;
325 
326  /*!
327  \brief Update the block cutOff profiles using the full image profiles.
328  \param imageVerticalProfiles The generated horizontal profiles (empty profiles also will be accepted).
329  \param horizontalProfiles The generated vertical profiles (empty profiles also will be accepted).
330  \param segmentsBlock The block to update.
331  \return true if OK, false on errors.
332  */
333  bool updateBlockCutOffProfiles(
334  const std::vector< std::vector< unsigned int> >& imageHorizontalProfiles,
335  const std::vector< std::vector< unsigned int> >& imageVerticalProfiles,
336  SegmenterSegmentsBlock& segmentsBlock ) const;
337 
338  bool createCutOffLinesTiff(
339  const std::vector< std::vector< unsigned int> >& imageHorizontalProfiles,
340  const std::vector< unsigned int >& imageHorizontalProfilesCenterLines,
341  const std::vector< std::vector< unsigned int> >& imageVerticalProfiles,
342  const std::vector< unsigned int >& imageVerticalProfilesCenterLines,
343  const std::string& filename );
344  };
345 
346  } // end namespace rp
347 } // end namespace te
348 
349 #endif
350 
bool m_instanceInitialized
Definition: Segmenter.h:255
Segmenter segments IDs manager.
std::string m_strategyName
The segmenter strategy name see each te::rp::SegmenterStrategyFactory inherited classes documentation...
Definition: Segmenter.h:101
unsigned char m_blocksOverlapPercent
The percentage of blocks overlapped area (valid range:0-25, defaul:10).
Definition: Segmenter.h:99
std::vector< std::complex< double > > m_inputRasterNoDataValues
A vector of values to be used as input raster no-data values.
Definition: Segmenter.h:242
Segmenter Output Parameters.
Definition: Segmenter.h:149
SegmenterIdsManager * m_segmentsIdsManagerPtr
Pointer to the segments Ids manager - (default 0).
Definition: Segmenter.h:227
Raster segmentation.
Definition: Segmenter.h:73
Base exception class for plugin module.
Definition: Exception.h:42
boost::mutex * m_blockProcessedSignalMutexPtr
Pointer to the mutex used by the block processed signal (default:0).
Definition: Segmenter.h:221
Raster Processing algorithm output parameters base interface.
std::auto_ptr< te::rst::Raster > m_outputRasterPtr
A pointer the ge generated output raster (label image).
Definition: Segmenter.h:157
std::string m_rType
Output raster data source type (as described in te::raster::RasterFactory ).
Definition: Segmenter.h:153
The parameters passed to the Segmenter::segmenterthreadEntry method.
Definition: Segmenter.h:199
An access synchronizer to be used in SynchronizedRaster raster instances.
Raster Processing algorithm base interface class.
bool volatile * m_abortSegmentationFlagPtr
Pointer to the abort segmentation flag (default:0).
Definition: Segmenter.h:224
unsigned int m_maxBlockSize
The input image will be split into blocks with this width for processing, this parameter tells the ma...
Definition: Segmenter.h:97
bool m_enableProgress
Enable/Disable the progress interface (default:false).
Definition: Segmenter.h:103
Segments IDs manager.
Raster Processing algorithm base interface.
Definition: Algorithm.h:41
Segmenter::InputParameters m_inputParameters
Segmenter execution parameters.
Definition: Segmenter.h:257
std::vector< std::complex< double > > m_inputRasterNoDataValues
A vector of values to be used as input raster no-data values or an empty vector indicating to use the...
Definition: Segmenter.h:89
An abstract class for raster data strucutures.
Definition: Raster.h:71
te::rst::Raster const * m_inputRasterPtr
Input raster.
Definition: Segmenter.h:85
URI C++ Library.
#define TERPEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:139
te::rst::RasterSynchronizer * m_inputRasterSyncPtr
Pointer to the input raster synchronizer (default:0).
Definition: Segmenter.h:215
Segmenter::OutputParameters * m_outputParametersPtr
A pointer to the global segmenter input execution parameters (default:0).
Definition: Segmenter.h:206
unsigned int m_maxInputRasterCachedBlocks
The maximum number of input raster cached blocks per-thread.
Definition: Segmenter.h:248
Segmenter Input Parameters.
Definition: Segmenter.h:81
std::vector< std::complex< double > > m_inputRasterBandMaxValues
A vector of input raster bands maximum values.
Definition: Segmenter.h:239
Generic template matrix.
Raster segmenter strategy base class.
std::map< std::string, std::string > m_rInfo
The necessary information to create the raster (as described in te::raster::RasterFactory).
Definition: Segmenter.h:155
bool m_enableRasterCache
Enable/Disable the use of raster data cache (default:true).
Definition: Segmenter.h:105
Configuration flags for the Raster Processing module of TerraLib.
bool m_enableStrategyProgress
Enable/Disable the segmentation strategy to use its own progress interface (default:false).
Definition: Segmenter.h:245
Segments block.
Raster segmenter strategy parameters base class.
A generic template matrix.
Definition: Matrix.h:54
SegmentsBlocksMatrixT * m_segsBlocksMatrixPtr
Pointer to the segments blocks matrix (default:0).
Definition: Segmenter.h:209
Raster Processing algorithm input parameters base interface.
Segmenter segments block description class.
boost::condition_variable * m_blockProcessedSignalPtr
Pointer to a signal to be emited when a segments block was processed (default:0). ...
Definition: Segmenter.h:230
boost::mutex * m_generalMutexPtr
Pointer to a general global mutex (default:0).
Definition: Segmenter.h:212
te::rst::RasterSynchronizer * m_outputRasterSyncPtr
Pointer to the output raster synchronizer (default:0).
Definition: Segmenter.h:218
bool m_enableThreadedProcessing
If true, threaded processing will be performed (best with multi-core or multi-processor systems (defa...
Definition: Segmenter.h:91
unsigned int m_maxSegThreads
The maximum number of concurrent segmenter threads (default:0 - automatically found).
Definition: Segmenter.h:93
unsigned int volatile * m_runningThreadsCounterPtr
Pointer to the running threads counter - default 0).
Definition: Segmenter.h:233
std::vector< unsigned int > m_inputRasterBands
Bands to be processed from the input raster.
Definition: Segmenter.h:87
std::vector< std::complex< double > > m_inputRasterBandMinValues
A vector of input raster bands minimum values.
Definition: Segmenter.h:236
Segmenter::InputParameters m_inputParameters
The global segmenter input execution parameters.
Definition: Segmenter.h:203
bool m_enableBlockProcessing
If true, the original raster will be splitted into small blocks, each one will be segmented independe...
Definition: Segmenter.h:95