Refactored videostab module, added base class for woobble suppression
[profile/ivi/opencv.git] / modules / videostab / include / opencv2 / videostab / global_motion.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-2011, 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_VIDEOSTAB_GLOBAL_MOTION_HPP__
44 #define __OPENCV_VIDEOSTAB_GLOBAL_MOTION_HPP__
45
46 #include <vector>
47 #include "opencv2/core/core.hpp"
48 #include "opencv2/features2d/features2d.hpp"
49 #include "opencv2/videostab/optical_flow.hpp"
50
51 namespace cv
52 {
53 namespace videostab
54 {
55
56 enum MotionModel
57 {
58     TRANSLATION = 0,
59     TRANSLATION_AND_SCALE = 1,
60     LINEAR_SIMILARITY = 2,
61     AFFINE = 3,
62     HOMOGRAPHY = 4,
63     UNKNOWN = 5
64 };
65
66 CV_EXPORTS Mat estimateGlobalMotionLeastSquares(
67         const std::vector<Point2f> &points0, const std::vector<Point2f> &points1,
68         int model = AFFINE, float *rmse = 0);
69
70 struct CV_EXPORTS RansacParams
71 {
72     int size; // subset size
73     float thresh; // max error to classify as inlier
74     float eps; // max outliers ratio
75     float prob; // probability of success
76
77     RansacParams() : size(0), thresh(0), eps(0), prob(0) {}
78     RansacParams(int size, float thresh, float eps, float prob)
79         : size(size), thresh(thresh), eps(eps), prob(prob) {}
80
81     static RansacParams translation2dMotionStd() { return RansacParams(2, 0.5f, 0.5f, 0.99f); }
82     static RansacParams translationAndScale2dMotionStd() { return RansacParams(3, 0.5f, 0.5f, 0.99f); }
83     static RansacParams linearSimilarity2dMotionStd() { return RansacParams(4, 0.5f, 0.5f, 0.99f); }
84     static RansacParams affine2dMotionStd() { return RansacParams(6, 0.5f, 0.5f, 0.99f); }
85     static RansacParams homography2dMotionStd() { return RansacParams(8, 0.5f, 0.5f, 0.99f); }
86 };
87
88 CV_EXPORTS Mat estimateGlobalMotionRobust(
89         const std::vector<Point2f> &points0, const std::vector<Point2f> &points1,
90         int model = AFFINE, const RansacParams &params = RansacParams::affine2dMotionStd(),
91         float *rmse = 0, int *ninliers = 0);
92
93 class CV_EXPORTS GlobalMotionEstimatorBase
94 {
95 public:
96     GlobalMotionEstimatorBase() : motionModel_(UNKNOWN) {}
97     virtual ~GlobalMotionEstimatorBase() {}
98
99     virtual void setMotionModel(MotionModel val) { motionModel_ = val; }
100     virtual MotionModel motionModel() const { return motionModel_; }
101
102     virtual Mat estimate(const Mat &frame0, const Mat &frame1) = 0;
103
104 protected:
105     MotionModel motionModel_;
106 };
107
108 class CV_EXPORTS EyeMotionEstimator : public GlobalMotionEstimatorBase
109 {
110 public:
111     virtual Mat estimate(const Mat &/*frame0*/, const Mat &/*frame1*/)
112     {
113         return Mat::eye(3, 3, CV_32F);
114     }
115 };
116
117 class CV_EXPORTS PyrLkRobustMotionEstimator : public GlobalMotionEstimatorBase
118 {
119 public:
120     PyrLkRobustMotionEstimator();
121
122     void setDetector(Ptr<FeatureDetector> val) { detector_ = val; }
123     Ptr<FeatureDetector> detector() const { return detector_; }
124
125     void setOptFlowEstimator(Ptr<ISparseOptFlowEstimator> val) { optFlowEstimator_ = val; }
126     Ptr<ISparseOptFlowEstimator> optFlowEstimator() const { return optFlowEstimator_; }
127
128     void setRansacParams(const RansacParams &val) { ransacParams_ = val; }
129     RansacParams ransacParams() const { return ransacParams_; }
130
131     void setMaxRmse(float val) { maxRmse_ = val; }
132     float maxRmse() const { return maxRmse_; }
133
134     void setMinInlierRatio(float val) { minInlierRatio_ = val; }
135     float minInlierRatio() const { return minInlierRatio_; }
136
137     virtual Mat estimate(const Mat &frame0, const Mat &frame1);
138
139 private:
140     Ptr<FeatureDetector> detector_;
141     Ptr<ISparseOptFlowEstimator> optFlowEstimator_;
142     RansacParams ransacParams_;
143     std::vector<uchar> status_;
144     std::vector<KeyPoint> keypointsPrev_;
145     std::vector<Point2f> pointsPrev_, points_;
146     std::vector<Point2f> pointsPrevGood_, pointsGood_;
147     float maxRmse_;
148     float minInlierRatio_;
149 };
150
151 CV_EXPORTS Mat getMotion(int from, int to, const std::vector<Mat> &motions);
152
153 } // namespace videostab
154 } // namespace cv
155
156 #endif