All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
29 
30 #include <boost/thread.hpp>
31 
32 #include <cmath>
33 
34 namespace te
35 {
36  namespace rp
37  {
38  // Forwards
39  class TiePointsLocator;
40 
41  /*!
42  \class TiePointsLocatorSURFStrategy
43  \brief Tie-points locator SURF strategy.
44  */
46  {
47  friend class TiePointsLocator;
48 
49  public:
50 
52 
53  protected :
54 
55  /*!
56  \brief The parameters passed to the surfLocatorThreadEntry method.
57  */
59  {
60  public :
61 
62  bool* m_returnValuePtr; //! Thread return value pointer.
63 
64  unsigned int m_scalesNumber; //!< The number of sub-samples scales to generate (minimum:3).
65 
66  unsigned int m_octavesNumber; //!< The number of octaves to generate (minimum:1).
67 
68  std::vector< InterestPointsSetT >* m_interestPointsSubSectorsPtr; //!< A pointer to a valid interest points container (one element by subsector)..
69 
70  unsigned int m_maxInterestPointsBySubSector; //!< The maximum number of interest points by sub-sector.
71 
72  unsigned int m_tiePointsSubSectorsSplitFactor; //!< The number of sectors along each direction.
73 
74  FloatsMatrix const* m_integralRasterDataPtr; //!< The integral image raster data.
75 
76  UCharsMatrix const* m_maskRasterDataPtr; //!< The loaded mask raster data pointer (or zero if no mask is avaliable).
77 
78  boost::mutex* m_rastaDataAccessMutexPtr; //!< A pointer to a valid mutex to controle raster data access.
79 
80  boost::mutex* m_interestPointsAccessMutexPtr; //!< A pointer to a valid mutex to control the output interest points container access.
81 
82  unsigned int m_processingBlocksNumber; //!< The raster data will be splitted into this number of blocks for processing.
83 
84  unsigned int* m_nextRasterLinesBlockToProcessValuePtr; //!< A pointer to a valid counter to control the blocks processing sequence.
85 
87 
89  };
90 
91  /*!
92  \brief The parameters passed to the executeMatchingByEuclideanDistThreadEntry method.
93  */
95  {
96  public :
97 
99 
101 
103 
105 
107 
109 
110  boost::mutex* m_syncMutexPtr;
111 
112  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.
113 
114  double m_searchOptTreeSearchRadius; //!< Optimization tree search radius (pixels).
115 
117 
119  };
120 
121  bool m_isInitialized; //!< true if this instance is initialized.
122 
124 
126 
127  //overload
128  bool initialize(
129  const te::rp::TiePointsLocatorInputParameters& inputParameters );
130 
131  //overload
132  void reset();
133 
134  //overload
135  bool getMatchedInterestPoints(
136  te::gm::GeometricTransformation const * const raster1ToRaster2TransfPtr,
137  const double raster1ToRaster2TransfDMapError,
138  MatchedInterestPointsSetT& matchedInterestPoints );
139 
140  //overload
141  unsigned int getAutoMaxTiePointsNumber() const;
142 
143  /*!
144  \brief Create an integral image.
145 
146  \param inputData The input data.
147 
148  \param outputData The output data.
149 
150  \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.
151 
152  \return true if ok, false on errors.
153  */
154  static bool createIntegralImage(
155  const FloatsMatrix& inputData,
156  FloatsMatrix& outputData );
157 
158  /*!
159  \brief SURF interest points locator.
160 
161  \param maxInterestPoints The maximum number of interest points to find.
162 
163  \param integralRasterData Integral image raster data.
164 
165  \param maskRasterDataPtr The loaded mask raster data pointer (or zero if no mask is avaliable).
166 
167  \param interestPoints The found interest points (coords related to rasterData lines/cols).
168 
169  \return true if ok, false on errors.
170 
171  \note InterestPointT::m_feature1 will be sum of differences between the hessian matrix determinant each pixel and its neighborhoods (always a positive value).
172 
173  \note InterestPointT::m_feature2 will be used filter width (pixels).
174 
175  \note InterestPointT::m_feature3 will 1 if the laplacian sign is positive, or zero if negative.
176  */
177  bool locateSurfInterestPoints(
178  const unsigned int maxInterestPoints,
179  const FloatsMatrix& integralRasterData,
180  UCharsMatrix const* maskRasterDataPtr,
181  InterestPointsSetT& interestPoints ) const;
182 
183  /*!
184  \brief Surf locator thread entry.
185 
186  \param paramsPtr A pointer to the thread parameters.
187  */
188  static void locateSurfInterestPointsThreadEntry(SurfLocatorThreadParams* paramsPtr);
189 
190  /*!
191  \brief Generate a Surf features matrix for the given interes points.
192 
193  \param interestPoints The interest points (coords related to rasterData lines/cols).
194 
195  \param integralRasterData The integral raster data.
196 
197  \param validInterestPoints The valid interest points.
198 
199  \param features The generated features matrix (one feature per line, one feature per interest point).
200 
201  \return true if ok, false on errors.
202  */
203  static bool generateSurfFeatures(
204  const InterestPointsSetT& interestPoints,
205  const FloatsMatrix& integralRasterData,
206  InterestPointsSetT& validInterestPoints,
207  FloatsMatrix& features );
208 
209  /*!
210  \brief Match each feature using eucliean distance.
211 
212  \param featuresSet1 Features set 1.
213 
214  \param featuresSet2 Features set 2.
215 
216  \param interestPointsSet1 The interest pionts set 1 (full raster 1 indexed coods reference)..
217 
218  \param interestPointsSet2 The interest pionts set 2 (full raster 1 indexed coods reference)..
219 
220  \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.
221 
222  \param raster1ToRaster2TransfDMapError The expected transformation error.
223 
224  \param matchedPoints The matched points.
225 
226  \note Each matched point feature value ( MatchedInterestPoint::m_feature ) will be set to the distance value between both features.
227  */
228  bool executeMatchingByEuclideanDist(
229  const FloatsMatrix& featuresSet1,
230  const FloatsMatrix& featuresSet2,
231  const InterestPointsSetT& interestPointsSet1,
232  const InterestPointsSetT& interestPointsSet2,
233  te::gm::GeometricTransformation const * const raster1ToRaster2TransfPtr,
234  const double raster1ToRaster2TransfDMapError,
235  MatchedInterestPointsSetT& matchedPoints ) const;
236 
237  /*!
238  \brief Correlation/Euclidean match thread entry.
239 
240  \param paramsPtr A pointer to the thread parameters.
241  */
242  static void executeMatchingByEuclideanDistThreadEntry(
244 
245  /*!
246  \brief Return a sum of all pixels inside a box over the given integral image buffer.
247 
248  \param bufferPtr Buffer pointer.
249 
250  \param upperLeftX Box upper left X.
251 
252  \param upperLeftY Box upper left Y.
253 
254  \param lowerRightX Box lower right X.
255 
256  \param lowerRightY Box lower right X.
257  */
258  template< typename BufferType >
259  inline static float getIntegralBoxSum( BufferType& buffer,
260  const unsigned int& upperLeftX, const unsigned int& upperLeftY,
261  const unsigned int& lowerRightX, const unsigned int& lowerRightY )
262  {
263  return buffer[ lowerRightY ][ lowerRightX ]
264  - ( ( upperLeftY ) ? buffer[ upperLeftY - 1 ][ lowerRightX ] : 0 )
265  - ( ( upperLeftX ) ? buffer[ lowerRightY ][ upperLeftX - 1 ] : 0 )
266  + ( ( ( upperLeftX != 0 ) && ( upperLeftY != 0 ) ) ? buffer[ upperLeftY - 1 ][ upperLeftX - 1 ] : 0 );
267  }
268 
269  /*!
270  \brief Return a SURF box filter Dxx derivative centered over the given position from the given integral image buffer.
271 
272  \param bufferPtr Buffer pointer.
273 
274  \param centerX Center X.
275 
276  \param centerY Center Y.
277 
278  \param lobeWidth Filter lobe width.
279 
280  \param lobeRadius Filter lobe radius.
281  */
282  inline static float getSurfDyyDerivative( float** bufferPtr,
283  const unsigned int& centerX, const unsigned int& centerY,
284  const unsigned int& lobeWidth, const unsigned int& lobeRadius )
285  {
286  return
287  getIntegralBoxSum(
288  bufferPtr,
289  ( centerX + 1 - lobeWidth ),
290  ( centerY - lobeWidth - lobeRadius ),
291  ( centerX + lobeWidth - 1 ),
292  ( centerY + lobeWidth + lobeRadius ) )
293  - ( 3.0f *
294  getIntegralBoxSum(
295  bufferPtr,
296  ( centerX + 1 - lobeWidth ),
297  ( centerY - lobeRadius ),
298  ( centerX + lobeWidth - 1 ),
299  ( centerY + lobeRadius ) ) );
300  };
301 
302  /*!
303  \brief Return a SURF box filter Dyy derivative centered over the given position from the given integral image buffer.
304 
305  \param bufferPtr Buffer pointer.
306 
307  \param centerX Center X.
308 
309  \param centerY Center Y.
310 
311  \param lobeWidth Filter lobe width.
312 
313  \param lobeRadius Filter lobe radius.
314  */
315  inline static float getSurfDxxDerivative( float** bufferPtr,
316  const unsigned int& centerX, const unsigned int& centerY,
317  const unsigned int& lobeWidth, const unsigned int& lobeRadius )
318  {
319  return
320  getIntegralBoxSum(
321  bufferPtr,
322  ( centerX - lobeWidth - lobeRadius ),
323  ( centerY + 1 - lobeWidth ),
324  ( centerX + lobeWidth + lobeRadius ),
325  ( centerY + lobeWidth - 1 ) )
326  - 3.0f *
327  getIntegralBoxSum(
328  bufferPtr,
329  ( centerX - lobeRadius ),
330  ( centerY + 1 - lobeWidth ),
331  ( centerX + lobeRadius ),
332  ( centerY + lobeWidth - 1 ) );
333  };
334 
335  /*!
336  \brief Return a SURF box filter Dxy derivative centered over the given position from the given integral image buffer.
337 
338  \param bufferPtr Buffer pointer.
339 
340  \param centerX Center X.
341 
342  \param centerY Center Y.
343 
344  \param lobeWidth Filter lobe width.
345  */
346  inline static float getSurfDxyDerivative( float** bufferPtr,
347  const unsigned int& centerX, const unsigned int& centerY,
348  const unsigned int& lobeWidth )
349  {
350  return
351  // upper-left filter lobe
352  getIntegralBoxSum(
353  bufferPtr,
354  ( centerX - lobeWidth ),
355  ( centerY - lobeWidth ),
356  ( centerX - 1 ),
357  ( centerY - 1 ) )
358  +
359  // lower-right filter lobe
360  getIntegralBoxSum(
361  bufferPtr,
362  ( centerX + 1 ),
363  ( centerY + 1 ),
364  ( centerX + lobeWidth ),
365  ( centerY + lobeWidth ) )
366  -
367  // upper-right filter lobe
368  getIntegralBoxSum(
369  bufferPtr,
370  ( centerX + 1 ),
371  ( centerY - lobeWidth ),
372  ( centerX + lobeWidth ),
373  ( centerY - 1 ) )
374  -
375  // lower-left filter lobe
376  getIntegralBoxSum(
377  bufferPtr,
378  ( centerX - lobeWidth ),
379  ( centerY + 1 ),
380  ( centerX - 1 ),
381  ( centerY + lobeWidth ) );
382  };
383 
384  /*!
385  \brief Return a Haar X intesity vector for the window centered at the given point.
386 
387  \param buffer Integral image buffer.
388 
389  \param centerX Center X.
390 
391  \param centerY Center Y.
392 
393  \param radius Window radius.
394  */
395  template< typename BufferType >
396  inline static float getHaarXVectorIntensity( BufferType& buffer,
397  const unsigned int& centerX, const unsigned int& centerY,
398  const unsigned int& radius )
399  {
400  return
401  getIntegralBoxSum(
402  buffer,
403  ( centerX + 1 ),
404  ( centerY - radius ),
405  ( centerX + radius ),
406  ( centerY + radius ) )
407  -
408  getIntegralBoxSum(
409  buffer,
410  ( centerX - radius ),
411  ( centerY - radius ),
412  ( centerX - 1 ),
413  ( centerY + radius ) );
414  }
415 
416  /*!
417  \brief Return a Haar Y intesity vector for the window centered at the given point.
418 
419  \param buffer Integral image buffer.
420 
421  \param centerX Center X.
422 
423  \param centerY Center Y.
424 
425  \param radius Window radius.
426  */
427  template< typename BufferType >
428  inline static float getHaarYVectorIntensity( BufferType& buffer,
429  const unsigned int& centerX, const unsigned int& centerY,
430  const unsigned int& radius )
431  {
432  return
433  getIntegralBoxSum(
434  buffer,
435  ( centerX - radius ),
436  ( centerY - radius ),
437  ( centerX + radius ),
438  ( centerY - 1 ) )
439  -
440  getIntegralBoxSum(
441  buffer,
442  ( centerX - radius ),
443  ( centerY + 1 ),
444  ( centerX + radius ),
445  ( centerY + radius ) );
446  }
447 
448  /*!
449  \brief Return the surf octave filter step size (width).
450 
451  \param octaveIndex Octave index (starting from zero).
452  */
453  inline static unsigned int getSurfOctaveFilterStepSize( const unsigned int& octaveIndex )
454  {
455  return (unsigned int)std::pow( 2.0, (double)(octaveIndex + 1) );
456  };
457 
458  /*!
459  \brief Return the surf octave base filter size (width).
460 
461  \param octaveIndex Octave index (starting from zero).
462  */
463  inline static unsigned int getSurfOctaveBaseFilterSize( const unsigned int& octaveIndex )
464  {
465  return 3 + ( 3 * getSurfOctaveFilterStepSize( octaveIndex ) );
466  };
467 
468  /*!
469  \brief Return the surf octave filter size (width).
470 
471  \param octaveIndex Octave index (starting from zero).
472 
473  \param scaleIndex Scale index (starting from zero).
474  */
475  inline static unsigned int getSurfFilterSize( const unsigned int& octaveIndex,
476  const unsigned int& scaleIndex )
477  {
478  return getSurfOctaveBaseFilterSize( octaveIndex ) +
479  ( 3 * ( getSurfOctaveFilterStepSize( octaveIndex ) * scaleIndex ) );
480  };
481  };
482 
483  } // end namespace rp
484 } // end namespace te
485 
486 #endif
487 
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 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...
te::gm::GeometricTransformation const * m_raster1ToRaster2TransfPtr
A pointer to a transformation direct mapping raster 1 indexed coords into raster 2 indexed coords...
unsigned int m_tiePointsSubSectorsSplitFactor
The number of sectors along each direction.
2D Geometric transformation base class.
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...
Tie-Pointsr locator strategy.
unsigned int m_processingBlocksNumber
The raster data will be splitted into this number of blocks for processing.
unsigned int m_octavesNumber
The number of octaves to generate (minimum:1).
std::vector< InterestPointsSetT > * m_interestPointsSubSectorsPtr
A pointer to a valid interest points container (one element by subsector)..
static unsigned int getSurfOctaveBaseFilterSize(const unsigned int &octaveIndex)
Return the surf octave base filter size (width).
boost::mutex * m_rastaDataAccessMutexPtr
A pointer to a valid mutex to controle raster data access.
static unsigned int getSurfFilterSize(const unsigned int &octaveIndex, const unsigned int &scaleIndex)
Return the surf octave filter size (width).
te::rp::TiePointsLocatorInputParameters m_inputParameters
Input parameters.
The parameters passed to the executeMatchingByEuclideanDistThreadEntry method.
std::multiset< InterestPointT > InterestPointsSetT
#define TERPEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:141
FloatsMatrix const * m_integralRasterDataPtr
The integral image raster data.
Tie-points locator SURF strategy.
Tie-points locator strategy.
Tie points locator.
The parameters passed to the surfLocatorThreadEntry method.
UCharsMatrix const * m_maskRasterDataPtr
The loaded mask raster data pointer (or zero if no mask is avaliable).
A generic template matrix.
Definition: Matrix.h:51
boost::mutex * m_interestPointsAccessMutexPtr
A pointer to a valid mutex to control the output interest points container access.
unsigned int m_maxInterestPointsBySubSector
The maximum number of interest points by sub-sector.
bool m_isInitialized
true if this instance is initialized.
unsigned int * m_nextRasterLinesBlockToProcessValuePtr
A pointer to a valid counter to control the blocks processing sequence.
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 unsigned int getSurfOctaveFilterStepSize(const unsigned int &octaveIndex)
Return the surf octave filter step size (width).
std::multiset< MatchedInterestPointsT > MatchedInterestPointsSetT
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.
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.