All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Skeleton.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/Skeleton.h
22  \brief Creation of skeleton imagems.
23  */
24 
25 #ifndef __TERRALIB_RP_INTERNAL_SKELETON_H
26 #define __TERRALIB_RP_INTERNAL_SKELETON_H
27 
28 #include "Algorithm.h"
29 #include "Matrix.h"
30 #include "../raster/Raster.h"
31 #include "../raster/BandProperty.h"
32 #include "../raster/RasterFactory.h"
33 #include "../raster/Grid.h"
34 #include "../common/progress/TaskProgress.h"
35 
36 #include <boost/thread.hpp>
37 
38 #include <memory>
39 #include <vector>
40 #include <map>
41 #include <string>
42 
43 #include <climits>
44 #include <cfloat>
45 
46 namespace te
47 {
48  namespace rp
49  {
50  /*!
51  \class Skeleton
52 
53  \brief Creation of skeleton images.
54 
55  \details The skeleton of a region may be defined via the media axis transformation MAT
56  proposed by Blum [1967]. The MAT of a region R with border B is as follows:
57  For each point p in R, we find its closest neightbor in B. If p has more than one
58  such neighbor, it is said to belong to the medial axis (skeleton) of R.
59 
60  \note Reference: Blum, H. 1967. A transformation for extracting new descriptors of form.
61 
62  \note Reference: Normalized Gradient Vector Diffusion and Image Segmentation, Zeyun Yu, Chandrajit Bajaj.
63 
64  \note Reference: Image Segmentation Using Gradient Vector Diffusion and Region Merging, Zeyun Yu, Chandrajit Bajaj.
65 
66  \note Reference: A Segmentation-Free Approach for Skeletonization of Gray-Scale Images via Anisotropic Vector Diffusion, Zeyun Yu and Chandrajit Bajaj.
67  */
68  class TERPEXPORT Skeleton : public Algorithm
69  {
70  public:
71 
72  /*!
73  \class InputParameters
74  \brief Skeleton input parameters
75  */
77  {
78  public:
79 
80  te::rst::Raster const* m_inputRasterPtr; //!< Input raster.
81 
82  unsigned int m_inputRasterBand; //!< Bands to process from the input raster.
83 
84  te::rst::Raster const* m_inputMaskRasterPtr; //!< A pointer to an input raster (where pixels with zero velues will be ignored) or an null pointer if no input mask raster must be used.
85 
86  double m_diffusionThreshold; //!< A threshold over the residue from one iteration to another, if the residue drops below this value the process is finished - valid range: (0,1] positive values, lower values will cause more iterations to be performed.
87 
88  double m_diffusionRegularization; //!< A regularization parameter to control the variation from one iteration to the next one (higher values can reduce the number of iterations but can bring the system to an unstable state, valid range: (0,1] positive values ).
89 
90  unsigned int m_diffusionMaxIterations; //!< The maximum number of iterations to perform (zero: no iterations limit).
91 
92  bool m_enableMultiThread; //!< Enable (true) the use of threads.
93 
94  double m_skeletonThreshold; //!< A threshold to select those pixels as being part of the final skeleton - valid range [0,1].
95 
96  bool m_enableProgress; //!< Enable/Disable the progress interface (default:false).
97 
99 
101 
102  ~InputParameters();
103 
104  //overload
105  void reset() throw( te::rp::Exception );
106 
107  //overload
108  const InputParameters& operator=( const InputParameters& params );
109 
110  //overload
111  AbstractParameters* clone() const;
112  };
113 
114  /*!
115  \class OutputParameters
116  \brief Skeleton output parameters
117  */
119  {
120  public:
121 
122  std::string m_rType; //!< Output raster data source type (as described in te::raster::RasterFactory ).
123 
124  std::map< std::string, std::string > m_rInfo; //!< The necessary information to create the output rasters (as described in te::raster::RasterFactory).
125 
126  std::auto_ptr< te::rst::Raster > m_outputRasterPtr; //!< The generated output registered raster.
127 
129 
131 
132  ~OutputParameters();
133 
134  //overload
135  void reset() throw( te::rp::Exception );
136 
137  //overload
138  const OutputParameters& operator=( const OutputParameters& params );
139 
140  //overload
141  AbstractParameters* clone() const;
142  };
143 
144  Skeleton();
145 
146  ~Skeleton();
147 
148  //overload
149  bool execute( AlgorithmOutputParameters& outputParams ) throw( te::rp::Exception );
150 
151  //overload
152  void reset() throw( te::rp::Exception );
153 
154  //overload
155  bool initialize( const AlgorithmInputParameters& inputParams ) throw( te::rp::Exception );
156 
157  bool isInitialized() const;
158 
159  protected:
160 
161  /*!
162  \class ApplyRansacThreadEntryThreadParams
163 
164  \brief Parameters used by the GTFilter::applyRansacThreadEntry method.
165  */
167  {
168  public:
169 
170  te::rp::Matrix< double > const * m_initialXBufPtr; //!< A pointer to the input buffer initial X component.
171  te::rp::Matrix< double > const * m_initialYBufPtr; //!< A pointer to the input buffer initial Y component.
172  te::rp::Matrix< double > const * m_inputMagBufPtr; //!< A pointer to the input magnitude buffer.
173  te::rp::Matrix< double > const * m_inputBufXPtr; //!< A pointer to the input buffer X component.
174  te::rp::Matrix< double > const * m_inputBufYPtr; //!< A pointer to the input buffer Y component.
175  te::rp::Matrix< double > * m_outputMagBufPtr; //!< A pointer to the output magnitude buffer.
176  te::rp::Matrix< double > * m_outputBufXPtr; //!< A pointer to the output buffer X component.
177  te::rp::Matrix< double > * m_outputBufYPtr; //!< A pointer to the output buffer X component.
178  boost::mutex* m_mutexPtr; //!< A pointer to the sync mutex.
179  unsigned int m_firstRowIdx; //!< First row to process.
180  unsigned int m_lastRowIdx; //!< Last row to process.
181  double* m_currentIterationResiduePtr; //!< A pointer the the current iteration residue;
182  double m_diffusionRegularization; //!< The diffusion regularization parameter.
183 
185 
186  ApplyVecDiffusionThreadParams( const ApplyVecDiffusionThreadParams& other )
187  {
188  operator=( other );
189  };
190 
192 
193  ApplyVecDiffusionThreadParams& operator=( const ApplyVecDiffusionThreadParams& other )
194  {
195  m_initialXBufPtr = other.m_initialXBufPtr;
196  m_initialYBufPtr = other.m_initialYBufPtr;
197  m_inputMagBufPtr = other.m_inputMagBufPtr;
198  m_inputBufXPtr = other.m_inputBufXPtr;
199  m_inputBufYPtr = other.m_inputBufYPtr;
200  m_outputMagBufPtr = other.m_outputMagBufPtr;
201  m_outputBufXPtr = other.m_outputBufXPtr;
202  m_outputBufYPtr = other.m_outputBufYPtr;
203  m_mutexPtr = other.m_mutexPtr;
204  m_firstRowIdx = other.m_firstRowIdx;
205  m_lastRowIdx = other.m_lastRowIdx;
206  m_currentIterationResiduePtr = other.m_currentIterationResiduePtr;
207  m_diffusionRegularization = other.m_diffusionRegularization;
208 
209  return *this;
210  };
211  };
212 
213  Skeleton::InputParameters m_inputParameters; //!< Input execution parameters.
214 
215  bool m_isInitialized; //!< Tells if this instance is initialized.
216 
217  /*!
218  \brief Load data from the input raster.
219  \param rasterData The loaded data buffer.
220  \return true if OK, false on errors.
221  */
222  bool loadData( te::rp::Matrix< double >& rasterData ) const;
223 
224  /*!
225  \brief Create an gradient maps from the input image.
226  \param inputData The input data.
227  \param unitVectors If true, unit vectors will be created.
228  \param gradXMap The created gradient X vectors map.
229  \param gradYMap The created gradient Y vectors map.
230  \return true if OK, false on errors.
231  */
232  bool getGradientMaps(
233  const te::rp::Matrix< double >& inputData,
234  const bool unitVectors,
235  te::rp::Matrix< double >& gradXMap,
236  te::rp::Matrix< double >& gradYMap ) const;
237 
238  /*!
239  \brief Apply a mean filter.
240  \param input The input data.
241  \param output The output data.
242  \return true if OK, false on errors.
243  */
244  template< typename MatrixElementT >
247  te::rp::Matrix< MatrixElementT >& output ) const
248  {
249  const unsigned int nRows = input.getLinesNumber();
250  const unsigned int nCols = input.getColumnsNumber();
251 
252  if( ! output.reset( nRows,nCols ) )
253  return false;
254 
255  const unsigned int lastRowIdx = nRows - 1;
256  const unsigned int lastColIdx = nCols - 1;
257  unsigned int row = 0;
258  unsigned int col = 0;
259  unsigned int nextRow = 0;
260  unsigned int nextCol = 0;
261  unsigned int prevRow = 0;
262  unsigned int prevCol = 0;
263 
264  for( row = 0 ; row < nRows ; ++row )
265  {
266  output[ row ][ 0 ] = input[ row ][ 0 ];
267  output[ row ][ lastColIdx ] = input[ row ][ lastColIdx ];
268  }
269 
270  for( col = 0 ; col < nCols ; ++col )
271  {
272  output[ 0 ][ col ] = input[ 0 ][ col ];
273  output[ lastRowIdx ][ col ] = input[ lastRowIdx ][ col ];
274  }
275 
276  for( row = 1 ; row < lastRowIdx ; ++row )
277  {
278  prevRow = row - 1;
279  nextRow = row + 1;
280 
281  for( col = 1 ; col < lastColIdx ; ++col )
282  {
283  prevCol = col - 1;
284  nextCol = col + 1;
285 
286  output[ row ][ col ] =
287  (
288  input[ row ][ prevCol ]
289  + input[ row ][ nextCol ]
290  + input[ prevRow ][ prevCol ]
291  + input[ prevRow ][ col ]
292  + input[ prevRow ][ nextCol ]
293  + input[ nextRow ][ prevCol ]
294  + input[ nextRow ][ col ]
295  + input[ nextRow ][ nextCol ]
296  ) / 8.0;
297  }
298  }
299 
300  return true;
301  };
302 
303  /*!
304  \brief Generate the magnitude map from the input vectors.
305  \param xMap The input data X component.
306  \param yMap The input data Y component.
307  \param magnitude The magnitude output data.
308  \return true if OK, false on errors.
309  */
310  template< typename MatrixElementT >
312  const te::rp::Matrix< double >& xMap,
313  const te::rp::Matrix< double >& yMap,
314  te::rp::Matrix< MatrixElementT >& magnitude ) const
315  {
316  assert( xMap.getColumnsNumber() == yMap.getColumnsNumber() );
317  assert( xMap.getLinesNumber() == yMap.getLinesNumber() );
318 
319  const unsigned int nRows = xMap.getLinesNumber();
320  const unsigned int nCols = xMap.getColumnsNumber();
321 
322  if( ( magnitude.getColumnsNumber() != nCols ) &&
323  ( magnitude.getLinesNumber() != nRows ) )
324  {
325  if( ! magnitude.reset( nRows,nCols ) )
326  return false;
327  }
328 
329  unsigned int row = 0;
330  unsigned int col = 0;
331  double xValue = 0;
332  double yValue = 0;
333 
334  for( row = 0 ; row < nRows ; ++row )
335  {
336  for( col = 0 ; col < nCols ; ++col )
337  {
338  xValue = xMap[ row ][ col ];
339  yValue = yMap[ row ][ col ];
340 
341  magnitude[ row ][ col ] = std::sqrt( ( xValue * xValue ) +
342  ( yValue * yValue ) );
343  }
344  }
345 
346  return true;
347  };
348 
349  /*!
350  \brief Create an Edge strenght Map from the input data.
351  \param inputMap The input map.
352  \param edgeStrengthMap The edge strength map (values between 0 and 1).
353  \return true if OK, false on errors.
354  \details The edge map have the property that the values are large near image boundaries and small within homogeneous regions.
355  */
356  bool getEdgeStrengthMap(
357  const te::rp::Matrix< double >& inputMap,
358  te::rp::Matrix< double >& edgeStrengthMap ) const;
359 
360  /*!
361  \brief Create a tiff file from a vector field.
362  \param inputVecFieldX The vector decomposed X component;
363  \param inputVecFieldY The vector decomposed Y component;
364  \param backGroundMapPtr An optional background image (0 means no background image).
365  \param vecPixelStep The step between each vector.
366  \param tifFileName Tif file name.
367  */
368  void createTifFromVecField(
369  const te::rp::Matrix< double >& inputVecFieldX,
370  const te::rp::Matrix< double >& inputVecFieldY,
371  te::rp::Matrix< double > const * const backGroundMapPtr,
372  const unsigned int vecPixelStep,
373  const std::string& tifFileName ) const;
374 
375  /*!
376  \brief Apply a vector diffusion over the given vector field.
377  \param inputX The vector decomposed X component;
378  \param inputY The vector decomposed Y component;
379  \param backGroundMapPtr An optional background image (0 means no background image).
380  \param progressPtr A pointer to a progress interface or an NULL pointer.
381  \param outputX The diffused X component.
382  \param outputX The diffused Y component.
383  */
384  bool applyVecDiffusion(
385  const te::rp::Matrix< double >& inputX,
386  const te::rp::Matrix< double >& inputY,
387  te::rp::Matrix< double > const * const backgroundDataPtr,
388  te::common::TaskProgress* progressPtr,
389  te::rp::Matrix< double >& outputX,
390  te::rp::Matrix< double >& outputY ) const;
391 
392  /*!
393  \brief Create a skeleton strength map.
394  \details The skeleton strength map is a scalar map defined on every pixel and indicates the likelihood of each pixel being on the skeletons.
395  \param inputX The vector decomposed X component;
396  \param inputY The vector decomposed Y component;
397  \param edgeStrengthMap The edge strength map (values between 0 and 1).
398  \param skelSMap The skeleton map (values between 0 and 1).
399  \return true if ok, false on errors.
400  */
401  bool createSkeletonStrengthMap(
402  const te::rp::Matrix< double >& inputX,
403  const te::rp::Matrix< double >& inputY,
404  const te::rp::Matrix< double >& edgeStrengthMap,
405  te::rp::Matrix< double >& skelMap ) const;
406 
407  /*!
408  \brief Vector diffusion thread entry.
409 
410  \param paramsPtr A pointer to the thread parameters.
411  */
412  static void applyVecDiffusionThreadEntry( ApplyVecDiffusionThreadParams* paramsPtr);
413 
414  };
415 
416  } // end namespace rp
417 } // end namespace te
418 
419 #endif
420 
te::rp::Matrix< double > const * m_initialXBufPtr
A pointer to the input buffer initial X component.
Definition: Skeleton.h:170
Generic template matrix.
Skeleton::InputParameters m_inputParameters
Input execution parameters.
Definition: Skeleton.h:213
std::auto_ptr< te::rst::Raster > m_outputRasterPtr
The generated output registered raster.
Definition: Skeleton.h:126
double m_diffusionRegularization
The diffusion regularization parameter.
Definition: Skeleton.h:182
unsigned int m_firstRowIdx
First row to process.
Definition: Skeleton.h:179
te::rst::Raster const * m_inputMaskRasterPtr
A pointer to an input raster (where pixels with zero velues will be ignored) or an null pointer if no...
Definition: Skeleton.h:84
unsigned int m_lastRowIdx
Last row to process.
Definition: Skeleton.h:180
bool applyMeanSmooth(const te::rp::Matrix< MatrixElementT > &input, te::rp::Matrix< MatrixElementT > &output) const
Apply a mean filter.
Definition: Skeleton.h:245
double m_diffusionRegularization
A regularization parameter to control the variation from one iteration to the next one (higher values...
Definition: Skeleton.h:88
Raster Processing algorithm base interface class.
te::rp::Matrix< double > const * m_inputMagBufPtr
A pointer to the input magnitude buffer.
Definition: Skeleton.h:172
te::rp::Matrix< double > * m_outputMagBufPtr
A pointer to the output magnitude buffer.
Definition: Skeleton.h:175
double * m_currentIterationResiduePtr
A pointer the the current iteration residue;.
Definition: Skeleton.h:181
unsigned int getLinesNumber() const
The number of current matrix lines.
Definition: Matrix.h:665
te::rp::Matrix< double > const * m_inputBufYPtr
A pointer to the input buffer Y component.
Definition: Skeleton.h:174
ApplyVecDiffusionThreadParams & operator=(const ApplyVecDiffusionThreadParams &other)
Definition: Skeleton.h:193
ApplyVecDiffusionThreadParams(const ApplyVecDiffusionThreadParams &other)
Definition: Skeleton.h:186
Raster Processing algorithm base interface.
Definition: Algorithm.h:42
Skeleton output parameters.
Definition: Skeleton.h:118
bool m_enableMultiThread
Enable (true) the use of threads.
Definition: Skeleton.h:92
te::rst::Raster const * m_inputRasterPtr
Input raster.
Definition: Skeleton.h:80
boost::mutex * m_mutexPtr
A pointer to the sync mutex.
Definition: Skeleton.h:178
std::string m_rType
Output raster data source type (as described in te::raster::RasterFactory ).
Definition: Skeleton.h:122
te::rp::Matrix< double > * m_outputBufXPtr
A pointer to the output buffer X component.
Definition: Skeleton.h:176
te::rp::Matrix< double > const * m_initialYBufPtr
A pointer to the input buffer initial Y component.
Definition: Skeleton.h:171
te::rp::Matrix< double > const * m_inputBufXPtr
A pointer to the input buffer X component.
Definition: Skeleton.h:173
bool m_enableProgress
Enable/Disable the progress interface (default:false).
Definition: Skeleton.h:96
unsigned int m_diffusionMaxIterations
The maximum number of iterations to perform (zero: no iterations limit).
Definition: Skeleton.h:90
Skeleton input parameters.
Definition: Skeleton.h:76
unsigned int getColumnsNumber() const
The number of current matrix columns.
Definition: Matrix.h:672
double m_skeletonThreshold
A threshold to select those pixels as being part of the final skeleton - valid range [0...
Definition: Skeleton.h:94
Raster Processing algorithm output parameters base interface.
void reset()
Reset (clear) the active instance data.
Definition: Matrix.h:474
bool m_isInitialized
Tells if this instance is initialized.
Definition: Skeleton.h:215
An abstract class for raster data strucutures.
Definition: Raster.h:70
This class can be used to inform the progress of a task.
Definition: TaskProgress.h:53
Creation of skeleton images.
Definition: Skeleton.h:68
unsigned int m_inputRasterBand
Bands to process from the input raster.
Definition: Skeleton.h:82
te::rp::Matrix< double > * m_outputBufYPtr
A pointer to the output buffer X component.
Definition: Skeleton.h:177
double m_diffusionThreshold
A threshold over the residue from one iteration to another, if the residue drops below this value the...
Definition: Skeleton.h:86
std::map< std::string, std::string > m_rInfo
The necessary information to create the output rasters (as described in te::raster::RasterFactory).
Definition: Skeleton.h:124
#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.
bool getMagnitude(const te::rp::Matrix< double > &xMap, const te::rp::Matrix< double > &yMap, te::rp::Matrix< MatrixElementT > &magnitude) const
Generate the magnitude map from the input vectors.
Definition: Skeleton.h:311