matchers.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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);
  115. Ptr<FeatureDetector> detector_;
  116. Ptr<DescriptorExtractor> extractor_;
  117. Ptr<Feature2D> surf;
  118. };
  119. /** @brief ORB features finder. :
  120. @sa detail::FeaturesFinder, ORB
  121. */
  122. class CV_EXPORTS OrbFeaturesFinder : public FeaturesFinder
  123. {
  124. public:
  125. OrbFeaturesFinder(Size _grid_size = Size(3,1), int nfeatures=1500, float scaleFactor=1.3f, int nlevels=5);
  126. private:
  127. void find(InputArray image, ImageFeatures &features);
  128. Ptr<ORB> orb;
  129. Size grid_size;
  130. };
  131. /** @brief AKAZE features finder. :
  132. @sa detail::FeaturesFinder, AKAZE
  133. */
  134. class CV_EXPORTS AKAZEFeaturesFinder : public detail::FeaturesFinder
  135. {
  136. public:
  137. AKAZEFeaturesFinder(int descriptor_type = AKAZE::DESCRIPTOR_MLDB,
  138. int descriptor_size = 0,
  139. int descriptor_channels = 3,
  140. float threshold = 0.001f,
  141. int nOctaves = 4,
  142. int nOctaveLayers = 4,
  143. int diffusivity = KAZE::DIFF_PM_G2);
  144. private:
  145. void find(InputArray image, detail::ImageFeatures &features);
  146. Ptr<AKAZE> akaze;
  147. };
  148. #ifdef HAVE_OPENCV_XFEATURES2D
  149. class CV_EXPORTS SurfFeaturesFinderGpu : public FeaturesFinder
  150. {
  151. public:
  152. SurfFeaturesFinderGpu(double hess_thresh = 300., int num_octaves = 3, int num_layers = 4,
  153. int num_octaves_descr = 4, int num_layers_descr = 2);
  154. void collectGarbage();
  155. private:
  156. void find(InputArray image, ImageFeatures &features);
  157. cuda::GpuMat image_;
  158. cuda::GpuMat gray_image_;
  159. cuda::SURF_CUDA surf_;
  160. cuda::GpuMat keypoints_;
  161. cuda::GpuMat descriptors_;
  162. int num_octaves_, num_layers_;
  163. int num_octaves_descr_, num_layers_descr_;
  164. };
  165. #endif
  166. /** @brief Structure containing information about matches between two images.
  167. It's assumed that there is a transformation between those images. Transformation may be
  168. homography or affine transformation based on selected matcher.
  169. @sa detail::FeaturesMatcher
  170. */
  171. struct CV_EXPORTS MatchesInfo
  172. {
  173. MatchesInfo();
  174. MatchesInfo(const MatchesInfo &other);
  175. MatchesInfo& operator =(const MatchesInfo &other);
  176. int src_img_idx, dst_img_idx; //!< Images indices (optional)
  177. std::vector<DMatch> matches;
  178. std::vector<uchar> inliers_mask; //!< Geometrically consistent matches mask
  179. int num_inliers; //!< Number of geometrically consistent matches
  180. Mat H; //!< Estimated transformation
  181. double confidence; //!< Confidence two images are from the same panorama
  182. };
  183. /** @brief Feature matchers base class. */
  184. class CV_EXPORTS FeaturesMatcher
  185. {
  186. public:
  187. virtual ~FeaturesMatcher() {}
  188. /** @overload
  189. @param features1 First image features
  190. @param features2 Second image features
  191. @param matches_info Found matches
  192. */
  193. void operator ()(const ImageFeatures &features1, const ImageFeatures &features2,
  194. MatchesInfo& matches_info) { match(features1, features2, matches_info); }
  195. /** @brief Performs images matching.
  196. @param features Features of the source images
  197. @param pairwise_matches Found pairwise matches
  198. @param mask Mask indicating which image pairs must be matched
  199. The function is parallelized with the TBB library.
  200. @sa detail::MatchesInfo
  201. */
  202. void operator ()(const std::vector<ImageFeatures> &features, std::vector<MatchesInfo> &pairwise_matches,
  203. const cv::UMat &mask = cv::UMat());
  204. /** @return True, if it's possible to use the same matcher instance in parallel, false otherwise
  205. */
  206. bool isThreadSafe() const { return is_thread_safe_; }
  207. /** @brief Frees unused memory allocated before if there is any.
  208. */
  209. virtual void collectGarbage() {}
  210. protected:
  211. FeaturesMatcher(bool is_thread_safe = false) : is_thread_safe_(is_thread_safe) {}
  212. /** @brief This method must implement matching logic in order to make the wrappers
  213. detail::FeaturesMatcher::operator()_ work.
  214. @param features1 first image features
  215. @param features2 second image features
  216. @param matches_info found matches
  217. */
  218. virtual void match(const ImageFeatures &features1, const ImageFeatures &features2,
  219. MatchesInfo& matches_info) = 0;
  220. bool is_thread_safe_;
  221. };
  222. /** @brief Features matcher which finds two best matches for each feature and leaves the best one only if the
  223. ratio between descriptor distances is greater than the threshold match_conf
  224. @sa detail::FeaturesMatcher
  225. */
  226. class CV_EXPORTS BestOf2NearestMatcher : public FeaturesMatcher
  227. {
  228. public:
  229. /** @brief Constructs a "best of 2 nearest" matcher.
  230. @param try_use_gpu Should try to use GPU or not
  231. @param match_conf Match distances ration threshold
  232. @param num_matches_thresh1 Minimum number of matches required for the 2D projective transform
  233. estimation used in the inliers classification step
  234. @param num_matches_thresh2 Minimum number of matches required for the 2D projective transform
  235. re-estimation on inliers
  236. */
  237. BestOf2NearestMatcher(bool try_use_gpu = false, float match_conf = 0.3f, int num_matches_thresh1 = 6,
  238. int num_matches_thresh2 = 6);
  239. void collectGarbage();
  240. protected:
  241. void match(const ImageFeatures &features1, const ImageFeatures &features2, MatchesInfo &matches_info);
  242. int num_matches_thresh1_;
  243. int num_matches_thresh2_;
  244. Ptr<FeaturesMatcher> impl_;
  245. };
  246. class CV_EXPORTS BestOf2NearestRangeMatcher : public BestOf2NearestMatcher
  247. {
  248. public:
  249. BestOf2NearestRangeMatcher(int range_width = 5, bool try_use_gpu = false, float match_conf = 0.3f,
  250. int num_matches_thresh1 = 6, int num_matches_thresh2 = 6);
  251. void operator ()(const std::vector<ImageFeatures> &features, std::vector<MatchesInfo> &pairwise_matches,
  252. const cv::UMat &mask = cv::UMat());
  253. protected:
  254. int range_width_;
  255. };
  256. /** @brief Features matcher similar to cv::detail::BestOf2NearestMatcher which
  257. finds two best matches for each feature and leaves the best one only if the
  258. ratio between descriptor distances is greater than the threshold match_conf.
  259. Unlike cv::detail::BestOf2NearestMatcher this matcher uses affine
  260. transformation (affine trasformation estimate will be placed in matches_info).
  261. @sa cv::detail::FeaturesMatcher cv::detail::BestOf2NearestMatcher
  262. */
  263. class CV_EXPORTS AffineBestOf2NearestMatcher : public BestOf2NearestMatcher
  264. {
  265. public:
  266. /** @brief Constructs a "best of 2 nearest" matcher that expects affine trasformation
  267. between images
  268. @param full_affine whether to use full affine transformation with 6 degress of freedom or reduced
  269. transformation with 4 degrees of freedom using only rotation, translation and uniform scaling
  270. @param try_use_gpu Should try to use GPU or not
  271. @param match_conf Match distances ration threshold
  272. @param num_matches_thresh1 Minimum number of matches required for the 2D affine transform
  273. estimation used in the inliers classification step
  274. @sa cv::estimateAffine2D cv::estimateAffinePartial2D
  275. */
  276. AffineBestOf2NearestMatcher(bool full_affine = false, bool try_use_gpu = false,
  277. float match_conf = 0.3f, int num_matches_thresh1 = 6) :
  278. BestOf2NearestMatcher(try_use_gpu, match_conf, num_matches_thresh1, num_matches_thresh1),
  279. full_affine_(full_affine) {}
  280. protected:
  281. void match(const ImageFeatures &features1, const ImageFeatures &features2, MatchesInfo &matches_info);
  282. bool full_affine_;
  283. };
  284. //! @} stitching_match
  285. } // namespace detail
  286. } // namespace cv
  287. #endif // OPENCV_STITCHING_MATCHERS_HPP