matchers.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. namespace cv {
  48. namespace detail {
  49. //! @addtogroup stitching_match
  50. //! @{
  51. /** @brief Structure containing image keypoints and descriptors. */
  52. struct CV_EXPORTS_W_SIMPLE ImageFeatures
  53. {
  54. CV_PROP_RW int img_idx;
  55. CV_PROP_RW Size img_size;
  56. CV_PROP_RW std::vector<KeyPoint> keypoints;
  57. CV_PROP_RW UMat descriptors;
  58. CV_WRAP std::vector<KeyPoint> getKeypoints() { return keypoints; }
  59. };
  60. /** @brief
  61. @param featuresFinder
  62. @param images
  63. @param features
  64. @param masks
  65. */
  66. CV_EXPORTS_W void computeImageFeatures(
  67. const Ptr<Feature2D> &featuresFinder,
  68. InputArrayOfArrays images,
  69. CV_OUT std::vector<ImageFeatures> &features,
  70. InputArrayOfArrays masks = noArray());
  71. /** @brief
  72. @param featuresFinder
  73. @param image
  74. @param features
  75. @param mask
  76. */
  77. CV_EXPORTS_AS(computeImageFeatures2) void computeImageFeatures(
  78. const Ptr<Feature2D> &featuresFinder,
  79. InputArray image,
  80. CV_OUT ImageFeatures &features,
  81. InputArray mask = noArray());
  82. /** @brief Structure containing information about matches between two images.
  83. It's assumed that there is a transformation between those images. Transformation may be
  84. homography or affine transformation based on selected matcher.
  85. @sa detail::FeaturesMatcher
  86. */
  87. struct CV_EXPORTS_W_SIMPLE MatchesInfo
  88. {
  89. MatchesInfo();
  90. MatchesInfo(const MatchesInfo &other);
  91. MatchesInfo& operator =(const MatchesInfo &other);
  92. CV_PROP_RW int src_img_idx;
  93. CV_PROP_RW int dst_img_idx; //!< Images indices (optional)
  94. CV_PROP_RW std::vector<DMatch> matches;
  95. CV_PROP_RW std::vector<uchar> inliers_mask; //!< Geometrically consistent matches mask
  96. CV_PROP_RW int num_inliers; //!< Number of geometrically consistent matches
  97. CV_PROP_RW Mat H; //!< Estimated transformation
  98. CV_PROP_RW double confidence; //!< Confidence two images are from the same panorama
  99. CV_WRAP std::vector<DMatch> getMatches() { return matches; }
  100. CV_WRAP std::vector<uchar> getInliers() { return inliers_mask; }
  101. };
  102. /** @brief Feature matchers base class. */
  103. class CV_EXPORTS_W FeaturesMatcher
  104. {
  105. public:
  106. CV_WRAP virtual ~FeaturesMatcher() {}
  107. /** @overload
  108. @param features1 First image features
  109. @param features2 Second image features
  110. @param matches_info Found matches
  111. */
  112. CV_WRAP_AS(apply) void operator ()(const ImageFeatures &features1, const ImageFeatures &features2,
  113. CV_OUT MatchesInfo& matches_info) { match(features1, features2, matches_info); }
  114. /** @brief Performs images matching.
  115. @param features Features of the source images
  116. @param pairwise_matches Found pairwise matches
  117. @param mask Mask indicating which image pairs must be matched
  118. The function is parallelized with the TBB library.
  119. @sa detail::MatchesInfo
  120. */
  121. CV_WRAP_AS(apply2) void operator ()(const std::vector<ImageFeatures> &features, CV_OUT std::vector<MatchesInfo> &pairwise_matches,
  122. const cv::UMat &mask = cv::UMat()) { match(features, pairwise_matches, mask); }
  123. /** @return True, if it's possible to use the same matcher instance in parallel, false otherwise
  124. */
  125. CV_WRAP bool isThreadSafe() const { return is_thread_safe_; }
  126. /** @brief Frees unused memory allocated before if there is any.
  127. */
  128. CV_WRAP virtual void collectGarbage() {}
  129. protected:
  130. FeaturesMatcher(bool is_thread_safe = false) : is_thread_safe_(is_thread_safe) {}
  131. /** @brief This method must implement matching logic in order to make the wrappers
  132. detail::FeaturesMatcher::operator()_ work.
  133. @param features1 first image features
  134. @param features2 second image features
  135. @param matches_info found matches
  136. */
  137. virtual void match(const ImageFeatures &features1, const ImageFeatures &features2,
  138. MatchesInfo& matches_info) = 0;
  139. /** @brief This method implements logic to match features between arbitrary number of features.
  140. By default this checks every pair of inputs in the input, but the behaviour can be changed by subclasses.
  141. @param features vector of image features
  142. @param pairwise_matches found matches
  143. @param mask (optional) mask indicating which image pairs should be matched
  144. */
  145. virtual void match(const std::vector<ImageFeatures> &features, std::vector<MatchesInfo> &pairwise_matches,
  146. const cv::UMat &mask = cv::UMat());
  147. bool is_thread_safe_;
  148. };
  149. /** @brief Features matcher which finds two best matches for each feature and leaves the best one only if the
  150. ratio between descriptor distances is greater than the threshold match_conf
  151. @sa detail::FeaturesMatcher
  152. */
  153. class CV_EXPORTS_W BestOf2NearestMatcher : public FeaturesMatcher
  154. {
  155. public:
  156. /** @brief Constructs a "best of 2 nearest" matcher.
  157. @param try_use_gpu Should try to use GPU or not
  158. @param match_conf Match distances ration threshold
  159. @param num_matches_thresh1 Minimum number of matches required for the 2D projective transform
  160. estimation used in the inliers classification step
  161. @param num_matches_thresh2 Minimum number of matches required for the 2D projective transform
  162. re-estimation on inliers
  163. @param matches_confindece_thresh Matching confidence threshold to take the match into account.
  164. The threshold was determined experimentally and set to 3 by default.
  165. */
  166. CV_WRAP BestOf2NearestMatcher(bool try_use_gpu = false, float match_conf = 0.3f, int num_matches_thresh1 = 6,
  167. int num_matches_thresh2 = 6, double matches_confindece_thresh = 3.);
  168. CV_WRAP void collectGarbage() CV_OVERRIDE;
  169. CV_WRAP static Ptr<BestOf2NearestMatcher> create(bool try_use_gpu = false, float match_conf = 0.3f, int num_matches_thresh1 = 6,
  170. int num_matches_thresh2 = 6, double matches_confindece_thresh = 3.);
  171. protected:
  172. void match(const ImageFeatures &features1, const ImageFeatures &features2, MatchesInfo &matches_info) CV_OVERRIDE;
  173. int num_matches_thresh1_;
  174. int num_matches_thresh2_;
  175. double matches_confindece_thresh_;
  176. Ptr<FeaturesMatcher> impl_;
  177. };
  178. class CV_EXPORTS_W BestOf2NearestRangeMatcher : public BestOf2NearestMatcher
  179. {
  180. public:
  181. CV_WRAP BestOf2NearestRangeMatcher(int range_width = 5, bool try_use_gpu = false, float match_conf = 0.3f,
  182. int num_matches_thresh1 = 6, int num_matches_thresh2 = 6);
  183. protected:
  184. // indicate that we do not want to hide the base class match method with a different signature
  185. using BestOf2NearestMatcher::match;
  186. void match(const std::vector<ImageFeatures> &features, std::vector<MatchesInfo> &pairwise_matches,
  187. const cv::UMat &mask = cv::UMat()) CV_OVERRIDE;
  188. int range_width_;
  189. };
  190. /** @brief Features matcher similar to cv::detail::BestOf2NearestMatcher which
  191. finds two best matches for each feature and leaves the best one only if the
  192. ratio between descriptor distances is greater than the threshold match_conf.
  193. Unlike cv::detail::BestOf2NearestMatcher this matcher uses affine
  194. transformation (affine transformation estimate will be placed in matches_info).
  195. @sa cv::detail::FeaturesMatcher cv::detail::BestOf2NearestMatcher
  196. */
  197. class CV_EXPORTS_W AffineBestOf2NearestMatcher : public BestOf2NearestMatcher
  198. {
  199. public:
  200. /** @brief Constructs a "best of 2 nearest" matcher that expects affine transformation
  201. between images
  202. @param full_affine whether to use full affine transformation with 6 degress of freedom or reduced
  203. transformation with 4 degrees of freedom using only rotation, translation and uniform scaling
  204. @param try_use_gpu Should try to use GPU or not
  205. @param match_conf Match distances ration threshold
  206. @param num_matches_thresh1 Minimum number of matches required for the 2D affine transform
  207. estimation used in the inliers classification step
  208. @sa cv::estimateAffine2D cv::estimateAffinePartial2D
  209. */
  210. CV_WRAP AffineBestOf2NearestMatcher(bool full_affine = false, bool try_use_gpu = false,
  211. float match_conf = 0.3f, int num_matches_thresh1 = 6) :
  212. BestOf2NearestMatcher(try_use_gpu, match_conf, num_matches_thresh1, num_matches_thresh1),
  213. full_affine_(full_affine) {}
  214. protected:
  215. void match(const ImageFeatures &features1, const ImageFeatures &features2, MatchesInfo &matches_info) CV_OVERRIDE;
  216. bool full_affine_;
  217. };
  218. //! @} stitching_match
  219. } // namespace detail
  220. } // namespace cv
  221. #endif // OPENCV_STITCHING_MATCHERS_HPP