matchers.hpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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());
  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. bool is_thread_safe_;
  140. };
  141. /** @brief Features matcher which finds two best matches for each feature and leaves the best one only if the
  142. ratio between descriptor distances is greater than the threshold match_conf
  143. @sa detail::FeaturesMatcher
  144. */
  145. class CV_EXPORTS_W BestOf2NearestMatcher : public FeaturesMatcher
  146. {
  147. public:
  148. /** @brief Constructs a "best of 2 nearest" matcher.
  149. @param try_use_gpu Should try to use GPU or not
  150. @param match_conf Match distances ration threshold
  151. @param num_matches_thresh1 Minimum number of matches required for the 2D projective transform
  152. estimation used in the inliers classification step
  153. @param num_matches_thresh2 Minimum number of matches required for the 2D projective transform
  154. re-estimation on inliers
  155. */
  156. CV_WRAP BestOf2NearestMatcher(bool try_use_gpu = false, float match_conf = 0.3f, int num_matches_thresh1 = 6,
  157. int num_matches_thresh2 = 6);
  158. CV_WRAP void collectGarbage() CV_OVERRIDE;
  159. CV_WRAP static Ptr<BestOf2NearestMatcher> create(bool try_use_gpu = false, float match_conf = 0.3f, int num_matches_thresh1 = 6,
  160. int num_matches_thresh2 = 6);
  161. protected:
  162. void match(const ImageFeatures &features1, const ImageFeatures &features2, MatchesInfo &matches_info) CV_OVERRIDE;
  163. int num_matches_thresh1_;
  164. int num_matches_thresh2_;
  165. Ptr<FeaturesMatcher> impl_;
  166. };
  167. class CV_EXPORTS_W BestOf2NearestRangeMatcher : public BestOf2NearestMatcher
  168. {
  169. public:
  170. CV_WRAP BestOf2NearestRangeMatcher(int range_width = 5, bool try_use_gpu = false, float match_conf = 0.3f,
  171. int num_matches_thresh1 = 6, int num_matches_thresh2 = 6);
  172. void operator ()(const std::vector<ImageFeatures> &features, std::vector<MatchesInfo> &pairwise_matches,
  173. const cv::UMat &mask = cv::UMat());
  174. protected:
  175. int range_width_;
  176. };
  177. /** @brief Features matcher similar to cv::detail::BestOf2NearestMatcher which
  178. finds two best matches for each feature and leaves the best one only if the
  179. ratio between descriptor distances is greater than the threshold match_conf.
  180. Unlike cv::detail::BestOf2NearestMatcher this matcher uses affine
  181. transformation (affine transformation estimate will be placed in matches_info).
  182. @sa cv::detail::FeaturesMatcher cv::detail::BestOf2NearestMatcher
  183. */
  184. class CV_EXPORTS_W AffineBestOf2NearestMatcher : public BestOf2NearestMatcher
  185. {
  186. public:
  187. /** @brief Constructs a "best of 2 nearest" matcher that expects affine transformation
  188. between images
  189. @param full_affine whether to use full affine transformation with 6 degress of freedom or reduced
  190. transformation with 4 degrees of freedom using only rotation, translation and uniform scaling
  191. @param try_use_gpu Should try to use GPU or not
  192. @param match_conf Match distances ration threshold
  193. @param num_matches_thresh1 Minimum number of matches required for the 2D affine transform
  194. estimation used in the inliers classification step
  195. @sa cv::estimateAffine2D cv::estimateAffinePartial2D
  196. */
  197. CV_WRAP AffineBestOf2NearestMatcher(bool full_affine = false, bool try_use_gpu = false,
  198. float match_conf = 0.3f, int num_matches_thresh1 = 6) :
  199. BestOf2NearestMatcher(try_use_gpu, match_conf, num_matches_thresh1, num_matches_thresh1),
  200. full_affine_(full_affine) {}
  201. protected:
  202. void match(const ImageFeatures &features1, const ImageFeatures &features2, MatchesInfo &matches_info) CV_OVERRIDE;
  203. bool full_affine_;
  204. };
  205. //! @} stitching_match
  206. } // namespace detail
  207. } // namespace cv
  208. #endif // OPENCV_STITCHING_MATCHERS_HPP