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