All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Segmenter.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/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 "../common/progress/TaskProgress.h"
36 
37 #include <vector>
38 #include <string>
39 #include <map>
40 #include <memory>
41 
42 namespace te
43 {
44  namespace da
45  {
46  class DataSource;
47  };
48 
49  namespace rst
50  {
51  class Raster;
52  class Band;
53  };
54 
55  namespace rp
56  {
57  /*!
58  \class Segmenter
59  \brief Raster segmentation.
60  \details Raster segmentation following the choosed segmentation
61  strategy.
62  \note The created label image will be written to the raster instance
63  pointed by m_outRasterPtr (in this case the output band must also be
64  passed by m_outRasterBand ); or written to a new raster instance created
65  inside the given data source pointed by m_outDataSourcePtr (in this case
66  the data set name must be supplied - m_outDataSetName ).
67  \sa te::rp::Segmenter::Parameters For detailed parameters description.
68  \sa te::rp::SegmenterStrategy For detailed parameters description.
69 
70  \ingroup rp
71  */
73  {
74  public:
75 
76  /*!
77  \class InputParameters
78  \brief Segmenter Input Parameters
79  */
81  {
82  public:
83 
84  te::rst::Raster const* m_inputRasterPtr; //!< Input raster.
85 
86  std::vector< unsigned int > m_inputRasterBands; //!< Bands to be processed from the input raster.
87 
88  bool m_enableThreadedProcessing; //!< If true, threaded processing will be performed (best with multi-core or multi-processor systems (default:true).
89 
90  unsigned int m_maxSegThreads; //!< The maximum number of concurrent segmenter threads (default:0 - automatically found).
91 
92  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).
93 
94  bool m_enableBlockMerging; //!< If true, a block merging procedure will be performed (default:true).
95 
96  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).
97 
98  std::string m_strategyName; //!< The segmenter strategy name see each te::rp::SegmenterStrategyFactory inherited classes documentation for reference.
99 
100  bool m_enableProgress; //!< Enable/Disable the progress interface (default:false).
101 
102  InputParameters();
103 
104  InputParameters( const InputParameters& other );
105 
106  ~InputParameters();
107 
108  /*!
109  \brief Set specific segmenter strategy parameters.
110  \param params The specific segmenter strategy parameters.
111  */
112  void setSegStrategyParams(
113  const SegmenterStrategyParameters& segStratParams );
114 
115  /*!
116  \brief Returns a pointer to the internal specific segmenter strategy parameters.
117  \return A pointer to the internal specific segmenter strategy parameters, or
118  null if no parameters are present.
119  */
120  SegmenterStrategyParameters const* getSegStrategyParams() const;
121 
122  //overload
123  void reset() throw( te::rp::Exception );
124 
125  //overload
126  const Segmenter::InputParameters& operator=( const Segmenter::InputParameters& params );
127 
128  //overload
129  AbstractParameters* clone() const;
130 
131  protected :
132 
133  /*!
134  \brief A pointer to the internal specific segmenter strategy parameters or
135  NULL if no parameters are present.
136  */
137  SegmenterStrategyParameters* m_segStratParamsPtr;
138  };
139 
140  /*!
141  \class OutputParameters
142  \brief Segmenter Output Parameters
143  */
145  {
146  public:
147 
148  std::string m_rType; //!< Output raster data source type (as described in te::raster::RasterFactory ).
149 
150  std::map< std::string, std::string > m_rInfo; //!< The necessary information to create the raster (as described in te::raster::RasterFactory).
151 
152  std::auto_ptr< te::rst::Raster > m_outputRasterPtr; //!< A pointer the ge generated output raster (label image).
153 
155 
156  OutputParameters( const OutputParameters& other );
157 
158  ~OutputParameters();
159 
160  //overload
161  void reset() throw( te::rp::Exception );
162 
163  //overload
164  const Segmenter::OutputParameters& operator=( const Segmenter::OutputParameters& params );
165 
166  //overload
167  AbstractParameters* clone() const;
168  };
169 
170  Segmenter();
171 
172  ~Segmenter();
173 
174  //overload
175  bool execute( AlgorithmOutputParameters& outputParams ) throw( te::rp::Exception );
176 
177  //overload
178  void reset() throw( te::rp::Exception );
179 
180  //overload
181  bool initialize( const AlgorithmInputParameters& inputParams )
182  throw( te::rp::Exception );
183 
184  bool isInitialized() const;
185 
186  protected :
187 
188  /*! Segments blocks matrix type definition */
190 
191  /*!
192  \brief The parameters passed to the Segmenter::segmenterthreadEntry method.
193  */
195  {
196  public :
197  //! A pointer to the global segmenter input execution parameters (default:0). */
199 
200  //! A pointer to the global segmenter input execution parameters (default:0). */
202 
203  //! Pointer to the segments blocks matrix (default:0)*/
204  SegmentsBlocksMatrixT* m_segsBlocksMatrixPtr;
205 
206  //! Pointer to a general global mutex (default:0)*/
207  boost::mutex* m_generalMutexPtr;
208 
209  //! Pointer to the mutex used when accessing the input raster (default:0)*/
210  boost::mutex* m_inputRasterIOMutexPtr;
211 
212  //! Pointer to the mutex used when accessing the output raster (default:0)*/
214 
215  //! Pointer to the mutex used by the block processed signal (default:0)*/
217 
218  //! Pointer to the abort segmentation flag (default:0)*/
220 
221  //! Pointer to the segments Ids manager - (default 0) */
223 
224  //! Pointer to a signal to be emited when a segments block was processed (default:0)*/
225  boost::condition_variable* m_blockProcessedSignalPtr;
226 
227  //! Pointer to the running threads counter - default 0) */
228  unsigned int volatile* m_runningThreadsCounterPtr;
229 
230  //! Pointer to a vector of input raster bands gain values */
231  std::vector< double > const * m_inputRasterGainsPtr;
232 
233  //! Pointer to a vector of input raster bands offset values */
234  std::vector< double > const * m_inputRasterOffsetsPtr;
235 
236  //! Enable/Disable the segmentation strategy to use its own progress interface (default:false). */
238 
239  //! A pointer to an active task progress tha should be pulsed for each processed block or a null pointer (default:null). */
241 
242  SegmenterThreadEntryParams();
243 
244  ~SegmenterThreadEntryParams();
245  };
246 
247  bool m_instanceInitialized; //"< Is this instance already initialized ?
248 
249  Segmenter::InputParameters m_inputParameters; //!< Segmenter execution parameters.
250 
251  /*!
252  \brief Calc the best sub-image block size for each thread to
253  process.
254  \param totalImageLines The total original full image lines.
255  \param totalImageCols The total original full image columns.
256  \param minBlockPixels The minimun allowed pixels number for each block (expanded block).
257  \param maxBlockPixels The maximum allowed pixels number for each block (expanded block).
258  \param blocksHOverlapSizePercent The blocks horizontal overlap size percentual related to the non-expanded block horizontal size - valid range[0,1].
259  \param blocksVOverlapSizePectent The blocks vertical overlap size percentual related to the non-expanded block vertial size - valid range[0,1].
260  \param blockWidth The calculated block width (non-expanded block).
261  \param blockHeight The calculated block height (non-expanded block).
262  \param blocksHOverlapSize The calculated block horizontal overlap size.
263  \param blocksVOverlapSize The calculated block vertical overlap size.
264  \return true if OK, false on errors.
265  */
266  bool calcBestBlockSize(
267  const unsigned int totalImageLines,
268  const unsigned int totalImageCols,
269  const unsigned int minBlockPixels,
270  const unsigned int maxBlockPixels,
271  const double blocksHOverlapSizePercent,
272  const double blocksVOverlapSizePectent,
273  unsigned int& blockWidth,
274  unsigned int& blockHeight,
275  unsigned int& blocksHOverlapSize,
276  unsigned int& blocksVOverlapSize ) const;
277 
278  /*!
279  \brief Segmenter thread entry.
280  \param paramsPtr A pointer to the segmenter thread parameters.
281  */
282  static void segmenterThreadEntry(SegmenterThreadEntryParams* paramsPtr);
283 
284  /*!
285  \brief Generate the horizontal cutOff prifles for the entire image..
286  \param profileCenter The profile center line.
287  \param inRaster The input raster.
288  \param inRasterBands The input raster bands.
289  \param pixelNeighborhoodSize The pixel neighborhood size over the
290  line transverse to each tile line.
291  \param tileNeighborhoodSize The buffer size around each tile.
292  \param profileAntiSmoothingFactor A positive profile anti-smoothing factor.
293  \param profile The generated profile (each element is a inRaster
294  line index ).
295  \return true if OK, false on errors.
296  */
297  bool genImageHCutOffProfile( const unsigned int profileCenter,
298  const te::rst::Raster& inRaster,
299  const std::vector< unsigned int >& inRasterBands,
300  const unsigned int pixelNeighborhoodSize,
301  const unsigned int tileNeighborhoodSize,
302  const unsigned int profileAntiSmoothingFactor,
303  std::vector< unsigned int>& profile ) const;
304 
305  /*!
306  \brief Generate the vertical cutOff prifles for the entire image..
307  \param profileCenter The profile center column.
308  \param inRaster The input raster.
309  \param inRasterBands The input raster bands.
310  \param pixelNeighborhoodSize The pixel neighborhood size over the
311  line transverse to each tile line.
312  \param tileNeighborhoodSize The buffer size around each tile.
313  \param profileAntiSmoothingFactor A positive profile anti-smoothing factor.
314  \param profile The generated profile (each element is a inRaster
315  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 
std::auto_ptr< te::rst::Raster > m_outputRasterPtr
A pointer the ge generated output raster (label image).
Definition: Segmenter.h:152
bool m_enableProgress
Enable/Disable the progress interface (default:false).
Definition: Segmenter.h:100
Generic template matrix.
std::string m_strategyName
The segmenter strategy name see each te::rp::SegmenterStrategyFactory inherited classes documentation...
Definition: Segmenter.h:98
Raster segmenter strategy parameters base class.
bool volatile * m_abortSegmentationFlagPtr
Pointer to the abort segmentation flag (default:0)*/.
Definition: Segmenter.h:219
std::map< std::string, std::string > m_rInfo
The necessary information to create the raster (as described in te::raster::RasterFactory).
Definition: Segmenter.h:150
Segments IDs manager.
Raster Processing algorithm base interface class.
Segmenter segments block description class.
Segmenter Input Parameters.
Definition: Segmenter.h:80
boost::mutex * m_outputRasterIOMutexPtr
Pointer to the mutex used when accessing the output raster (default:0)*/.
Definition: Segmenter.h:213
bool m_enableBlockMerging
If true, a block merging procedure will be performed (default:true).
Definition: Segmenter.h:94
The parameters passed to the Segmenter::segmenterthreadEntry method.
Definition: Segmenter.h:194
te::rst::Raster const * m_inputRasterPtr
Input raster.
Definition: Segmenter.h:84
Segmenter Output Parameters.
Definition: Segmenter.h:144
unsigned int m_maxSegThreads
The maximum number of concurrent segmenter threads (default:0 - automatically found).
Definition: Segmenter.h:90
std::vector< double > const * m_inputRasterGainsPtr
Pointer to a vector of input raster bands gain values */.
Definition: Segmenter.h:231
Raster Processing algorithm base interface.
Definition: Algorithm.h:42
bool m_enableBlockProcessing
If true, the original raster will be splitted into small blocks, each one will be segmented independe...
Definition: Segmenter.h:92
Segmenter::InputParameters m_inputParameters
Segmenter execution parameters.
Definition: Segmenter.h:249
std::vector< double > const * m_inputRasterOffsetsPtr
Pointer to a vector of input raster bands offset values */.
Definition: Segmenter.h:234
bool m_enableThreadedProcessing
If true, threaded processing will be performed (best with multi-core or multi-processor systems (defa...
Definition: Segmenter.h:88
Segmenter segments IDs manager.
SegmenterIdsManager * m_segmentsIdsManagerPtr
Pointer to the segments Ids manager - (default 0) */.
Definition: Segmenter.h:222
bool m_enableStrategyProgress
Enable/Disable the segmentation strategy to use its own progress interface (default:false). */.
Definition: Segmenter.h:237
Segmenter::OutputParameters * m_outputParametersPtr
A pointer to the global segmenter input execution parameters (default:0). */.
Definition: Segmenter.h:201
unsigned int volatile * m_runningThreadsCounterPtr
Pointer to the running threads counter - default 0) */.
Definition: Segmenter.h:228
boost::mutex * m_inputRasterIOMutexPtr
Pointer to the mutex used when accessing the input raster (default:0)*/.
Definition: Segmenter.h:210
SegmentsBlocksMatrixT * m_segsBlocksMatrixPtr
Pointer to the segments blocks matrix (default:0)*/.
Definition: Segmenter.h:204
Raster segmentation.
Definition: Segmenter.h:72
boost::mutex * m_generalMutexPtr
Pointer to a general global mutex (default:0)*/.
Definition: Segmenter.h:207
Raster segmenter strategy base class.
Configuration flags for the Raster Processing module of TerraLib.
A generic template matrix.
Definition: Matrix.h:50
std::vector< unsigned int > m_inputRasterBands
Bands to be processed from the input raster.
Definition: Segmenter.h:86
Segmenter::InputParameters const * m_inputParametersPtr
A pointer to the global segmenter input execution parameters (default:0). */.
Definition: Segmenter.h:198
Raster Processing algorithm output parameters base interface.
boost::mutex * m_blockProcessedSignalMutexPtr
Pointer to the mutex used by the block processed signal (default:0)*/.
Definition: Segmenter.h:216
bool m_instanceInitialized
Definition: Segmenter.h:247
An abstract class for raster data strucutures.
Definition: Raster.h:70
te::common::TaskProgress * m_progressPtr
A pointer to an active task progress tha should be pulsed for each processed block or a null pointer ...
Definition: Segmenter.h:240
This class can be used to inform the progress of a task.
Definition: TaskProgress.h:53
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:96
std::string m_rType
Output raster data source type (as described in te::raster::RasterFactory ).
Definition: Segmenter.h:148
boost::condition_variable * m_blockProcessedSignalPtr
Pointer to a signal to be emited when a segments block was processed (default:0)*/.
Definition: Segmenter.h:225
Segments block.
#define TERPEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:91
Raster Processing algorithm input parameters base interface.