include/opencv2/stitching/detail/matchers.hpp
Go to the documentation of this file.
00001 /*M///////////////////////////////////////////////////////////////////////////////////////
00002 //
00003 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
00004 //
00005 //  By downloading, copying, installing or using the software you agree to this license.
00006 //  If you do not agree to this license, do not download, install,
00007 //  copy or use the software.
00008 //
00009 //
00010 //                          License Agreement
00011 //                For Open Source Computer Vision Library
00012 //
00013 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
00014 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
00015 // Third party copyrights are property of their respective owners.
00016 //
00017 // Redistribution and use in source and binary forms, with or without modification,
00018 // are permitted provided that the following conditions are met:
00019 //
00020 //   * Redistribution's of source code must retain the above copyright notice,
00021 //     this list of conditions and the following disclaimer.
00022 //
00023 //   * Redistribution's in binary form must reproduce the above copyright notice,
00024 //     this list of conditions and the following disclaimer in the documentation
00025 //     and/or other materials provided with the distribution.
00026 //
00027 //   * The name of the copyright holders may not be used to endorse or promote products
00028 //     derived from this software without specific prior written permission.
00029 //
00030 // This software is provided by the copyright holders and contributors "as is" and
00031 // any express or implied warranties, including, but not limited to, the implied
00032 // warranties of merchantability and fitness for a particular purpose are disclaimed.
00033 // In no event shall the Intel Corporation or contributors be liable for any direct,
00034 // indirect, incidental, special, exemplary, or consequential damages
00035 // (including, but not limited to, procurement of substitute goods or services;
00036 // loss of use, data, or profits; or business interruption) however caused
00037 // and on any theory of liability, whether in contract, strict liability,
00038 // or tort (including negligence or otherwise) arising in any way out of
00039 // the use of this software, even if advised of the possibility of such damage.
00040 //
00041 //M*/
00042 
00043 #ifndef __OPENCV_STITCHING_MATCHERS_HPP__
00044 #define __OPENCV_STITCHING_MATCHERS_HPP__
00045 
00046 #include "opencv2/core/core.hpp"
00047 #include "opencv2/features2d/features2d.hpp"
00048 
00049 #include "opencv2/opencv_modules.hpp"
00050 #ifdef HAVE_OPENCV_GPU
00051 #include "opencv2/gpu/gpu.hpp"
00052 #endif
00053 
00054 namespace cv {
00055 namespace detail {
00056 
00057 struct CV_EXPORTS ImageFeatures
00058 {
00059     int img_idx;
00060     Size img_size;
00061     std::vector<KeyPoint> keypoints;
00062     Mat descriptors;
00063 };
00064 
00065 
00066 class CV_EXPORTS FeaturesFinder
00067 {
00068 public:
00069     virtual ~FeaturesFinder() {}
00070     void operator ()(const Mat &image, ImageFeatures &features);
00071     void operator ()(const Mat &image, ImageFeatures &features, const std::vector<cv::Rect> &rois);
00072     virtual void collectGarbage() {}
00073 
00074 protected:
00075     virtual void find(const Mat &image, ImageFeatures &features) = 0;
00076 };
00077 
00078 
00079 class CV_EXPORTS SurfFeaturesFinder : public FeaturesFinder
00080 {
00081 public:
00082     SurfFeaturesFinder(double hess_thresh = 300., int num_octaves = 3, int num_layers = 4,
00083                        int num_octaves_descr = /*4*/3, int num_layers_descr = /*2*/4);
00084 
00085 private:
00086     void find(const Mat &image, ImageFeatures &features);
00087 
00088     Ptr<FeatureDetector> detector_;
00089     Ptr<DescriptorExtractor> extractor_;
00090     Ptr<Feature2D> surf;
00091 };
00092 
00093 class CV_EXPORTS OrbFeaturesFinder : public FeaturesFinder
00094 {
00095 public:
00096     OrbFeaturesFinder(Size _grid_size = Size(3,1), int nfeatures=1500, float scaleFactor=1.3f, int nlevels=5);
00097 
00098 private:
00099     void find(const Mat &image, ImageFeatures &features);
00100 
00101     Ptr<ORB> orb;
00102     Size grid_size;
00103 };
00104 
00105 
00106 #ifdef HAVE_OPENCV_GPU
00107 class CV_EXPORTS SurfFeaturesFinderGpu : public FeaturesFinder
00108 {
00109 public:
00110     SurfFeaturesFinderGpu(double hess_thresh = 300., int num_octaves = 3, int num_layers = 4,
00111                           int num_octaves_descr = 4, int num_layers_descr = 2);
00112 
00113     void collectGarbage();
00114 
00115 private:
00116     void find(const Mat &image, ImageFeatures &features);
00117 
00118     gpu::GpuMat image_;
00119     gpu::GpuMat gray_image_;
00120     gpu::SURF_GPU surf_;
00121     gpu::GpuMat keypoints_;
00122     gpu::GpuMat descriptors_;
00123     int num_octaves_, num_layers_;
00124     int num_octaves_descr_, num_layers_descr_;
00125 };
00126 #endif
00127 
00128 
00129 struct CV_EXPORTS MatchesInfo
00130 {
00131     MatchesInfo();
00132     MatchesInfo(const MatchesInfo &other);
00133     const MatchesInfo& operator =(const MatchesInfo &other);
00134 
00135     int src_img_idx, dst_img_idx;       // Images indices (optional)
00136     std::vector<DMatch> matches;
00137     std::vector<uchar> inliers_mask;    // Geometrically consistent matches mask
00138     int num_inliers;                    // Number of geometrically consistent matches
00139     Mat H;                              // Estimated homography
00140     double confidence;                  // Confidence two images are from the same panorama
00141 };
00142 
00143 
00144 class CV_EXPORTS FeaturesMatcher
00145 {
00146 public:
00147     virtual ~FeaturesMatcher() {}
00148 
00149     void operator ()(const ImageFeatures &features1, const ImageFeatures &features2,
00150                      MatchesInfo& matches_info) { match(features1, features2, matches_info); }
00151 
00152     void operator ()(const std::vector<ImageFeatures> &features, std::vector<MatchesInfo> &pairwise_matches,
00153                      const cv::Mat &mask = cv::Mat());
00154 
00155     bool isThreadSafe() const { return is_thread_safe_; }
00156 
00157     virtual void collectGarbage() {}
00158 
00159 protected:
00160     FeaturesMatcher(bool is_thread_safe = false) : is_thread_safe_(is_thread_safe) {}
00161 
00162     virtual void match(const ImageFeatures &features1, const ImageFeatures &features2,
00163                        MatchesInfo& matches_info) = 0;
00164 
00165     bool is_thread_safe_;
00166 };
00167 
00168 
00169 class CV_EXPORTS BestOf2NearestMatcher : public FeaturesMatcher
00170 {
00171 public:
00172     BestOf2NearestMatcher(bool try_use_gpu = false, float match_conf = 0.3f, int num_matches_thresh1 = 6,
00173                           int num_matches_thresh2 = 6);
00174 
00175     void collectGarbage();
00176 
00177 protected:
00178     void match(const ImageFeatures &features1, const ImageFeatures &features2, MatchesInfo &matches_info);
00179 
00180     int num_matches_thresh1_;
00181     int num_matches_thresh2_;
00182     Ptr<FeaturesMatcher> impl_;
00183 };
00184 
00185 } // namespace detail
00186 } // namespace cv
00187 
00188 #endif // __OPENCV_STITCHING_MATCHERS_HPP__