TiePointsLocatorSURFStrategy.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/TiePointsLocatorSURFStrategy.h
22  \brief Tie-Pointsr locator SURF strategy.
23  */
24 
25 #ifndef __TERRALIB_RP_INTERNAL_TIEPOINTSLOCATORSURFSTRATEGY_H
26 #define __TERRALIB_RP_INTERNAL_TIEPOINTSLOCATORSURFSTRATEGY_H
27 
31 
32 #include <boost/thread.hpp>
33 
34 #include <cmath>
35 
36 namespace te
37 {
38  namespace rp
39  {
40  /*!
41  \class TiePointsLocatorSURFStrategy
42  \brief Tie-points locator SURF strategy.
43  */
45  {
47 
48  public:
49 
50  /*!
51  \class Parameters
52  \brief TiePointsLocator SURF strategy parameters.
53  */
55  {
56  public:
57 
58  unsigned int m_surfScalesNumber; //!< The number of sub-sampling scales to generate, when applicable (default:3, minimum:3).
59 
60  unsigned int m_surfOctavesNumber; //!< The number of octaves to generate, when applicable (default: 2, minimum:2).
61 
62  double m_surfMaxNormEuclideanDist; //!< The maximum acceptable euclidean distance when matching features (when applicable), default:0.75, valid range: [0,1].
63 
65 
67 
69 
70  //overload
71  void reset() ;
72 
73  //overload
74  const Parameters& operator=( const Parameters& params );
75 
76  //overload
77  AbstractParameters* clone() const;
78 
79  //overload
80  bool serialize ( AlgorithmParametersSerializer& serializer ) const;
81  };
82 
84 
85  //overload
87  const double subSampleOptimizationRescaleFactor,
88  const TiePointsLocatorStrategyParameters& inputSpecParams,
89  std::unique_ptr< TiePointsLocatorStrategyParameters >& subSampledSpecParamsPtr ) const;
90 
91  //overload
93  std::unique_ptr< TiePointsLocatorStrategyParameters >& defaultSpecParamsPtr ) const;
94 
95  protected :
96 
97  /*!
98  \brief The parameters passed to the surfLocatorThreadEntry method.
99  */
101  {
102  public :
103 
104  bool* m_returnValuePtr; //! Thread return value pointer.
105 
106  unsigned int m_scalesNumber; //!< The number of sub-samples scales to generate (minimum:3).
107 
108  unsigned int m_octavesNumber; //!< The number of octaves to generate (minimum:1).
109 
110  std::vector< InterestPointsSetT >* m_interestPointsSubSectorsPtr; //!< A pointer to a valid interest points container (one element by subsector)..
111 
112  unsigned int m_maxInterestPointsBySubSector; //!< The maximum number of interest points by sub-sector.
113 
114  unsigned int m_tiePointsSubSectorsSplitFactor; //!< The number of sectors along each direction.
115 
116  FloatsMatrix const* m_integralRasterDataPtr; //!< The integral image raster data.
117 
118  UCharsMatrix const* m_maskRasterDataPtr; //!< The loaded mask raster data pointer (or zero if no mask is avaliable).
119 
120  boost::mutex* m_rastaDataAccessMutexPtr; //!< A pointer to a valid mutex to controle raster data access.
121 
122  boost::mutex* m_interestPointsAccessMutexPtr; //!< A pointer to a valid mutex to control the output interest points container access.
123 
124  unsigned int m_processingBlocksNumber; //!< The raster data will be splitted into this number of blocks for processing.
125 
126  unsigned int* m_nextRasterLinesBlockToProcessValuePtr; //!< A pointer to a valid counter to control the blocks processing sequence.
127 
129 
131  };
132 
133  /*!
134  \brief The parameters passed to the executeMatchingByEuclideanDistThreadEntry method.
135  */
137  {
138  public :
139 
141 
143 
145 
147 
149 
151 
152  boost::mutex* m_syncMutexPtr;
153 
154  te::gm::GeometricTransformation const * m_raster1ToRaster2TransfPtr; //!< A pointer to a transformation direct mapping raster 1 indexed coords into raster 2 indexed coords, of an empty pointer if there is no transformation avaliable.
155 
156  double m_searchOptTreeSearchRadius; //!< Optimization tree search radius (pixels).
157 
159 
161  };
162 
163  bool m_isInitialized; //!< true if this instance is initialized.
164 
166 
168 
169  //overload
170  bool initialize(
171  const te::rp::TiePointsLocatorInputParameters& inputParameters );
172 
173  //overload
174  void reset();
175 
176  //overload
178  te::gm::GeometricTransformation const * const raster1ToRaster2TransfPtr,
179  const double raster1ToRaster2TransfDMapError,
180  MatchedInterestPointsSetT& matchedInterestPoints );
181 
182  //overload
183  unsigned int getAutoMaxTiePointsNumber() const;
184 
185  /*!
186  \brief Create an integral image.
187 
188  \param inputData The input data.
189 
190  \param outputData The output data.
191 
192  \note The entry of an integral image IΣ(x) at a location x = (x, y) represents the sum of all pixels in the input image I of a rectangular region formed by the point x and the origi.
193 
194  \return true if ok, false on errors.
195  */
196  static bool createIntegralImage(
197  const FloatsMatrix& inputData,
198  FloatsMatrix& outputData );
199 
200  /*!
201  \brief SURF interest points locator.
202 
203  \param maxInterestPoints The maximum number of interest points to find.
204 
205  \param integralRasterData Integral image raster data.
206 
207  \param maskRasterDataPtr The loaded mask raster data pointer (or zero if no mask is avaliable).
208 
209  \param interestPoints The found interest points (coords related to rasterData lines/cols).
210 
211  \return true if ok, false on errors.
212 
213  \note InterestPointT::m_feature1 will be sum of differences between the hessian matrix determinant each pixel and its neighborhoods (always a positive value).
214 
215  \note InterestPointT::m_feature2 will be used filter width (pixels).
216 
217  \note InterestPointT::m_feature3 will 1 if the laplacian sign is positive, or zero if negative.
218  */
220  const unsigned int maxInterestPoints,
221  const FloatsMatrix& integralRasterData,
222  UCharsMatrix const* maskRasterDataPtr,
223  InterestPointsSetT& interestPoints ) const;
224 
225  /*!
226  \brief Surf locator thread entry.
227 
228  \param paramsPtr A pointer to the thread parameters.
229  */
231 
232  /*!
233  \brief Generate a Surf features matrix for the given interes points.
234 
235  \param interestPoints The interest points (coords related to rasterData lines/cols).
236 
237  \param integralRasterData The integral raster data.
238 
239  \param validInterestPoints The valid interest points.
240 
241  \param features The generated features matrix (one feature per line, one feature per interest point).
242 
243  \return true if ok, false on errors.
244  */
245  static bool generateSurfFeatures(
246  const InterestPointsSetT& interestPoints,
247  const FloatsMatrix& integralRasterData,
248  InterestPointsSetT& validInterestPoints,
249  FloatsMatrix& features );
250 
251  /*!
252  \brief Match each feature using eucliean distance.
253 
254  \param featuresSet1 Features set 1.
255 
256  \param featuresSet2 Features set 2.
257 
258  \param interestPointsSet1 The interest pionts set 1 (full raster 1 indexed coods reference)..
259 
260  \param interestPointsSet2 The interest pionts set 2 (full raster 1 indexed coods reference)..
261 
262  \param raster1ToRaster2TransfPtr A pointer to a transformation direct mapping raster 1 indexed coords into raster 2 indexed coords, of an empty pointer if there is no transformation avaliable.
263 
264  \param raster1ToRaster2TransfDMapError The expected transformation error.
265 
266  \param matchedPoints The matched points.
267 
268  \note Each matched point feature value ( MatchedInterestPoint::m_feature ) will be set to the distance value between both features.
269  */
271  const FloatsMatrix& featuresSet1,
272  const FloatsMatrix& featuresSet2,
273  const InterestPointsSetT& interestPointsSet1,
274  const InterestPointsSetT& interestPointsSet2,
275  te::gm::GeometricTransformation const * const raster1ToRaster2TransfPtr,
276  const double raster1ToRaster2TransfDMapError,
277  MatchedInterestPointsSetT& matchedPoints );
278 
279  /*!
280  \brief Correlation/Euclidean match thread entry.
281 
282  \param paramsPtr A pointer to the thread parameters.
283  */
286 
287  /*!
288  \brief Return a sum of all pixels inside a box over the given integral image buffer.
289 
290  \param bufferPtr Buffer pointer.
291 
292  \param upperLeftX Box upper left X.
293 
294  \param upperLeftY Box upper left Y.
295 
296  \param lowerRightX Box lower right X.
297 
298  \param lowerRightY Box lower right X.
299  */
300  template< typename BufferType >
301  inline static float getIntegralBoxSum( BufferType& buffer,
302  const unsigned int& upperLeftX, const unsigned int& upperLeftY,
303  const unsigned int& lowerRightX, const unsigned int& lowerRightY )
304  {
305  return buffer[ lowerRightY ][ lowerRightX ]
306  - ( ( upperLeftY ) ? buffer[ upperLeftY - 1 ][ lowerRightX ] : 0 )
307  - ( ( upperLeftX ) ? buffer[ lowerRightY ][ upperLeftX - 1 ] : 0 )
308  + ( ( ( upperLeftX != 0 ) && ( upperLeftY != 0 ) ) ? buffer[ upperLeftY - 1 ][ upperLeftX - 1 ] : 0 );
309  }
310 
311  /*!
312  \brief Return a SURF box filter Dxx derivative centered over the given position from the given integral image buffer.
313 
314  \param bufferPtr Buffer pointer.
315 
316  \param centerX Center X.
317 
318  \param centerY Center Y.
319 
320  \param lobeWidth Filter lobe width.
321 
322  \param lobeRadius Filter lobe radius.
323  */
324  inline static float getSurfDyyDerivative( float** bufferPtr,
325  const unsigned int& centerX, const unsigned int& centerY,
326  const unsigned int& lobeWidth, const unsigned int& lobeRadius )
327  {
328  return
329  getIntegralBoxSum(
330  bufferPtr,
331  ( centerX + 1 - lobeWidth ),
332  ( centerY - lobeWidth - lobeRadius ),
333  ( centerX + lobeWidth - 1 ),
334  ( centerY + lobeWidth + lobeRadius ) )
335  - ( 3.0f *
336  getIntegralBoxSum(
337  bufferPtr,
338  ( centerX + 1 - lobeWidth ),
339  ( centerY - lobeRadius ),
340  ( centerX + lobeWidth - 1 ),
341  ( centerY + lobeRadius ) ) );
342  };
343 
344  /*!
345  \brief Return a SURF box filter Dyy derivative centered over the given position from the given integral image buffer.
346 
347  \param bufferPtr Buffer pointer.
348 
349  \param centerX Center X.
350 
351  \param centerY Center Y.
352 
353  \param lobeWidth Filter lobe width.
354 
355  \param lobeRadius Filter lobe radius.
356  */
357  inline static float getSurfDxxDerivative( float** bufferPtr,
358  const unsigned int& centerX, const unsigned int& centerY,
359  const unsigned int& lobeWidth, const unsigned int& lobeRadius )
360  {
361  return
362  getIntegralBoxSum(
363  bufferPtr,
364  ( centerX - lobeWidth - lobeRadius ),
365  ( centerY + 1 - lobeWidth ),
366  ( centerX + lobeWidth + lobeRadius ),
367  ( centerY + lobeWidth - 1 ) )
368  - 3.0f *
369  getIntegralBoxSum(
370  bufferPtr,
371  ( centerX - lobeRadius ),
372  ( centerY + 1 - lobeWidth ),
373  ( centerX + lobeRadius ),
374  ( centerY + lobeWidth - 1 ) );
375  };
376 
377  /*!
378  \brief Return a SURF box filter Dxy derivative centered over the given position from the given integral image buffer.
379 
380  \param bufferPtr Buffer pointer.
381 
382  \param centerX Center X.
383 
384  \param centerY Center Y.
385 
386  \param lobeWidth Filter lobe width.
387  */
388  inline static float getSurfDxyDerivative( float** bufferPtr,
389  const unsigned int& centerX, const unsigned int& centerY,
390  const unsigned int& lobeWidth )
391  {
392  return
393  // upper-left filter lobe
394  getIntegralBoxSum(
395  bufferPtr,
396  ( centerX - lobeWidth ),
397  ( centerY - lobeWidth ),
398  ( centerX - 1 ),
399  ( centerY - 1 ) )
400  +
401  // lower-right filter lobe
402  getIntegralBoxSum(
403  bufferPtr,
404  ( centerX + 1 ),
405  ( centerY + 1 ),
406  ( centerX + lobeWidth ),
407  ( centerY + lobeWidth ) )
408  -
409  // upper-right filter lobe
410  getIntegralBoxSum(
411  bufferPtr,
412  ( centerX + 1 ),
413  ( centerY - lobeWidth ),
414  ( centerX + lobeWidth ),
415  ( centerY - 1 ) )
416  -
417  // lower-left filter lobe
418  getIntegralBoxSum(
419  bufferPtr,
420  ( centerX - lobeWidth ),
421  ( centerY + 1 ),
422  ( centerX - 1 ),
423  ( centerY + lobeWidth ) );
424  };
425 
426  /*!
427  \brief Return a Haar X intesity vector for the window centered at the given point.
428 
429  \param buffer Integral image buffer.
430 
431  \param centerX Center X.
432 
433  \param centerY Center Y.
434 
435  \param radius Window radius.
436  */
437  template< typename BufferType >
438  inline static float getHaarXVectorIntensity( BufferType& buffer,
439  const unsigned int& centerX, const unsigned int& centerY,
440  const unsigned int& radius )
441  {
442  return
443  getIntegralBoxSum(
444  buffer,
445  ( centerX + 1 ),
446  ( centerY - radius ),
447  ( centerX + radius ),
448  ( centerY + radius ) )
449  -
450  getIntegralBoxSum(
451  buffer,
452  ( centerX - radius ),
453  ( centerY - radius ),
454  ( centerX - 1 ),
455  ( centerY + radius ) );
456  }
457 
458  /*!
459  \brief Return a Haar Y intesity vector for the window centered at the given point.
460 
461  \param buffer Integral image buffer.
462 
463  \param centerX Center X.
464 
465  \param centerY Center Y.
466 
467  \param radius Window radius.
468  */
469  template< typename BufferType >
470  inline static float getHaarYVectorIntensity( BufferType& buffer,
471  const unsigned int& centerX, const unsigned int& centerY,
472  const unsigned int& radius )
473  {
474  return
475  getIntegralBoxSum(
476  buffer,
477  ( centerX - radius ),
478  ( centerY - radius ),
479  ( centerX + radius ),
480  ( centerY - 1 ) )
481  -
482  getIntegralBoxSum(
483  buffer,
484  ( centerX - radius ),
485  ( centerY + 1 ),
486  ( centerX + radius ),
487  ( centerY + radius ) );
488  }
489 
490  /*!
491  \brief Return the surf octave filter step size (width).
492 
493  \param octaveIndex Octave index (starting from zero).
494  */
495  inline static unsigned int getSurfOctaveFilterStepSize( const unsigned int& octaveIndex )
496  {
497  return (unsigned int)std::pow( 2.0, (double)(octaveIndex + 1) );
498  };
499 
500  /*!
501  \brief Return the surf octave base filter size (width).
502 
503  \param octaveIndex Octave index (starting from zero).
504  */
505  inline static unsigned int getSurfOctaveBaseFilterSize( const unsigned int& octaveIndex )
506  {
507  return 3 + ( 3 * getSurfOctaveFilterStepSize( octaveIndex ) );
508  };
509 
510  /*!
511  \brief Return the surf octave filter size (width).
512 
513  \param octaveIndex Octave index (starting from zero).
514 
515  \param scaleIndex Scale index (starting from zero).
516  */
517  inline static unsigned int getSurfFilterSize( const unsigned int& octaveIndex,
518  const unsigned int& scaleIndex )
519  {
520  return getSurfOctaveBaseFilterSize( octaveIndex ) +
521  ( 3 * ( getSurfOctaveFilterStepSize( octaveIndex ) * scaleIndex ) );
522  };
523  };
524 
525  /*!
526  \class TiePointsLocatorSURFStrategyFactory
527  \brief SURF tie-points locator strategy factory.
528  \note Factory key: SURF
529  */
532  {
533  public:
534 
536 
538 
539  //overload
541 
542  };
543 
544  } // end namespace rp
545 } // end namespace te
546 
547 #endif
548 
Raster tie points locator strategy factory base class.
Tie Points Locator strategy parameters.
Tie-Pointsr locator strategy.
2D Geometric transformation base class.
A class to standardize algorithm parameters serialization.
A generic template matrix.
Definition: Matrix.h:55
SURF tie-points locator strategy factory.
te::rp::TiePointsLocatorStrategy * build()
Concrete factories (derived from this one) must implement this method in order to create objects.
The parameters passed to the executeMatchingByEuclideanDistThreadEntry method.
te::gm::GeometricTransformation const * m_raster1ToRaster2TransfPtr
A pointer to a transformation direct mapping raster 1 indexed coords into raster 2 indexed coords,...
TiePointsLocator SURF strategy parameters.
AbstractParameters * clone() const
Create a clone copy of this instance.
void reset()
Clear all internal allocated resources and reset the parameters instance to its initial state.
const Parameters & operator=(const Parameters &params)
bool serialize(AlgorithmParametersSerializer &serializer) const
Returns a parameter serialization object.
unsigned int m_surfOctavesNumber
The number of octaves to generate, when applicable (default: 2, minimum:2).
double m_surfMaxNormEuclideanDist
The maximum acceptable euclidean distance when matching features (when applicable),...
unsigned int m_surfScalesNumber
The number of sub-sampling scales to generate, when applicable (default:3, minimum:3).
The parameters passed to the surfLocatorThreadEntry method.
boost::mutex * m_interestPointsAccessMutexPtr
A pointer to a valid mutex to control the output interest points container access.
boost::mutex * m_rastaDataAccessMutexPtr
A pointer to a valid mutex to controle raster data access.
UCharsMatrix const * m_maskRasterDataPtr
The loaded mask raster data pointer (or zero if no mask is avaliable).
unsigned int m_tiePointsSubSectorsSplitFactor
The number of sectors along each direction.
unsigned int * m_nextRasterLinesBlockToProcessValuePtr
A pointer to a valid counter to control the blocks processing sequence.
unsigned int m_octavesNumber
The number of octaves to generate (minimum:1).
unsigned int m_processingBlocksNumber
The raster data will be splitted into this number of blocks for processing.
unsigned int m_maxInterestPointsBySubSector
The maximum number of interest points by sub-sector.
FloatsMatrix const * m_integralRasterDataPtr
The integral image raster data.
std::vector< InterestPointsSetT > * m_interestPointsSubSectorsPtr
A pointer to a valid interest points container (one element by subsector)..
static float getSurfDxyDerivative(float **bufferPtr, const unsigned int &centerX, const unsigned int &centerY, const unsigned int &lobeWidth)
Return a SURF box filter Dxy derivative centered over the given position from the given integral imag...
static float getIntegralBoxSum(BufferType &buffer, const unsigned int &upperLeftX, const unsigned int &upperLeftY, const unsigned int &lowerRightX, const unsigned int &lowerRightY)
Return a sum of all pixels inside a box over the given integral image buffer.
static unsigned int getSurfOctaveBaseFilterSize(const unsigned int &octaveIndex)
Return the surf octave base filter size (width).
void getSubSampledSpecStrategyParams(const double subSampleOptimizationRescaleFactor, const TiePointsLocatorStrategyParameters &inputSpecParams, std::unique_ptr< TiePointsLocatorStrategyParameters > &subSampledSpecParamsPtr) const
Returns a sub-sampled version of the given locator strategy specific input parameters.
static float getSurfDxxDerivative(float **bufferPtr, const unsigned int &centerX, const unsigned int &centerY, const unsigned int &lobeWidth, const unsigned int &lobeRadius)
Return a SURF box filter Dyy derivative centered over the given position from the given integral imag...
static void executeMatchingByEuclideanDistThreadEntry(ExecuteMatchingByEuclideanDistThreadEntryParams *paramsPtr)
Correlation/Euclidean match thread entry.
bool m_isInitialized
true if this instance is initialized.
bool executeMatchingByEuclideanDist(const FloatsMatrix &featuresSet1, const FloatsMatrix &featuresSet2, const InterestPointsSetT &interestPointsSet1, const InterestPointsSetT &interestPointsSet2, te::gm::GeometricTransformation const *const raster1ToRaster2TransfPtr, const double raster1ToRaster2TransfDMapError, MatchedInterestPointsSetT &matchedPoints)
Match each feature using eucliean distance.
bool locateSurfInterestPoints(const unsigned int maxInterestPoints, const FloatsMatrix &integralRasterData, UCharsMatrix const *maskRasterDataPtr, InterestPointsSetT &interestPoints) const
SURF interest points locator.
static unsigned int getSurfOctaveFilterStepSize(const unsigned int &octaveIndex)
Return the surf octave filter step size (width).
static void locateSurfInterestPointsThreadEntry(SurfLocatorThreadParams *paramsPtr)
Surf locator thread entry.
unsigned int getAutoMaxTiePointsNumber() const
Returns a automatically calculated optimum maximum amount tie-points following the current parameters...
static bool generateSurfFeatures(const InterestPointsSetT &interestPoints, const FloatsMatrix &integralRasterData, InterestPointsSetT &validInterestPoints, FloatsMatrix &features)
Generate a Surf features matrix for the given interes points.
static float getSurfDyyDerivative(float **bufferPtr, const unsigned int &centerX, const unsigned int &centerY, const unsigned int &lobeWidth, const unsigned int &lobeRadius)
Return a SURF box filter Dxx derivative centered over the given position from the given integral imag...
static float getHaarXVectorIntensity(BufferType &buffer, const unsigned int &centerX, const unsigned int &centerY, const unsigned int &radius)
Return a Haar X intesity vector for the window centered at the given point.
bool getMatchedInterestPoints(te::gm::GeometricTransformation const *const raster1ToRaster2TransfPtr, const double raster1ToRaster2TransfDMapError, MatchedInterestPointsSetT &matchedInterestPoints)
Try to find matched interest points.
te::rp::TiePointsLocatorInputParameters m_inputParameters
Input parameters.
void reset()
Clear all internal allocated resources and go back to the initial not-initialized state.
static unsigned int getSurfFilterSize(const unsigned int &octaveIndex, const unsigned int &scaleIndex)
Return the surf octave filter size (width).
static bool createIntegralImage(const FloatsMatrix &inputData, FloatsMatrix &outputData)
Create an integral image.
void getDefaultSpecStrategyParams(std::unique_ptr< TiePointsLocatorStrategyParameters > &defaultSpecParamsPtr) const
Returns a sub-sampled version of the given locator strategy specific input parameters.
bool initialize(const te::rp::TiePointsLocatorInputParameters &inputParameters)
Initialize the strategy.
static float getHaarYVectorIntensity(BufferType &buffer, const unsigned int &centerX, const unsigned int &centerY, const unsigned int &radius)
Return a Haar Y intesity vector for the window centered at the given point.
Raster tie-points locator strategy factory base class.
std::multiset< InterestPointT > InterestPointsSetT
std::multiset< MatchedInterestPointsT > MatchedInterestPointsSetT
TerraLib.
#define TERPEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:139