matchers.hpp
Go to the documentation of this file.
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/core.hpp"
47 #include "opencv2/core/gpumat.hpp"
49 
51 
52 #if defined(HAVE_OPENCV_NONFREE)
53  #include "opencv2/nonfree/gpu.hpp"
54 #endif
55 
56 namespace cv {
57 namespace detail {
58 
59 struct CV_EXPORTS ImageFeatures
60 {
61  int img_idx;
63  std::vector<KeyPoint> keypoints;
65 };
66 
67 
68 class CV_EXPORTS FeaturesFinder
69 {
70 public:
71  virtual ~FeaturesFinder() {}
72  void operator ()(const Mat &image, ImageFeatures &features);
73  void operator ()(const Mat &image, ImageFeatures &features, const std::vector<cv::Rect> &rois);
74  virtual void collectGarbage() {}
75 
76 protected:
77  virtual void find(const Mat &image, ImageFeatures &features) = 0;
78 };
79 
80 
81 class CV_EXPORTS SurfFeaturesFinder : public FeaturesFinder
82 {
83 public:
84  SurfFeaturesFinder(double hess_thresh = 300., int num_octaves = 3, int num_layers = 4,
85  int num_octaves_descr = /*4*/3, int num_layers_descr = /*2*/4);
86 
87 private:
88  void find(const Mat &image, ImageFeatures &features);
89 
90  Ptr<FeatureDetector> detector_;
91  Ptr<DescriptorExtractor> extractor_;
92  Ptr<Feature2D> surf;
93 };
94 
95 class CV_EXPORTS OrbFeaturesFinder : public FeaturesFinder
96 {
97 public:
98  OrbFeaturesFinder(Size _grid_size = Size(3,1), int nfeatures=1500, float scaleFactor=1.3f, int nlevels=5);
99 
100 private:
101  void find(const Mat &image, ImageFeatures &features);
102 
103  Ptr<ORB> orb;
104  Size grid_size;
105 };
106 
107 
108 #if defined(HAVE_OPENCV_NONFREE)
109 class CV_EXPORTS SurfFeaturesFinderGpu : public FeaturesFinder
110 {
111 public:
112  SurfFeaturesFinderGpu(double hess_thresh = 300., int num_octaves = 3, int num_layers = 4,
113  int num_octaves_descr = 4, int num_layers_descr = 2);
114 
115  void collectGarbage();
116 
117 private:
118  void find(const Mat &image, ImageFeatures &features);
119 
120  gpu::GpuMat image_;
121  gpu::GpuMat gray_image_;
122  gpu::SURF_GPU surf_;
123  gpu::GpuMat keypoints_;
124  gpu::GpuMat descriptors_;
125  int num_octaves_, num_layers_;
126  int num_octaves_descr_, num_layers_descr_;
127 };
128 #endif
129 
130 
131 struct CV_EXPORTS MatchesInfo
132 {
133  MatchesInfo();
134  MatchesInfo(const MatchesInfo &other);
135  const MatchesInfo& operator =(const MatchesInfo &other);
136 
137  int src_img_idx, dst_img_idx; // Images indices (optional)
138  std::vector<DMatch> matches;
139  std::vector<uchar> inliers_mask; // Geometrically consistent matches mask
140  int num_inliers; // Number of geometrically consistent matches
141  Mat H; // Estimated homography
142  double confidence; // Confidence two images are from the same panorama
143 };
144 
145 
146 class CV_EXPORTS FeaturesMatcher
147 {
148 public:
149  virtual ~FeaturesMatcher() {}
150 
151  void operator ()(const ImageFeatures &features1, const ImageFeatures &features2,
152  MatchesInfo& matches_info) { match(features1, features2, matches_info); }
153 
154  void operator ()(const std::vector<ImageFeatures> &features, std::vector<MatchesInfo> &pairwise_matches,
155  const cv::Mat &mask = cv::Mat());
156 
157  bool isThreadSafe() const { return is_thread_safe_; }
158 
159  virtual void collectGarbage() {}
160 
161 protected:
162  FeaturesMatcher(bool is_thread_safe = false) : is_thread_safe_(is_thread_safe) {}
163 
164  virtual void match(const ImageFeatures &features1, const ImageFeatures &features2,
165  MatchesInfo& matches_info) = 0;
166 
168 };
169 
170 
171 class CV_EXPORTS BestOf2NearestMatcher : public FeaturesMatcher
172 {
173 public:
174  BestOf2NearestMatcher(bool try_use_gpu = false, float match_conf = 0.3f, int num_matches_thresh1 = 6,
175  int num_matches_thresh2 = 6);
176 
177  void collectGarbage();
178 
179 protected:
180  void match(const ImageFeatures &features1, const ImageFeatures &features2, MatchesInfo &matches_info);
181 
185 };
186 
187 } // namespace detail
188 } // namespace cv
189 
190 #endif // __OPENCV_STITCHING_MATCHERS_HPP__
bool isThreadSafe() const
Definition: matchers.hpp:157
Mat H
Definition: matchers.hpp:141
Definition: matchers.hpp:171
Size2i Size
Definition: core.hpp:896
virtual void collectGarbage()
Definition: matchers.hpp:159
Size img_size
Definition: matchers.hpp:62
Ptr< FeaturesMatcher > impl_
Definition: matchers.hpp:184
int num_inliers
Definition: matchers.hpp:140
Definition: matchers.hpp:59
Definition: gpu.hpp:50
int src_img_idx
Definition: matchers.hpp:137
FeaturesMatcher(bool is_thread_safe=false)
Definition: matchers.hpp:162
GLenum GLsizei GLenum GLenum const GLvoid * image
Definition: highgui_c.h:230
Mat descriptors
Definition: matchers.hpp:64
virtual ~FeaturesMatcher()
Definition: matchers.hpp:149
The 2D size class.
Definition: core.hpp:81
std::vector< KeyPoint > keypoints
Definition: matchers.hpp:63
Definition: matchers.hpp:131
Definition: matchers.hpp:146
std::vector< DMatch > matches
Definition: matchers.hpp:138
Definition: matchers.hpp:81
virtual void collectGarbage()
Definition: matchers.hpp:74
Smart pointer for GPU memory with reference counting. Its interface is mostly similar with cv::Mat...
Definition: gpumat.hpp:154
virtual ~FeaturesFinder()
Definition: matchers.hpp:71
The Core Functionality.
The n-dimensional matrix class.
Definition: core.hpp:1688
Definition: matchers.hpp:95
double confidence
Definition: matchers.hpp:142
int num_matches_thresh1_
Definition: matchers.hpp:182
int img_idx
Definition: matchers.hpp:61
std::vector< uchar > inliers_mask
Definition: matchers.hpp:139
Smart pointer to dynamically allocated objects.
Definition: core.hpp:1268
bool is_thread_safe_
Definition: matchers.hpp:167
GLenum GLint GLuint mask
Definition: tracking.hpp:132
Definition: matchers.hpp:109
GLclampf f
Definition: matchers.hpp:68
int num_matches_thresh2_
Definition: matchers.hpp:183