Merge pull request #3158 from arielelkin:master
[profile/ivi/opencv.git] / modules / stitching / include / opencv2 / stitching / detail / matchers.hpp
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 #ifdef HAVE_OPENCV_XFEATURES2D
52 #  include "opencv2/xfeatures2d/cuda.hpp"
53 #endif
54
55 namespace cv {
56 namespace detail {
57
58 struct CV_EXPORTS ImageFeatures
59 {
60     int img_idx;
61     Size img_size;
62     std::vector<KeyPoint> keypoints;
63     UMat descriptors;
64 };
65
66
67 class CV_EXPORTS FeaturesFinder
68 {
69 public:
70     virtual ~FeaturesFinder() {}
71     void operator ()(InputArray image, ImageFeatures &features);
72     void operator ()(InputArray image, ImageFeatures &features, const std::vector<cv::Rect> &rois);
73     virtual void collectGarbage() {}
74
75 protected:
76     virtual void find(InputArray image, ImageFeatures &features) = 0;
77 };
78
79
80 class CV_EXPORTS SurfFeaturesFinder : public FeaturesFinder
81 {
82 public:
83     SurfFeaturesFinder(double hess_thresh = 300., int num_octaves = 3, int num_layers = 4,
84                        int num_octaves_descr = /*4*/3, int num_layers_descr = /*2*/4);
85
86 private:
87     void find(InputArray image, ImageFeatures &features);
88
89     Ptr<FeatureDetector> detector_;
90     Ptr<DescriptorExtractor> extractor_;
91     Ptr<Feature2D> surf;
92 };
93
94 class CV_EXPORTS OrbFeaturesFinder : public FeaturesFinder
95 {
96 public:
97     OrbFeaturesFinder(Size _grid_size = Size(3,1), int nfeatures=1500, float scaleFactor=1.3f, int nlevels=5);
98
99 private:
100     void find(InputArray image, ImageFeatures &features);
101
102     Ptr<ORB> orb;
103     Size grid_size;
104 };
105
106
107 #ifdef HAVE_OPENCV_XFEATURES2D
108 class CV_EXPORTS SurfFeaturesFinderGpu : public FeaturesFinder
109 {
110 public:
111     SurfFeaturesFinderGpu(double hess_thresh = 300., int num_octaves = 3, int num_layers = 4,
112                           int num_octaves_descr = 4, int num_layers_descr = 2);
113
114     void collectGarbage();
115
116 private:
117     void find(InputArray image, ImageFeatures &features);
118
119     cuda::GpuMat image_;
120     cuda::GpuMat gray_image_;
121     cuda::SURF_CUDA surf_;
122     cuda::GpuMat keypoints_;
123     cuda::GpuMat descriptors_;
124     int num_octaves_, num_layers_;
125     int num_octaves_descr_, num_layers_descr_;
126 };
127 #endif
128
129
130 struct CV_EXPORTS MatchesInfo
131 {
132     MatchesInfo();
133     MatchesInfo(const MatchesInfo &other);
134     const MatchesInfo& operator =(const MatchesInfo &other);
135
136     int src_img_idx, dst_img_idx;       // Images indices (optional)
137     std::vector<DMatch> matches;
138     std::vector<uchar> inliers_mask;    // Geometrically consistent matches mask
139     int num_inliers;                    // Number of geometrically consistent matches
140     Mat H;                              // Estimated homography
141     double confidence;                  // Confidence two images are from the same panorama
142 };
143
144
145 class CV_EXPORTS FeaturesMatcher
146 {
147 public:
148     virtual ~FeaturesMatcher() {}
149
150     void operator ()(const ImageFeatures &features1, const ImageFeatures &features2,
151                      MatchesInfo& matches_info) { match(features1, features2, matches_info); }
152
153     void operator ()(const std::vector<ImageFeatures> &features, std::vector<MatchesInfo> &pairwise_matches,
154                      const cv::UMat &mask = cv::UMat());
155
156     bool isThreadSafe() const { return is_thread_safe_; }
157
158     virtual void collectGarbage() {}
159
160 protected:
161     FeaturesMatcher(bool is_thread_safe = false) : is_thread_safe_(is_thread_safe) {}
162
163     virtual void match(const ImageFeatures &features1, const ImageFeatures &features2,
164                        MatchesInfo& matches_info) = 0;
165
166     bool is_thread_safe_;
167 };
168
169
170 class CV_EXPORTS BestOf2NearestMatcher : public FeaturesMatcher
171 {
172 public:
173     BestOf2NearestMatcher(bool try_use_gpu = false, float match_conf = 0.3f, int num_matches_thresh1 = 6,
174                           int num_matches_thresh2 = 6);
175
176     void collectGarbage();
177
178 protected:
179     void match(const ImageFeatures &features1, const ImageFeatures &features2, MatchesInfo &matches_info);
180
181     int num_matches_thresh1_;
182     int num_matches_thresh2_;
183     Ptr<FeaturesMatcher> impl_;
184 };
185
186 class CV_EXPORTS BestOf2NearestRangeMatcher : public BestOf2NearestMatcher
187 {
188 public:
189     BestOf2NearestRangeMatcher(int range_width = 5, bool try_use_gpu = false, float match_conf = 0.3f,
190                             int num_matches_thresh1 = 6, int num_matches_thresh2 = 6);
191
192     void operator ()(const std::vector<ImageFeatures> &features, std::vector<MatchesInfo> &pairwise_matches,
193                      const cv::UMat &mask = cv::UMat());
194
195
196 protected:
197     int range_width_;
198 };
199
200 } // namespace detail
201 } // namespace cv
202
203 #endif // __OPENCV_STITCHING_MATCHERS_HPP__