cudafeatures2d.hpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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_CUDAFEATURES2D_HPP
  43. #define OPENCV_CUDAFEATURES2D_HPP
  44. #ifndef __cplusplus
  45. # error cudafeatures2d.hpp header must be compiled as C++
  46. #endif
  47. #include "opencv2/core/cuda.hpp"
  48. #include "opencv2/features2d.hpp"
  49. #include "opencv2/cudafilters.hpp"
  50. /**
  51. @addtogroup cuda
  52. @{
  53. @defgroup cudafeatures2d Feature Detection and Description
  54. @}
  55. */
  56. namespace cv { namespace cuda {
  57. //! @addtogroup cudafeatures2d
  58. //! @{
  59. //
  60. // DescriptorMatcher
  61. //
  62. /** @brief Abstract base class for matching keypoint descriptors.
  63. It has two groups of match methods: for matching descriptors of an image with another image or with
  64. an image set.
  65. */
  66. class CV_EXPORTS_W DescriptorMatcher : public cv::Algorithm
  67. {
  68. public:
  69. //
  70. // Factories
  71. //
  72. /** @brief Brute-force descriptor matcher.
  73. For each descriptor in the first set, this matcher finds the closest descriptor in the second set
  74. by trying each one. This descriptor matcher supports masking permissible matches of descriptor
  75. sets.
  76. @param normType One of NORM_L1, NORM_L2, NORM_HAMMING. L1 and L2 norms are
  77. preferable choices for SIFT and SURF descriptors, NORM_HAMMING should be used with ORB, BRISK and
  78. BRIEF).
  79. */
  80. CV_WRAP static Ptr<cuda::DescriptorMatcher> createBFMatcher(int normType = cv::NORM_L2);
  81. //
  82. // Utility
  83. //
  84. /** @brief Returns true if the descriptor matcher supports masking permissible matches.
  85. */
  86. CV_WRAP virtual bool isMaskSupported() const = 0;
  87. //
  88. // Descriptor collection
  89. //
  90. /** @brief Adds descriptors to train a descriptor collection.
  91. If the collection is not empty, the new descriptors are added to existing train descriptors.
  92. @param descriptors Descriptors to add. Each descriptors[i] is a set of descriptors from the same
  93. train image.
  94. */
  95. CV_WRAP virtual void add(const std::vector<GpuMat>& descriptors) = 0;
  96. /** @brief Returns a constant link to the train descriptor collection.
  97. */
  98. CV_WRAP virtual const std::vector<GpuMat>& getTrainDescriptors() const = 0;
  99. /** @brief Clears the train descriptor collection.
  100. */
  101. CV_WRAP virtual void clear() = 0;
  102. /** @brief Returns true if there are no train descriptors in the collection.
  103. */
  104. CV_WRAP virtual bool empty() const = 0;
  105. /** @brief Trains a descriptor matcher.
  106. Trains a descriptor matcher (for example, the flann index). In all methods to match, the method
  107. train() is run every time before matching.
  108. */
  109. CV_WRAP virtual void train() = 0;
  110. //
  111. // 1 to 1 match
  112. //
  113. /** @brief Finds the best match for each descriptor from a query set (blocking version).
  114. @param queryDescriptors Query set of descriptors.
  115. @param trainDescriptors Train set of descriptors. This set is not added to the train descriptors
  116. collection stored in the class object.
  117. @param matches Matches. If a query descriptor is masked out in mask , no match is added for this
  118. descriptor. So, matches size may be smaller than the query descriptors count.
  119. @param mask Mask specifying permissible matches between an input query and train matrices of
  120. descriptors.
  121. In the first variant of this method, the train descriptors are passed as an input argument. In the
  122. second variant of the method, train descriptors collection that was set by DescriptorMatcher::add is
  123. used. Optional mask (or masks) can be passed to specify which query and training descriptors can be
  124. matched. Namely, queryDescriptors[i] can be matched with trainDescriptors[j] only if
  125. mask.at\<uchar\>(i,j) is non-zero.
  126. */
  127. CV_WRAP virtual void match(InputArray queryDescriptors, InputArray trainDescriptors,
  128. CV_OUT std::vector<DMatch>& matches,
  129. InputArray mask = noArray()) = 0;
  130. /** @overload
  131. */
  132. CV_WRAP virtual void match(InputArray queryDescriptors,
  133. CV_OUT std::vector<DMatch>& matches,
  134. const std::vector<GpuMat>& masks = std::vector<GpuMat>()) = 0;
  135. /** @brief Finds the best match for each descriptor from a query set (asynchronous version).
  136. @param queryDescriptors Query set of descriptors.
  137. @param trainDescriptors Train set of descriptors. This set is not added to the train descriptors
  138. collection stored in the class object.
  139. @param matches Matches array stored in GPU memory. Internal representation is not defined.
  140. Use DescriptorMatcher::matchConvert method to retrieve results in standard representation.
  141. @param mask Mask specifying permissible matches between an input query and train matrices of
  142. descriptors.
  143. @param stream CUDA stream.
  144. In the first variant of this method, the train descriptors are passed as an input argument. In the
  145. second variant of the method, train descriptors collection that was set by DescriptorMatcher::add is
  146. used. Optional mask (or masks) can be passed to specify which query and training descriptors can be
  147. matched. Namely, queryDescriptors[i] can be matched with trainDescriptors[j] only if
  148. mask.at\<uchar\>(i,j) is non-zero.
  149. */
  150. CV_WRAP virtual void matchAsync(InputArray queryDescriptors, InputArray trainDescriptors,
  151. OutputArray matches,
  152. InputArray mask = noArray(),
  153. Stream& stream = Stream::Null()) = 0;
  154. /** @overload
  155. */
  156. CV_WRAP virtual void matchAsync(InputArray queryDescriptors,
  157. OutputArray matches,
  158. const std::vector<GpuMat>& masks = std::vector<GpuMat>(),
  159. Stream& stream = Stream::Null()) = 0;
  160. /** @brief Converts matches array from internal representation to standard matches vector.
  161. The method is supposed to be used with DescriptorMatcher::matchAsync to get final result.
  162. Call this method only after DescriptorMatcher::matchAsync is completed (ie. after synchronization).
  163. @param gpu_matches Matches, returned from DescriptorMatcher::matchAsync.
  164. @param matches Vector of DMatch objects.
  165. */
  166. CV_WRAP virtual void matchConvert(InputArray gpu_matches,
  167. CV_OUT std::vector<DMatch>& matches) = 0;
  168. //
  169. // knn match
  170. //
  171. /** @brief Finds the k best matches for each descriptor from a query set (blocking version).
  172. @param queryDescriptors Query set of descriptors.
  173. @param trainDescriptors Train set of descriptors. This set is not added to the train descriptors
  174. collection stored in the class object.
  175. @param matches Matches. Each matches[i] is k or less matches for the same query descriptor.
  176. @param k Count of best matches found per each query descriptor or less if a query descriptor has
  177. less than k possible matches in total.
  178. @param mask Mask specifying permissible matches between an input query and train matrices of
  179. descriptors.
  180. @param compactResult Parameter used when the mask (or masks) is not empty. If compactResult is
  181. false, the matches vector has the same size as queryDescriptors rows. If compactResult is true,
  182. the matches vector does not contain matches for fully masked-out query descriptors.
  183. These extended variants of DescriptorMatcher::match methods find several best matches for each query
  184. descriptor. The matches are returned in the distance increasing order. See DescriptorMatcher::match
  185. for the details about query and train descriptors.
  186. */
  187. CV_WRAP virtual void knnMatch(InputArray queryDescriptors, InputArray trainDescriptors,
  188. CV_OUT std::vector<std::vector<DMatch> >& matches,
  189. int k,
  190. InputArray mask = noArray(),
  191. bool compactResult = false) = 0;
  192. /** @overload
  193. */
  194. CV_WRAP virtual void knnMatch(InputArray queryDescriptors,
  195. CV_OUT std::vector<std::vector<DMatch> >& matches,
  196. int k,
  197. const std::vector<GpuMat>& masks = std::vector<GpuMat>(),
  198. bool compactResult = false) = 0;
  199. /** @brief Finds the k best matches for each descriptor from a query set (asynchronous version).
  200. @param queryDescriptors Query set of descriptors.
  201. @param trainDescriptors Train set of descriptors. This set is not added to the train descriptors
  202. collection stored in the class object.
  203. @param matches Matches array stored in GPU memory. Internal representation is not defined.
  204. Use DescriptorMatcher::knnMatchConvert method to retrieve results in standard representation.
  205. @param k Count of best matches found per each query descriptor or less if a query descriptor has
  206. less than k possible matches in total.
  207. @param mask Mask specifying permissible matches between an input query and train matrices of
  208. descriptors.
  209. @param stream CUDA stream.
  210. These extended variants of DescriptorMatcher::matchAsync methods find several best matches for each query
  211. descriptor. The matches are returned in the distance increasing order. See DescriptorMatcher::matchAsync
  212. for the details about query and train descriptors.
  213. */
  214. CV_WRAP virtual void knnMatchAsync(InputArray queryDescriptors, InputArray trainDescriptors,
  215. OutputArray matches,
  216. int k,
  217. InputArray mask = noArray(),
  218. Stream& stream = Stream::Null()) = 0;
  219. /** @overload
  220. */
  221. CV_WRAP virtual void knnMatchAsync(InputArray queryDescriptors,
  222. OutputArray matches,
  223. int k,
  224. const std::vector<GpuMat>& masks = std::vector<GpuMat>(),
  225. Stream& stream = Stream::Null()) = 0;
  226. /** @brief Converts matches array from internal representation to standard matches vector.
  227. The method is supposed to be used with DescriptorMatcher::knnMatchAsync to get final result.
  228. Call this method only after DescriptorMatcher::knnMatchAsync is completed (ie. after synchronization).
  229. @param gpu_matches Matches, returned from DescriptorMatcher::knnMatchAsync.
  230. @param matches Vector of DMatch objects.
  231. @param compactResult Parameter used when the mask (or masks) is not empty. If compactResult is
  232. false, the matches vector has the same size as queryDescriptors rows. If compactResult is true,
  233. the matches vector does not contain matches for fully masked-out query descriptors.
  234. */
  235. CV_WRAP virtual void knnMatchConvert(InputArray gpu_matches,
  236. CV_OUT std::vector< std::vector<DMatch> >& matches,
  237. bool compactResult = false) = 0;
  238. //
  239. // radius match
  240. //
  241. /** @brief For each query descriptor, finds the training descriptors not farther than the specified distance (blocking version).
  242. @param queryDescriptors Query set of descriptors.
  243. @param trainDescriptors Train set of descriptors. This set is not added to the train descriptors
  244. collection stored in the class object.
  245. @param matches Found matches.
  246. @param maxDistance Threshold for the distance between matched descriptors. Distance means here
  247. metric distance (e.g. Hamming distance), not the distance between coordinates (which is measured
  248. in Pixels)!
  249. @param mask Mask specifying permissible matches between an input query and train matrices of
  250. descriptors.
  251. @param compactResult Parameter used when the mask (or masks) is not empty. If compactResult is
  252. false, the matches vector has the same size as queryDescriptors rows. If compactResult is true,
  253. the matches vector does not contain matches for fully masked-out query descriptors.
  254. For each query descriptor, the methods find such training descriptors that the distance between the
  255. query descriptor and the training descriptor is equal or smaller than maxDistance. Found matches are
  256. returned in the distance increasing order.
  257. */
  258. CV_WRAP virtual void radiusMatch(InputArray queryDescriptors, InputArray trainDescriptors,
  259. CV_OUT std::vector<std::vector<DMatch> >& matches,
  260. float maxDistance,
  261. InputArray mask = noArray(),
  262. bool compactResult = false) = 0;
  263. /** @overload
  264. */
  265. CV_WRAP virtual void radiusMatch(InputArray queryDescriptors,
  266. CV_OUT std::vector<std::vector<DMatch> >& matches,
  267. float maxDistance,
  268. const std::vector<GpuMat>& masks = std::vector<GpuMat>(),
  269. bool compactResult = false) = 0;
  270. /** @brief For each query descriptor, finds the training descriptors not farther than the specified distance (asynchronous version).
  271. @param queryDescriptors Query set of descriptors.
  272. @param trainDescriptors Train set of descriptors. This set is not added to the train descriptors
  273. collection stored in the class object.
  274. @param matches Matches array stored in GPU memory. Internal representation is not defined.
  275. Use DescriptorMatcher::radiusMatchConvert method to retrieve results in standard representation.
  276. @param maxDistance Threshold for the distance between matched descriptors. Distance means here
  277. metric distance (e.g. Hamming distance), not the distance between coordinates (which is measured
  278. in Pixels)!
  279. @param mask Mask specifying permissible matches between an input query and train matrices of
  280. descriptors.
  281. @param stream CUDA stream.
  282. For each query descriptor, the methods find such training descriptors that the distance between the
  283. query descriptor and the training descriptor is equal or smaller than maxDistance. Found matches are
  284. returned in the distance increasing order.
  285. */
  286. CV_WRAP virtual void radiusMatchAsync(InputArray queryDescriptors, InputArray trainDescriptors,
  287. OutputArray matches,
  288. float maxDistance,
  289. InputArray mask = noArray(),
  290. Stream& stream = Stream::Null()) = 0;
  291. /** @overload
  292. */
  293. CV_WRAP virtual void radiusMatchAsync(InputArray queryDescriptors,
  294. OutputArray matches,
  295. float maxDistance,
  296. const std::vector<GpuMat>& masks = std::vector<GpuMat>(),
  297. Stream& stream = Stream::Null()) = 0;
  298. /** @brief Converts matches array from internal representation to standard matches vector.
  299. The method is supposed to be used with DescriptorMatcher::radiusMatchAsync to get final result.
  300. Call this method only after DescriptorMatcher::radiusMatchAsync is completed (ie. after synchronization).
  301. @param gpu_matches Matches, returned from DescriptorMatcher::radiusMatchAsync.
  302. @param matches Vector of DMatch objects.
  303. @param compactResult Parameter used when the mask (or masks) is not empty. If compactResult is
  304. false, the matches vector has the same size as queryDescriptors rows. If compactResult is true,
  305. the matches vector does not contain matches for fully masked-out query descriptors.
  306. */
  307. CV_WRAP virtual void radiusMatchConvert(InputArray gpu_matches,
  308. CV_OUT std::vector< std::vector<DMatch> >& matches,
  309. bool compactResult = false) = 0;
  310. };
  311. //
  312. // Feature2DAsync
  313. //
  314. /** @brief Abstract base class for CUDA asynchronous 2D image feature detectors and descriptor extractors.
  315. */
  316. class CV_EXPORTS_W Feature2DAsync : public cv::Feature2D
  317. {
  318. public:
  319. CV_WRAP virtual ~Feature2DAsync();
  320. /** @brief Detects keypoints in an image.
  321. @param image Image.
  322. @param keypoints The detected keypoints.
  323. @param mask Mask specifying where to look for keypoints (optional). It must be a 8-bit integer
  324. matrix with non-zero values in the region of interest.
  325. @param stream CUDA stream.
  326. */
  327. CV_WRAP virtual void detectAsync(InputArray image,
  328. OutputArray keypoints,
  329. InputArray mask = noArray(),
  330. Stream& stream = Stream::Null());
  331. /** @brief Computes the descriptors for a set of keypoints detected in an image.
  332. @param image Image.
  333. @param keypoints Input collection of keypoints.
  334. @param descriptors Computed descriptors. Row j is the descriptor for j-th keypoint.
  335. @param stream CUDA stream.
  336. */
  337. CV_WRAP virtual void computeAsync(InputArray image,
  338. OutputArray keypoints,
  339. OutputArray descriptors,
  340. Stream& stream = Stream::Null());
  341. /** Detects keypoints and computes the descriptors. */
  342. CV_WRAP virtual void detectAndComputeAsync(InputArray image,
  343. InputArray mask,
  344. OutputArray keypoints,
  345. OutputArray descriptors,
  346. bool useProvidedKeypoints = false,
  347. Stream& stream = Stream::Null());
  348. /** Converts keypoints array from internal representation to standard vector. */
  349. CV_WRAP virtual void convert(InputArray gpu_keypoints,
  350. CV_OUT std::vector<KeyPoint>& keypoints) = 0;
  351. };
  352. //
  353. // FastFeatureDetector
  354. //
  355. /** @brief Wrapping class for feature detection using the FAST method.
  356. */
  357. class CV_EXPORTS_W FastFeatureDetector : public Feature2DAsync
  358. {
  359. public:
  360. static const int LOCATION_ROW = 0;
  361. static const int RESPONSE_ROW = 1;
  362. static const int ROWS_COUNT = 2;
  363. static const int FEATURE_SIZE = 7;
  364. CV_WRAP static Ptr<cuda::FastFeatureDetector> create(int threshold=10,
  365. bool nonmaxSuppression=true,
  366. int type=cv::FastFeatureDetector::TYPE_9_16,
  367. int max_npoints = 5000);
  368. CV_WRAP virtual void setThreshold(int threshold) = 0;
  369. CV_WRAP virtual void setMaxNumPoints(int max_npoints) = 0;
  370. CV_WRAP virtual int getMaxNumPoints() const = 0;
  371. };
  372. //
  373. // ORB
  374. //
  375. /** @brief Class implementing the ORB (*oriented BRIEF*) keypoint detector and descriptor extractor
  376. *
  377. * @sa cv::ORB
  378. */
  379. class CV_EXPORTS_W ORB : public Feature2DAsync
  380. {
  381. public:
  382. static const int X_ROW = 0;
  383. static const int Y_ROW = 1;
  384. static const int RESPONSE_ROW = 2;
  385. static const int ANGLE_ROW = 3;
  386. static const int OCTAVE_ROW = 4;
  387. static const int SIZE_ROW = 5;
  388. static const int ROWS_COUNT = 6;
  389. CV_WRAP static Ptr<cuda::ORB> create(int nfeatures=500,
  390. float scaleFactor=1.2f,
  391. int nlevels=8,
  392. int edgeThreshold=31,
  393. int firstLevel=0,
  394. int WTA_K=2,
  395. int scoreType=cv::ORB::HARRIS_SCORE,
  396. int patchSize=31,
  397. int fastThreshold=20,
  398. bool blurForDescriptor=false);
  399. //! if true, image will be blurred before descriptors calculation
  400. CV_WRAP virtual void setBlurForDescriptor(bool blurForDescriptor) = 0;
  401. CV_WRAP virtual bool getBlurForDescriptor() const = 0;
  402. CV_WRAP virtual void setFastThreshold(int fastThreshold) = 0;
  403. CV_WRAP virtual int getFastThreshold() const = 0;
  404. };
  405. //! @}
  406. }} // namespace cv { namespace cuda {
  407. #endif /* OPENCV_CUDAFEATURES2D_HPP */