github.com/kaydxh/golang@v0.0.131/pkg/gocv/cgo/third_path/opencv4/include/opencv2/stitching/detail/matchers.hpp (about)

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