Skeleton.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/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  \ingroup rp_gen
69  */
70  class TERPEXPORT Skeleton : public Algorithm
71  {
72  public:
73 
74  /*!
75  \class InputParameters
76  \brief Skeleton input parameters
77  */
79  {
80  public:
81 
82  te::rst::Raster const* m_inputRasterPtr; //!< Input raster.
83 
84  unsigned int m_inputRasterBand; //!< Bands to process from the input raster.
85 
86  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.
87 
88  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.
89 
90  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 ).
91 
92  unsigned int m_diffusionMaxIterations; //!< The maximum number of iterations to perform (zero: no iterations limit).
93 
94  bool m_enableMultiThread; //!< Enable (true) the use of threads.
95 
96  double m_skeletonThreshold; //!< A threshold to select those pixels as being part of the final skeleton - valid range [0,1].
97 
98  bool m_enableProgress; //!< Enable/Disable the progress interface (default:false).
99 
101 
103 
105 
106  //overload
107  void reset() _NOEXCEPT_OP(false);
108 
109  //overload
110  const InputParameters& operator=( const InputParameters& params );
111 
112  //overload
113  AbstractParameters* clone() const;
114 
115  //overload
116  bool serialize ( AlgorithmParametersSerializer& serializer ) const;
117  };
118 
119  /*!
120  \class OutputParameters
121  \brief Skeleton output parameters
122  */
124  {
125  public:
126 
127  std::string m_rType; //!< Output raster data source type (as described in te::raster::RasterFactory ).
128 
129  std::map< std::string, std::string > m_rInfo; //!< The necessary information to create the output rasters (as described in te::raster::RasterFactory).
130 
131  std::unique_ptr< te::rst::Raster > m_outputRasterPtr; //!< The generated output registered raster.
132 
134 
136 
138 
139  //overload
140  void reset() _NOEXCEPT_OP(false);
141 
142  //overload
143  const OutputParameters& operator=( const OutputParameters& params );
144 
145  //overload
146  AbstractParameters* clone() const;
147  };
148 
150 
152 
153  //overload
154  bool execute( AlgorithmOutputParameters& outputParams ) _NOEXCEPT_OP(false);
155 
156  //overload
157  void reset() _NOEXCEPT_OP(false);
158 
159  //overload
160  bool initialize( const AlgorithmInputParameters& inputParams ) _NOEXCEPT_OP(false);
161 
162  bool isInitialized() const;
163 
164  protected:
165 
166  /*!
167  \class ApplyRansacThreadEntryThreadParams
168 
169  \brief Parameters used by the GTFilter::applyRansacThreadEntry method.
170  */
172  {
173  public:
174 
175  te::rp::Matrix< double > const * m_initialXBufPtr; //!< A pointer to the input buffer initial X component.
176  te::rp::Matrix< double > const * m_initialYBufPtr; //!< A pointer to the input buffer initial Y component.
177  te::rp::Matrix< double > const * m_inputMagBufPtr; //!< A pointer to the input magnitude buffer.
178  te::rp::Matrix< double > const * m_inputBufXPtr; //!< A pointer to the input buffer X component.
179  te::rp::Matrix< double > const * m_inputBufYPtr; //!< A pointer to the input buffer Y component.
180  te::rp::Matrix< double > * m_outputMagBufPtr; //!< A pointer to the output magnitude buffer.
181  te::rp::Matrix< double > * m_outputBufXPtr; //!< A pointer to the output buffer X component.
182  te::rp::Matrix< double > * m_outputBufYPtr; //!< A pointer to the output buffer X component.
183  boost::mutex* m_mutexPtr; //!< A pointer to the sync mutex.
184  unsigned int m_firstRowIdx; //!< First row to process.
185  unsigned int m_lastRowIdx; //!< Last row to process.
186  double* m_currentIterationResiduePtr; //!< A pointer the the current iteration residue;
187  double m_diffusionRegularization; //!< The diffusion regularization parameter.
188 
190 
192  {
193  operator=( other );
194  }
195 
197 
199  {
200  m_initialXBufPtr = other.m_initialXBufPtr;
201  m_initialYBufPtr = other.m_initialYBufPtr;
202  m_inputMagBufPtr = other.m_inputMagBufPtr;
203  m_inputBufXPtr = other.m_inputBufXPtr;
204  m_inputBufYPtr = other.m_inputBufYPtr;
205  m_outputMagBufPtr = other.m_outputMagBufPtr;
206  m_outputBufXPtr = other.m_outputBufXPtr;
207  m_outputBufYPtr = other.m_outputBufYPtr;
208  m_mutexPtr = other.m_mutexPtr;
209  m_firstRowIdx = other.m_firstRowIdx;
210  m_lastRowIdx = other.m_lastRowIdx;
211  m_currentIterationResiduePtr = other.m_currentIterationResiduePtr;
212  m_diffusionRegularization = other.m_diffusionRegularization;
213 
214  return *this;
215  }
216  };
217 
218  Skeleton::InputParameters m_inputParameters; //!< Input execution parameters.
219 
220  bool m_isInitialized; //!< Tells if this instance is initialized.
221 
222  /*!
223  \brief Load data from the input raster.
224  \param rasterData The loaded data buffer.
225  \return true if OK, false on errors.
226  */
227  bool loadData( te::rp::Matrix< double >& rasterData ) const;
228 
229  /*!
230  \brief Create an gradient maps from the input image.
231  \param inputData The input data.
232  \param unitVectors If true, unit vectors will be created.
233  \param gradXMap The created gradient X vectors map.
234  \param gradYMap The created gradient Y vectors map.
235  \return true if OK, false on errors.
236  */
238  const te::rp::Matrix< double >& inputData,
239  const bool unitVectors,
240  te::rp::Matrix< double >& gradXMap,
241  te::rp::Matrix< double >& gradYMap ) const;
242 
243  /*!
244  \brief Apply a mean filter.
245  \param input The input data.
246  \param output The output data.
247  \return true if OK, false on errors.
248  */
249  template< typename MatrixElementT >
252  te::rp::Matrix< MatrixElementT >& output ) const
253  {
254  const unsigned int nRows = input.getLinesNumber();
255  const unsigned int nCols = input.getColumnsNumber();
256 
257  if( ! output.reset( nRows,nCols ) )
258  return false;
259 
260  const unsigned int lastRowIdx = nRows - 1;
261  const unsigned int lastColIdx = nCols - 1;
262  unsigned int row = 0;
263  unsigned int col = 0;
264  unsigned int nextRow = 0;
265  unsigned int nextCol = 0;
266  unsigned int prevRow = 0;
267  unsigned int prevCol = 0;
268 
269  for( row = 0 ; row < nRows ; ++row )
270  {
271  output[ row ][ 0 ] = input[ row ][ 0 ];
272  output[ row ][ lastColIdx ] = input[ row ][ lastColIdx ];
273  }
274 
275  for( col = 0 ; col < nCols ; ++col )
276  {
277  output[ 0 ][ col ] = input[ 0 ][ col ];
278  output[ lastRowIdx ][ col ] = input[ lastRowIdx ][ col ];
279  }
280 
281  for( row = 1 ; row < lastRowIdx ; ++row )
282  {
283  prevRow = row - 1;
284  nextRow = row + 1;
285 
286  for( col = 1 ; col < lastColIdx ; ++col )
287  {
288  prevCol = col - 1;
289  nextCol = col + 1;
290 
291  output[ row ][ col ] =
292  (
293  input[ row ][ prevCol ]
294  + input[ row ][ nextCol ]
295  + input[ prevRow ][ prevCol ]
296  + input[ prevRow ][ col ]
297  + input[ prevRow ][ nextCol ]
298  + input[ nextRow ][ prevCol ]
299  + input[ nextRow ][ col ]
300  + input[ nextRow ][ nextCol ]
301  ) / 8.0;
302  }
303  }
304 
305  return true;
306  }
307 
308  /*!
309  \brief Generate the magnitude map from the input vectors.
310  \param xMap The input data X component.
311  \param yMap The input data Y component.
312  \param magnitude The magnitude output data.
313  \return true if OK, false on errors.
314  */
315  template< typename MatrixElementT >
317  const te::rp::Matrix< double >& xMap,
318  const te::rp::Matrix< double >& yMap,
319  te::rp::Matrix< MatrixElementT >& magnitude ) const
320  {
321  assert( xMap.getColumnsNumber() == yMap.getColumnsNumber() );
322  assert( xMap.getLinesNumber() == yMap.getLinesNumber() );
323 
324  const unsigned int nRows = xMap.getLinesNumber();
325  const unsigned int nCols = xMap.getColumnsNumber();
326 
327  if( ( magnitude.getColumnsNumber() != nCols ) &&
328  ( magnitude.getLinesNumber() != nRows ) )
329  {
330  if( ! magnitude.reset( nRows,nCols ) )
331  return false;
332  }
333 
334  unsigned int row = 0;
335  unsigned int col = 0;
336  double xValue = 0;
337  double yValue = 0;
338 
339  for( row = 0 ; row < nRows ; ++row )
340  {
341  for( col = 0 ; col < nCols ; ++col )
342  {
343  xValue = xMap[ row ][ col ];
344  yValue = yMap[ row ][ col ];
345 
346  magnitude[ row ][ col ] = std::sqrt( ( xValue * xValue ) +
347  ( yValue * yValue ) );
348  }
349  }
350 
351  return true;
352  }
353 
354  /*!
355  \brief Create an Edge strenght Map from the input data.
356  \param inputMap The input map.
357  \param edgeStrengthMap The edge strength map (values between 0 and 1).
358  \return true if OK, false on errors.
359  \details The edge map have the property that the values are large near image boundaries and small within homogeneous regions.
360  */
362  const te::rp::Matrix< double >& inputMap,
363  te::rp::Matrix< double >& edgeStrengthMap ) const;
364 
365  /*!
366  \brief Create a tiff file from a vector field.
367  \param inputVecFieldX The vector decomposed X component;
368  \param inputVecFieldY The vector decomposed Y component;
369  \param backGroundMapPtr An optional background image (0 means no background image).
370  \param vecPixelStep The step between each vector.
371  \param tifFileName Tif file name.
372  */
374  const te::rp::Matrix< double >& inputVecFieldX,
375  const te::rp::Matrix< double >& inputVecFieldY,
376  te::rp::Matrix< double > const * const backGroundMapPtr,
377  const unsigned int vecPixelStep,
378  const std::string& tifFileName ) const;
379 
380  /*!
381  \brief Apply a vector diffusion over the given vector field.
382  \param inputX The vector decomposed X component;
383  \param inputY The vector decomposed Y component;
384  \param backGroundMapPtr An optional background image (0 means no background image).
385  \param progressPtr A pointer to a progress interface or an NULL pointer.
386  \param outputX The diffused X component.
387  \param outputX The diffused Y component.
388  */
390  const te::rp::Matrix< double >& inputY, te::rp::Matrix< double > const * const,
391  te::common::TaskProgress* progressPtr,
392  te::rp::Matrix< double >& outputX,
393  te::rp::Matrix< double >& outputY ) const;
394 
395  /*!
396  \brief Create a skeleton strength map.
397  \details The skeleton strength map is a scalar map defined on every pixel and indicates the likelihood of each pixel being on the skeletons.
398  \param inputX The vector decomposed X component;
399  \param inputY The vector decomposed Y component;
400  \param edgeStrengthMap The edge strength map (values between 0 and 1).
401  \param skelSMap The skeleton map (values between 0 and 1).
402  \return true if ok, false on errors.
403  */
405  const te::rp::Matrix< double >& inputX,
406  const te::rp::Matrix< double >& inputY,
407  const te::rp::Matrix< double >& edgeStrengthMap,
408  te::rp::Matrix< double >& skelMap ) const;
409 
410  /*!
411  \brief Vector diffusion thread entry.
412 
413  \param paramsPtr A pointer to the thread parameters.
414  */
416 
417  };
418 
419  } // end namespace rp
420 } // end namespace te
421 
422 #endif
423 
Generic template matrix.
#define _NOEXCEPT_OP(x)
This class can be used to inform the progress of a task.
Definition: TaskProgress.h:54
Raster Processing algorithm input parameters base interface.
Raster Processing algorithm output parameters base interface.
A class to standardize algorithm parameters serialization.
Raster Processing algorithm base interface.
Definition: Algorithm.h:42
unsigned int getLinesNumber() const
The number of current matrix lines.
Definition: Matrix.h:782
unsigned int getColumnsNumber() const
The number of current matrix columns.
Definition: Matrix.h:789
void reset()
Reset (clear) the active instance data.
Definition: Matrix.h:492
te::rp::Matrix< double > * m_outputMagBufPtr
A pointer to the output magnitude buffer.
Definition: Skeleton.h:180
unsigned int m_lastRowIdx
Last row to process.
Definition: Skeleton.h:185
te::rp::Matrix< double > const * m_initialXBufPtr
A pointer to the input buffer initial X component.
Definition: Skeleton.h:175
unsigned int m_firstRowIdx
First row to process.
Definition: Skeleton.h:184
te::rp::Matrix< double > const * m_inputBufXPtr
A pointer to the input buffer X component.
Definition: Skeleton.h:178
boost::mutex * m_mutexPtr
A pointer to the sync mutex.
Definition: Skeleton.h:183
te::rp::Matrix< double > const * m_inputBufYPtr
A pointer to the input buffer Y component.
Definition: Skeleton.h:179
double m_diffusionRegularization
The diffusion regularization parameter.
Definition: Skeleton.h:187
ApplyVecDiffusionThreadParams(const ApplyVecDiffusionThreadParams &other)
Definition: Skeleton.h:191
ApplyVecDiffusionThreadParams & operator=(const ApplyVecDiffusionThreadParams &other)
Definition: Skeleton.h:198
te::rp::Matrix< double > * m_outputBufYPtr
A pointer to the output buffer X component.
Definition: Skeleton.h:182
te::rp::Matrix< double > * m_outputBufXPtr
A pointer to the output buffer X component.
Definition: Skeleton.h:181
te::rp::Matrix< double > const * m_initialYBufPtr
A pointer to the input buffer initial Y component.
Definition: Skeleton.h:176
te::rp::Matrix< double > const * m_inputMagBufPtr
A pointer to the input magnitude buffer.
Definition: Skeleton.h:177
double * m_currentIterationResiduePtr
A pointer the the current iteration residue;.
Definition: Skeleton.h:186
Skeleton input parameters.
Definition: Skeleton.h:79
double m_diffusionThreshold
A threshold over the residue from one iteration to another, if the residue drops below this value the...
Definition: Skeleton.h:88
unsigned int m_diffusionMaxIterations
The maximum number of iterations to perform (zero: no iterations limit).
Definition: Skeleton.h:92
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:86
InputParameters(const InputParameters &)
bool m_enableProgress
Enable/Disable the progress interface (default:false).
Definition: Skeleton.h:98
unsigned int m_inputRasterBand
Bands to process from the input raster.
Definition: Skeleton.h:84
double m_skeletonThreshold
A threshold to select those pixels as being part of the final skeleton - valid range [0,...
Definition: Skeleton.h:96
bool m_enableMultiThread
Enable (true) the use of threads.
Definition: Skeleton.h:94
te::rst::Raster const * m_inputRasterPtr
Input raster.
Definition: Skeleton.h:82
void reset() _NOEXCEPT_OP(false)
Clear all internal allocated resources and reset the parameters instance to its initial state.
double m_diffusionRegularization
A regularization parameter to control the variation from one iteration to the next one (higher values...
Definition: Skeleton.h:90
Skeleton output parameters.
Definition: Skeleton.h:124
void reset() _NOEXCEPT_OP(false)
Clear all internal allocated resources and reset the parameters instance to its initial state.
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:129
std::unique_ptr< te::rst::Raster > m_outputRasterPtr
The generated output registered raster.
Definition: Skeleton.h:131
OutputParameters(const OutputParameters &)
std::string m_rType
Output raster data source type (as described in te::raster::RasterFactory ).
Definition: Skeleton.h:127
Creation of skeleton images.
Definition: Skeleton.h:71
bool applyVecDiffusion(const te::rp::Matrix< double > &inputX, const te::rp::Matrix< double > &inputY, te::rp::Matrix< double > const *const, te::common::TaskProgress *progressPtr, te::rp::Matrix< double > &outputX, te::rp::Matrix< double > &outputY) const
Apply a vector diffusion over the given vector field.
bool applyMeanSmooth(const te::rp::Matrix< MatrixElementT > &input, te::rp::Matrix< MatrixElementT > &output) const
Apply a mean filter.
Definition: Skeleton.h:250
bool loadData(te::rp::Matrix< double > &rasterData) const
Load data from the input raster.
bool m_isInitialized
Tells if this instance is initialized.
Definition: Skeleton.h:220
bool getGradientMaps(const te::rp::Matrix< double > &inputData, const bool unitVectors, te::rp::Matrix< double > &gradXMap, te::rp::Matrix< double > &gradYMap) const
Create an gradient maps from the input image.
void createTifFromVecField(const te::rp::Matrix< double > &inputVecFieldX, const te::rp::Matrix< double > &inputVecFieldY, te::rp::Matrix< double > const *const backGroundMapPtr, const unsigned int vecPixelStep, const std::string &tifFileName) const
Create a tiff file from a vector field.
bool getEdgeStrengthMap(const te::rp::Matrix< double > &inputMap, te::rp::Matrix< double > &edgeStrengthMap) const
Create an Edge strenght Map from the input data.
static void applyVecDiffusionThreadEntry(ApplyVecDiffusionThreadParams *paramsPtr)
Vector diffusion thread entry.
Skeleton::InputParameters m_inputParameters
Input execution parameters.
Definition: Skeleton.h:218
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:316
bool createSkeletonStrengthMap(const te::rp::Matrix< double > &inputX, const te::rp::Matrix< double > &inputY, const te::rp::Matrix< double > &edgeStrengthMap, te::rp::Matrix< double > &skelMap) const
Create a skeleton strength map.
An abstract class for raster data strucutures.
Definition: Raster.h:72
TerraLib.
#define TERPEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:139
Abstract algorithm.