cuda.hpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
  14. // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
  15. // Third party copyrights are property of their respective owners.
  16. //
  17. // Redistribution and use in source and binary forms, with or without modification,
  18. // are permitted provided that the following conditions are met:
  19. //
  20. // * Redistribution's of source code must retain the above copyright notice,
  21. // this list of conditions and the following disclaimer.
  22. //
  23. // * Redistribution's in binary form must reproduce the above copyright notice,
  24. // this list of conditions and the following disclaimer in the documentation
  25. // and/or other materials provided with the distribution.
  26. //
  27. // * The name of the copyright holders may not be used to endorse or promote products
  28. // derived from this software without specific prior written permission.
  29. //
  30. // This software is provided by the copyright holders and contributors "as is" and
  31. // any express or implied warranties, including, but not limited to, the implied
  32. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  33. // In no event shall the Intel Corporation or contributors be liable for any direct,
  34. // indirect, incidental, special, exemplary, or consequential damages
  35. // (including, but not limited to, procurement of substitute goods or services;
  36. // loss of use, data, or profits; or business interruption) however caused
  37. // and on any theory of liability, whether in contract, strict liability,
  38. // or tort (including negligence or otherwise) arising in any way out of
  39. // the use of this software, even if advised of the possibility of such damage.
  40. //
  41. //M*/
  42. #ifndef __OPENCV_XFEATURES2D_CUDA_HPP__
  43. #define __OPENCV_XFEATURES2D_CUDA_HPP__
  44. #include "opencv2/core/cuda.hpp"
  45. namespace cv { namespace cuda {
  46. //! @addtogroup xfeatures2d_nonfree
  47. //! @{
  48. /** @brief Class used for extracting Speeded Up Robust Features (SURF) from an image. :
  49. The class SURF_CUDA implements Speeded Up Robust Features descriptor. There is a fast multi-scale
  50. Hessian keypoint detector that can be used to find the keypoints (which is the default option). But
  51. the descriptors can also be computed for the user-specified keypoints. Only 8-bit grayscale images
  52. are supported.
  53. The class SURF_CUDA can store results in the GPU and CPU memory. It provides functions to convert
  54. results between CPU and GPU version ( uploadKeypoints, downloadKeypoints, downloadDescriptors ). The
  55. format of CPU results is the same as SURF results. GPU results are stored in GpuMat. The keypoints
  56. matrix is \f$\texttt{nFeatures} \times 7\f$ matrix with the CV_32FC1 type.
  57. - keypoints.ptr\<float\>(X_ROW)[i] contains x coordinate of the i-th feature.
  58. - keypoints.ptr\<float\>(Y_ROW)[i] contains y coordinate of the i-th feature.
  59. - keypoints.ptr\<float\>(LAPLACIAN_ROW)[i] contains the laplacian sign of the i-th feature.
  60. - keypoints.ptr\<float\>(OCTAVE_ROW)[i] contains the octave of the i-th feature.
  61. - keypoints.ptr\<float\>(SIZE_ROW)[i] contains the size of the i-th feature.
  62. - keypoints.ptr\<float\>(ANGLE_ROW)[i] contain orientation of the i-th feature.
  63. - keypoints.ptr\<float\>(HESSIAN_ROW)[i] contains the response of the i-th feature.
  64. The descriptors matrix is \f$\texttt{nFeatures} \times \texttt{descriptorSize}\f$ matrix with the
  65. CV_32FC1 type.
  66. The class SURF_CUDA uses some buffers and provides access to it. All buffers can be safely released
  67. between function calls.
  68. @sa SURF
  69. @note
  70. - An example for using the SURF keypoint matcher on GPU can be found at
  71. opencv_source_code/samples/gpu/surf_keypoint_matcher.cpp
  72. */
  73. class CV_EXPORTS_W SURF_CUDA
  74. {
  75. public:
  76. enum KeypointLayout
  77. {
  78. X_ROW = 0,
  79. Y_ROW,
  80. LAPLACIAN_ROW,
  81. OCTAVE_ROW,
  82. SIZE_ROW,
  83. ANGLE_ROW,
  84. HESSIAN_ROW,
  85. ROWS_COUNT
  86. };
  87. //! the default constructor
  88. SURF_CUDA();
  89. //! the full constructor taking all the necessary parameters
  90. explicit SURF_CUDA(double _hessianThreshold, int _nOctaves=4,
  91. int _nOctaveLayers=2, bool _extended=false, float _keypointsRatio=0.01f, bool _upright = false);
  92. /**
  93. @param _hessianThreshold Threshold for hessian keypoint detector used in SURF.
  94. @param _nOctaves Number of pyramid octaves the keypoint detector will use.
  95. @param _nOctaveLayers Number of octave layers within each octave.
  96. @param _extended Extended descriptor flag (true - use extended 128-element descriptors; false - use
  97. 64-element descriptors).
  98. @param _keypointsRatio Limits a maximum number of features
  99. @param _upright Up-right or rotated features flag (true - do not compute orientation of features;
  100. false - compute orientation).
  101. */
  102. CV_WRAP static Ptr<SURF_CUDA> create(double _hessianThreshold, int _nOctaves = 4,
  103. int _nOctaveLayers = 2, bool _extended = false, float _keypointsRatio = 0.01f, bool _upright = false);
  104. //! returns the descriptor size in float's (64 or 128)
  105. CV_WRAP int descriptorSize() const;
  106. //! returns the default norm type
  107. CV_WRAP int defaultNorm() const;
  108. //! upload host keypoints to device memory
  109. void uploadKeypoints(const std::vector<KeyPoint>& keypoints, GpuMat& keypointsGPU);
  110. //! download keypoints from device to host memory
  111. CV_WRAP void downloadKeypoints(const GpuMat& keypointsGPU, CV_OUT std::vector<KeyPoint>& keypoints);
  112. //! download descriptors from device to host memory
  113. void downloadDescriptors(const GpuMat& descriptorsGPU, std::vector<float>& descriptors);
  114. //! finds the keypoints using fast hessian detector used in SURF
  115. //! supports CV_8UC1 images
  116. //! keypoints will have nFeature cols and 6 rows
  117. //! keypoints.ptr<float>(X_ROW)[i] will contain x coordinate of i'th feature
  118. //! keypoints.ptr<float>(Y_ROW)[i] will contain y coordinate of i'th feature
  119. //! keypoints.ptr<float>(LAPLACIAN_ROW)[i] will contain laplacian sign of i'th feature
  120. //! keypoints.ptr<float>(OCTAVE_ROW)[i] will contain octave of i'th feature
  121. //! keypoints.ptr<float>(SIZE_ROW)[i] will contain size of i'th feature
  122. //! keypoints.ptr<float>(ANGLE_ROW)[i] will contain orientation of i'th feature
  123. //! keypoints.ptr<float>(HESSIAN_ROW)[i] will contain response of i'th feature
  124. void operator()(const GpuMat& img, const GpuMat& mask, GpuMat& keypoints);
  125. //! finds the keypoints and computes their descriptors.
  126. //! Optionally it can compute descriptors for the user-provided keypoints and recompute keypoints direction
  127. void operator()(const GpuMat& img, const GpuMat& mask, GpuMat& keypoints, GpuMat& descriptors,
  128. bool useProvidedKeypoints = false);
  129. /** @brief Finds the keypoints using fast hessian detector used in SURF
  130. @param img Source image, currently supports only CV_8UC1 images.
  131. @param mask A mask image same size as src and of type CV_8UC1.
  132. @param keypoints Detected keypoints.
  133. */
  134. CV_WRAP inline void detect(const GpuMat& img, const GpuMat& mask, CV_OUT GpuMat& keypoints) {
  135. (*this)(img, mask, keypoints);
  136. }
  137. void operator()(const GpuMat& img, const GpuMat& mask, std::vector<KeyPoint>& keypoints);
  138. void operator()(const GpuMat& img, const GpuMat& mask, std::vector<KeyPoint>& keypoints, GpuMat& descriptors,
  139. bool useProvidedKeypoints = false);
  140. /** @brief Finds the keypoints and computes their descriptors using fast hessian detector used in SURF
  141. @param img Source image, currently supports only CV_8UC1 images.
  142. @param mask A mask image same size as src and of type CV_8UC1.
  143. @param keypoints Detected keypoints.
  144. @param descriptors Keypoint descriptors.
  145. @param useProvidedKeypoints Compute descriptors for the user-provided keypoints and recompute keypoints direction.
  146. */
  147. CV_WRAP inline void detectWithDescriptors(const GpuMat& img, const GpuMat& mask, CV_OUT GpuMat& keypoints, CV_OUT GpuMat& descriptors,
  148. bool useProvidedKeypoints = false) {
  149. (*this)(img, mask, keypoints, descriptors, useProvidedKeypoints);
  150. }
  151. void operator()(const GpuMat& img, const GpuMat& mask, std::vector<KeyPoint>& keypoints, std::vector<float>& descriptors,
  152. bool useProvidedKeypoints = false);
  153. void releaseMemory();
  154. // SURF parameters
  155. CV_PROP double hessianThreshold;
  156. CV_PROP int nOctaves;
  157. CV_PROP int nOctaveLayers;
  158. CV_PROP bool extended;
  159. CV_PROP bool upright;
  160. //! max keypoints = min(keypointsRatio * img.size().area(), 65535)
  161. CV_PROP float keypointsRatio;
  162. GpuMat sum, mask1, maskSum;
  163. GpuMat det, trace;
  164. GpuMat maxPosBuffer;
  165. };
  166. //! @}
  167. }} // namespace cv { namespace cuda {
  168. #endif // __OPENCV_XFEATURES2D_CUDA_HPP__