matchers.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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_STITCHING_MATCHERS_HPP
  43. #define OPENCV_STITCHING_MATCHERS_HPP
  44. #include "opencv2/core.hpp"
  45. #include "opencv2/features2d.hpp"
  46. #include "opencv2/opencv_modules.hpp"
  47. #ifdef HAVE_OPENCV_XFEATURES2D
  48. # include "opencv2/xfeatures2d/cuda.hpp"
  49. #endif
  50. namespace cv {
  51. namespace detail {
  52. //! @addtogroup stitching_match
  53. //! @{
  54. /** @brief Structure containing image keypoints and descriptors. */
  55. struct CV_EXPORTS ImageFeatures
  56. {
  57. int img_idx;
  58. Size img_size;
  59. std::vector<KeyPoint> keypoints;
  60. UMat descriptors;
  61. };
  62. /** @brief Feature finders base class */
  63. class CV_EXPORTS FeaturesFinder
  64. {
  65. public:
  66. virtual ~FeaturesFinder() {}
  67. /** @overload */
  68. void operator ()(InputArray image, ImageFeatures &features);
  69. /** @brief Finds features in the given image.
  70. @param image Source image
  71. @param features Found features
  72. @param rois Regions of interest
  73. @sa detail::ImageFeatures, Rect_
  74. */
  75. void operator ()(InputArray image, ImageFeatures &features, const std::vector<cv::Rect> &rois);
  76. /** @brief Finds features in the given images in parallel.
  77. @param images Source images
  78. @param features Found features for each image
  79. @param rois Regions of interest for each image
  80. @sa detail::ImageFeatures, Rect_
  81. */
  82. void operator ()(InputArrayOfArrays images, std::vector<ImageFeatures> &features,
  83. const std::vector<std::vector<cv::Rect> > &rois);
  84. /** @overload */
  85. void operator ()(InputArrayOfArrays images, std::vector<ImageFeatures> &features);
  86. /** @brief Frees unused memory allocated before if there is any. */
  87. virtual void collectGarbage() {}
  88. /* TODO OpenCV ABI 4.x
  89. reimplement this as public method similar to FeaturesMatcher and remove private function hack
  90. @return True, if it's possible to use the same finder instance in parallel, false otherwise
  91. bool isThreadSafe() const { return is_thread_safe_; }
  92. */
  93. protected:
  94. /** @brief This method must implement features finding logic in order to make the wrappers
  95. detail::FeaturesFinder::operator()_ work.
  96. @param image Source image
  97. @param features Found features
  98. @sa detail::ImageFeatures */
  99. virtual void find(InputArray image, ImageFeatures &features) = 0;
  100. /** @brief uses dynamic_cast to determine thread-safety
  101. @return True, if it's possible to use the same finder instance in parallel, false otherwise
  102. */
  103. bool isThreadSafe() const;
  104. };
  105. /** @brief SURF features finder.
  106. @sa detail::FeaturesFinder, SURF
  107. */
  108. class CV_EXPORTS SurfFeaturesFinder : public FeaturesFinder
  109. {
  110. public:
  111. SurfFeaturesFinder(double hess_thresh = 300., int num_octaves = 3, int num_layers = 4,
  112. int num_octaves_descr = /*4*/3, int num_layers_descr = /*2*/4);
  113. private:
  114. void find(InputArray image, ImageFeatures &features) CV_OVERRIDE;
  115. Ptr<FeatureDetector> detector_;
  116. Ptr<DescriptorExtractor> extractor_;
  117. Ptr<Feature2D> surf;
  118. };
  119. /** @brief SIFT features finder.
  120. @sa detail::FeaturesFinder, SIFT
  121. */
  122. class CV_EXPORTS SiftFeaturesFinder : public FeaturesFinder
  123. {
  124. public:
  125. SiftFeaturesFinder();
  126. private:
  127. void find(InputArray image, ImageFeatures &features) CV_OVERRIDE;
  128. Ptr<Feature2D> sift;
  129. };
  130. /** @brief ORB features finder. :
  131. @sa detail::FeaturesFinder, ORB
  132. */
  133. class CV_EXPORTS OrbFeaturesFinder : public FeaturesFinder
  134. {
  135. public:
  136. OrbFeaturesFinder(Size _grid_size = Size(3,1), int nfeatures=1500, float scaleFactor=1.3f, int nlevels=5);
  137. private:
  138. void find(InputArray image, ImageFeatures &features) CV_OVERRIDE;
  139. Ptr<ORB> orb;
  140. Size grid_size;
  141. };
  142. /** @brief AKAZE features finder. :
  143. @sa detail::FeaturesFinder, AKAZE
  144. */
  145. class CV_EXPORTS AKAZEFeaturesFinder : public detail::FeaturesFinder
  146. {
  147. public:
  148. AKAZEFeaturesFinder(int descriptor_type = AKAZE::DESCRIPTOR_MLDB,
  149. int descriptor_size = 0,
  150. int descriptor_channels = 3,
  151. float threshold = 0.001f,
  152. int nOctaves = 4,
  153. int nOctaveLayers = 4,
  154. int diffusivity = KAZE::DIFF_PM_G2);
  155. private:
  156. void find(InputArray image, ImageFeatures &features) CV_OVERRIDE;
  157. Ptr<AKAZE> akaze;
  158. };
  159. #ifdef HAVE_OPENCV_XFEATURES2D
  160. class CV_EXPORTS SurfFeaturesFinderGpu : public FeaturesFinder
  161. {
  162. public:
  163. SurfFeaturesFinderGpu(double hess_thresh = 300., int num_octaves = 3, int num_layers = 4,
  164. int num_octaves_descr = 4, int num_layers_descr = 2);
  165. void collectGarbage() CV_OVERRIDE;
  166. private:
  167. void find(InputArray image, ImageFeatures &features) CV_OVERRIDE;
  168. cuda::GpuMat image_;
  169. cuda::GpuMat gray_image_;
  170. cuda::SURF_CUDA surf_;
  171. cuda::GpuMat keypoints_;
  172. cuda::GpuMat descriptors_;
  173. int num_octaves_, num_layers_;
  174. int num_octaves_descr_, num_layers_descr_;
  175. };
  176. #endif
  177. /** @brief Structure containing information about matches between two images.
  178. It's assumed that there is a transformation between those images. Transformation may be
  179. homography or affine transformation based on selected matcher.
  180. @sa detail::FeaturesMatcher
  181. */
  182. struct CV_EXPORTS MatchesInfo
  183. {
  184. MatchesInfo();
  185. MatchesInfo(const MatchesInfo &other);
  186. MatchesInfo& operator =(const MatchesInfo &other);
  187. int src_img_idx, dst_img_idx; //!< Images indices (optional)
  188. std::vector<DMatch> matches;
  189. std::vector<uchar> inliers_mask; //!< Geometrically consistent matches mask
  190. int num_inliers; //!< Number of geometrically consistent matches
  191. Mat H; //!< Estimated transformation
  192. double confidence; //!< Confidence two images are from the same panorama
  193. };
  194. /** @brief Feature matchers base class. */
  195. class CV_EXPORTS FeaturesMatcher
  196. {
  197. public:
  198. virtual ~FeaturesMatcher() {}
  199. /** @overload
  200. @param features1 First image features
  201. @param features2 Second image features
  202. @param matches_info Found matches
  203. */
  204. void operator ()(const ImageFeatures &features1, const ImageFeatures &features2,
  205. MatchesInfo& matches_info) { match(features1, features2, matches_info); }
  206. /** @brief Performs images matching.
  207. @param features Features of the source images
  208. @param pairwise_matches Found pairwise matches
  209. @param mask Mask indicating which image pairs must be matched
  210. The function is parallelized with the TBB library.
  211. @sa detail::MatchesInfo
  212. */
  213. void operator ()(const std::vector<ImageFeatures> &features, std::vector<MatchesInfo> &pairwise_matches,
  214. const cv::UMat &mask = cv::UMat());
  215. /** @return True, if it's possible to use the same matcher instance in parallel, false otherwise
  216. */
  217. bool isThreadSafe() const { return is_thread_safe_; }
  218. /** @brief Frees unused memory allocated before if there is any.
  219. */
  220. virtual void collectGarbage() {}
  221. protected:
  222. FeaturesMatcher(bool is_thread_safe = false) : is_thread_safe_(is_thread_safe) {}
  223. /** @brief This method must implement matching logic in order to make the wrappers
  224. detail::FeaturesMatcher::operator()_ work.
  225. @param features1 first image features
  226. @param features2 second image features
  227. @param matches_info found matches
  228. */
  229. virtual void match(const ImageFeatures &features1, const ImageFeatures &features2,
  230. MatchesInfo& matches_info) = 0;
  231. bool is_thread_safe_;
  232. };
  233. /** @brief Features matcher which finds two best matches for each feature and leaves the best one only if the
  234. ratio between descriptor distances is greater than the threshold match_conf
  235. @sa detail::FeaturesMatcher
  236. */
  237. class CV_EXPORTS BestOf2NearestMatcher : public FeaturesMatcher
  238. {
  239. public:
  240. /** @brief Constructs a "best of 2 nearest" matcher.
  241. @param try_use_gpu Should try to use GPU or not
  242. @param match_conf Match distances ration threshold
  243. @param num_matches_thresh1 Minimum number of matches required for the 2D projective transform
  244. estimation used in the inliers classification step
  245. @param num_matches_thresh2 Minimum number of matches required for the 2D projective transform
  246. re-estimation on inliers
  247. */
  248. BestOf2NearestMatcher(bool try_use_gpu = false, float match_conf = 0.3f, int num_matches_thresh1 = 6,
  249. int num_matches_thresh2 = 6);
  250. void collectGarbage() CV_OVERRIDE;
  251. protected:
  252. void match(const ImageFeatures &features1, const ImageFeatures &features2, MatchesInfo &matches_info) CV_OVERRIDE;
  253. int num_matches_thresh1_;
  254. int num_matches_thresh2_;
  255. Ptr<FeaturesMatcher> impl_;
  256. };
  257. class CV_EXPORTS BestOf2NearestRangeMatcher : public BestOf2NearestMatcher
  258. {
  259. public:
  260. BestOf2NearestRangeMatcher(int range_width = 5, bool try_use_gpu = false, float match_conf = 0.3f,
  261. int num_matches_thresh1 = 6, int num_matches_thresh2 = 6);
  262. void operator ()(const std::vector<ImageFeatures> &features, std::vector<MatchesInfo> &pairwise_matches,
  263. const cv::UMat &mask = cv::UMat());
  264. protected:
  265. int range_width_;
  266. };
  267. /** @brief Features matcher similar to cv::detail::BestOf2NearestMatcher which
  268. finds two best matches for each feature and leaves the best one only if the
  269. ratio between descriptor distances is greater than the threshold match_conf.
  270. Unlike cv::detail::BestOf2NearestMatcher this matcher uses affine
  271. transformation (affine transformation estimate will be placed in matches_info).
  272. @sa cv::detail::FeaturesMatcher cv::detail::BestOf2NearestMatcher
  273. */
  274. class CV_EXPORTS AffineBestOf2NearestMatcher : public BestOf2NearestMatcher
  275. {
  276. public:
  277. /** @brief Constructs a "best of 2 nearest" matcher that expects affine transformation
  278. between images
  279. @param full_affine whether to use full affine transformation with 6 degress of freedom or reduced
  280. transformation with 4 degrees of freedom using only rotation, translation and uniform scaling
  281. @param try_use_gpu Should try to use GPU or not
  282. @param match_conf Match distances ration threshold
  283. @param num_matches_thresh1 Minimum number of matches required for the 2D affine transform
  284. estimation used in the inliers classification step
  285. @sa cv::estimateAffine2D cv::estimateAffinePartial2D
  286. */
  287. AffineBestOf2NearestMatcher(bool full_affine = false, bool try_use_gpu = false,
  288. float match_conf = 0.3f, int num_matches_thresh1 = 6) :
  289. BestOf2NearestMatcher(try_use_gpu, match_conf, num_matches_thresh1, num_matches_thresh1),
  290. full_affine_(full_affine) {}
  291. protected:
  292. void match(const ImageFeatures &features1, const ImageFeatures &features2, MatchesInfo &matches_info) CV_OVERRIDE;
  293. bool full_affine_;
  294. };
  295. //! @} stitching_match
  296. } // namespace detail
  297. } // namespace cv
  298. #endif // OPENCV_STITCHING_MATCHERS_HPP