All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties 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_seg
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  bool m_enableRasterCache; //!< Enable/Disable the use of raster data cache (default:true).
103 
104  InputParameters();
105 
106  InputParameters( const InputParameters& other );
107 
108  ~InputParameters();
109 
110  /*!
111  \brief Set specific segmenter strategy parameters.
112  \param params The specific segmenter strategy parameters.
113  */
114  void setSegStrategyParams(
115  const SegmenterStrategyParameters& segStratParams );
116 
117  /*!
118  \brief Returns a pointer to the internal specific segmenter strategy parameters.
119  \return A pointer to the internal specific segmenter strategy parameters, or
120  null if no parameters are present.
121  */
122  SegmenterStrategyParameters const* getSegStrategyParams() const;
123 
124  //overload
125  void reset() throw( te::rp::Exception );
126 
127  //overload
128  const Segmenter::InputParameters& operator=( const Segmenter::InputParameters& params );
129 
130  //overload
131  AbstractParameters* clone() const;
132 
133  protected :
134 
135  /*!
136  \brief A pointer to the internal specific segmenter strategy parameters or
137  NULL if no parameters are present.
138  */
139  SegmenterStrategyParameters* m_segStratParamsPtr;
140  };
141 
142  /*!
143  \class OutputParameters
144  \brief Segmenter Output Parameters
145  */
147  {
148  public:
149 
150  std::string m_rType; //!< Output raster data source type (as described in te::raster::RasterFactory ).
151 
152  std::map< std::string, std::string > m_rInfo; //!< The necessary information to create the raster (as described in te::raster::RasterFactory).
153 
154  std::auto_ptr< te::rst::Raster > m_outputRasterPtr; //!< A pointer the ge generated output raster (label image).
155 
157 
158  OutputParameters( const OutputParameters& other );
159 
160  ~OutputParameters();
161 
162  //overload
163  void reset() throw( te::rp::Exception );
164 
165  //overload
166  const Segmenter::OutputParameters& operator=( const Segmenter::OutputParameters& params );
167 
168  //overload
169  AbstractParameters* clone() const;
170  };
171 
172  Segmenter();
173 
174  ~Segmenter();
175 
176  //overload
177  bool execute( AlgorithmOutputParameters& outputParams ) throw( te::rp::Exception );
178 
179  //overload
180  void reset() throw( te::rp::Exception );
181 
182  //overload
183  bool initialize( const AlgorithmInputParameters& inputParams )
184  throw( te::rp::Exception );
185 
186  bool isInitialized() const;
187 
188  protected :
189 
190  /*! Segments blocks matrix type definition */
192 
193  /*!
194  \brief The parameters passed to the Segmenter::segmenterthreadEntry method.
195  */
197  {
198  public :
199  //! A pointer to the global segmenter input execution parameters (default:0). */
201 
202  //! A pointer to the global segmenter input execution parameters (default:0). */
204 
205  //! Pointer to the segments blocks matrix (default:0)*/
206  SegmentsBlocksMatrixT* m_segsBlocksMatrixPtr;
207 
208  //! Pointer to a general global mutex (default:0)*/
209  boost::mutex* m_generalMutexPtr;
210 
211  //! Pointer to the mutex used when accessing the input raster (default:0)*/
212  boost::mutex* m_inputRasterIOMutexPtr;
213 
214  //! Pointer to the mutex used when accessing the output raster (default:0)*/
216 
217  //! Pointer to the mutex used by the block processed signal (default:0)*/
219 
220  //! Pointer to the abort segmentation flag (default:0)*/
222 
223  //! Pointer to the segments Ids manager - (default 0) */
225 
226  //! Pointer to a signal to be emited when a segments block was processed (default:0)*/
227  boost::condition_variable* m_blockProcessedSignalPtr;
228 
229  //! Pointer to the running threads counter - default 0) */
230  unsigned int volatile* m_runningThreadsCounterPtr;
231 
232  //! Pointer to a vector of input raster bands gain values */
233  std::vector< double > const * m_inputRasterGainsPtr;
234 
235  //! Pointer to a vector of input raster bands offset values */
236  std::vector< double > const * m_inputRasterOffsetsPtr;
237 
238  //! Enable/Disable the segmentation strategy to use its own progress interface (default:false). */
240 
241  //! A pointer to an active task progress tha should be pulsed for each processed block or a null pointer (default:null). */
243 
244  SegmenterThreadEntryParams();
245 
246  ~SegmenterThreadEntryParams();
247  };
248 
249  bool m_instanceInitialized; //"< Is this instance already initialized ?
250 
251  Segmenter::InputParameters m_inputParameters; //!< Segmenter execution parameters.
252 
253  /*!
254  \brief Calc the best sub-image block size for each thread to
255  process.
256  \param totalImageLines The total original full image lines.
257  \param totalImageCols The total original full image columns.
258  \param minBlockPixels The minimun allowed pixels number for each block (expanded block).
259  \param maxBlockPixels The maximum allowed pixels number for each block (expanded block).
260  \param blocksHOverlapSize The blocks horizontal overlap size (number of columns).
261  \param blocksVOverlapSize The blocks vertical overlap size (number of rows).
262  \param blockWidth The calculated block width (non-expanded block).
263  \param blockHeight The calculated block height (non-expanded block).
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 minExapandedBlockPixels,
270  const unsigned int maxExapandedBlockPixels,
271  const unsigned int blocksHOverlapSize,
272  const unsigned int blocksVOverlapSize,
273  unsigned int& nonExpandedBlockWidth,
274  unsigned int& nonExpandedBlockHeight ) const;
275 
276  /*!
277  \brief Segmenter thread entry.
278  \param paramsPtr A pointer to the segmenter thread parameters.
279  */
280  static void segmenterThreadEntry(SegmenterThreadEntryParams* paramsPtr);
281 
282  /*!
283  \brief Generate the horizontal cutOff prifles for the entire image..
284  \param profileCenter The profile center line.
285  \param inRaster The input raster.
286  \param inRasterBands The input raster bands.
287  \param pixelNeighborhoodSize The pixel neighborhood size over the
288  line transverse to each tile line.
289  \param tileNeighborhoodSize The buffer size around each tile.
290  \param profileAntiSmoothingFactor A positive profile anti-smoothing factor.
291  \param profile The generated profile (each element is a inRaster
292  line index ).
293  \return true if OK, false on errors.
294  */
295  bool genImageHCutOffProfile( const unsigned int profileCenter,
296  const te::rst::Raster& inRaster,
297  const std::vector< unsigned int >& inRasterBands,
298  const unsigned int pixelNeighborhoodSize,
299  const unsigned int tileNeighborhoodSize,
300  const unsigned int profileAntiSmoothingFactor,
301  std::vector< unsigned int>& profile ) const;
302 
303  /*!
304  \brief Generate the vertical cutOff prifles for the entire image..
305  \param profileCenter The profile center column.
306  \param inRaster The input raster.
307  \param inRasterBands The input raster bands.
308  \param pixelNeighborhoodSize The pixel neighborhood size over the
309  line transverse to each tile line.
310  \param tileNeighborhoodSize The buffer size around each tile.
311  \param profileAntiSmoothingFactor A positive profile anti-smoothing factor.
312  \param profile The generated profile (each element is a inRaster
313  column index )
314  \return true if OK, false on errors.
315  */
316  bool genImageVCutOffProfile( const unsigned int profileCenter,
317  const te::rst::Raster& inRaster,
318  const std::vector< unsigned int >& inRasterBands,
319  const unsigned int pixelNeighborhoodSize,
320  const unsigned int tileNeighborhoodSize,
321  const unsigned int profileAntiSmoothingFactor,
322  std::vector< unsigned int>& profile ) const;
323 
324  /*!
325  \brief Update the block cutOff profiles using the full image profiles.
326  \param imageVerticalProfiles The generated horizontal profiles (empty profiles also will be accepted).
327  \param horizontalProfiles The generated vertical profiles (empty profiles also will be accepted).
328  \param segmentsBlock The block to update.
329  \return true if OK, false on errors.
330  */
331  bool updateBlockCutOffProfiles(
332  const std::vector< std::vector< unsigned int> >& imageHorizontalProfiles,
333  const std::vector< std::vector< unsigned int> >& imageVerticalProfiles,
334  SegmenterSegmentsBlock& segmentsBlock ) const;
335 
336  bool createCutOffLinesTiff(
337  const std::vector< std::vector< unsigned int> >& imageHorizontalProfiles,
338  const std::vector< unsigned int >& imageHorizontalProfilesCenterLines,
339  const std::vector< std::vector< unsigned int> >& imageVerticalProfiles,
340  const std::vector< unsigned int >& imageVerticalProfilesCenterLines,
341  const std::string& filename );
342  };
343 
344  } // end namespace rp
345 } // end namespace te
346 
347 #endif
348 
bool m_instanceInitialized
Definition: Segmenter.h:249
Segmenter segments IDs manager.
std::string m_strategyName
The segmenter strategy name see each te::rp::SegmenterStrategyFactory inherited classes documentation...
Definition: Segmenter.h:98
bool m_enableBlockMerging
If true, a block merging procedure will be performed (default:true).
Definition: Segmenter.h:94
Segmenter Output Parameters.
Definition: Segmenter.h:146
SegmenterIdsManager * m_segmentsIdsManagerPtr
Pointer to the segments Ids manager - (default 0) */.
Definition: Segmenter.h:224
std::vector< double > const * m_inputRasterGainsPtr
Pointer to a vector of input raster bands gain values */.
Definition: Segmenter.h:233
Raster segmentation.
Definition: Segmenter.h:72
boost::mutex * m_outputRasterIOMutexPtr
Pointer to the mutex used when accessing the output raster (default:0)*/.
Definition: Segmenter.h:215
boost::mutex * m_blockProcessedSignalMutexPtr
Pointer to the mutex used by the block processed signal (default:0)*/.
Definition: Segmenter.h:218
This class can be used to inform the progress of a task.
Definition: TaskProgress.h:53
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:154
std::string m_rType
Output raster data source type (as described in te::raster::RasterFactory ).
Definition: Segmenter.h:150
The parameters passed to the Segmenter::segmenterthreadEntry method.
Definition: Segmenter.h:196
std::vector< double > const * m_inputRasterOffsetsPtr
Pointer to a vector of input raster bands offset values */.
Definition: Segmenter.h:236
bool volatile * m_abortSegmentationFlagPtr
Pointer to the abort segmentation flag (default:0)*/.
Definition: Segmenter.h:221
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
bool m_enableProgress
Enable/Disable the progress interface (default:false).
Definition: Segmenter.h:100
Segments IDs manager.
Raster Processing algorithm base interface.
Definition: Algorithm.h:41
Segmenter::InputParameters m_inputParameters
Segmenter execution parameters.
Definition: Segmenter.h:251
An abstract class for raster data strucutures.
Definition: Raster.h:71
te::rst::Raster const * m_inputRasterPtr
Input raster.
Definition: Segmenter.h:84
#define TERPEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:111
Raster Processing algorithm base interface class.
Segmenter::OutputParameters * m_outputParametersPtr
A pointer to the global segmenter input execution parameters (default:0). */.
Definition: Segmenter.h:203
Segmenter Input Parameters.
Definition: Segmenter.h:80
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:152
bool m_enableRasterCache
Enable/Disable the use of raster data cache (default:true).
Definition: Segmenter.h:102
Configuration flags for the Raster Processing module of TerraLib.
boost::mutex * m_inputRasterIOMutexPtr
Pointer to the mutex used when accessing the input raster (default:0)*/.
Definition: Segmenter.h:212
bool m_enableStrategyProgress
Enable/Disable the segmentation strategy to use its own progress interface (default:false). */.
Definition: Segmenter.h:239
Segments block.
Raster segmenter strategy parameters base class.
A generic template matrix.
Definition: Matrix.h:51
SegmentsBlocksMatrixT * m_segsBlocksMatrixPtr
Pointer to the segments blocks matrix (default:0)*/.
Definition: Segmenter.h:206
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:227
boost::mutex * m_generalMutexPtr
Pointer to a general global mutex (default:0)*/.
Definition: Segmenter.h:209
bool m_enableThreadedProcessing
If true, threaded processing will be performed (best with multi-core or multi-processor systems (defa...
Definition: Segmenter.h:88
unsigned int m_maxSegThreads
The maximum number of concurrent segmenter threads (default:0 - automatically found).
Definition: Segmenter.h:90
Segmenter::InputParameters const * m_inputParametersPtr
A pointer to the global segmenter input execution parameters (default:0). */.
Definition: Segmenter.h:200
unsigned int volatile * m_runningThreadsCounterPtr
Pointer to the running threads counter - default 0) */.
Definition: Segmenter.h:230
std::vector< unsigned int > m_inputRasterBands
Bands to be processed from the input raster.
Definition: Segmenter.h:86
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:242
bool m_enableBlockProcessing
If true, the original raster will be splitted into small blocks, each one will be segmented independe...
Definition: Segmenter.h:92