1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
11 // For Open Source Computer Vision Library
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.
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
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.
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.
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.
43 #ifndef __OPENCV_VIDEOSTAB_GLOBAL_MOTION_HPP__
44 #define __OPENCV_VIDEOSTAB_GLOBAL_MOTION_HPP__
47 #include "opencv2/core/core.hpp"
48 #include "opencv2/features2d/features2d.hpp"
49 #include "opencv2/videostab/optical_flow.hpp"
59 TRANSLATION_AND_SCALE = 1,
60 LINEAR_SIMILARITY = 2,
66 CV_EXPORTS Mat estimateGlobalMotionLeastSquares(
67 const std::vector<Point2f> &points0, const std::vector<Point2f> &points1,
68 int model = AFFINE, float *rmse = 0);
70 struct CV_EXPORTS RansacParams
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
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) {}
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); }
88 CV_EXPORTS Mat estimateGlobalMotionRobust(
89 const std::vector<Point2f> &points0, const std::vector<Point2f> &points1,
90 int model = AFFINE, const RansacParams ¶ms = RansacParams::affine2dMotionStd(),
91 float *rmse = 0, int *ninliers = 0);
93 class CV_EXPORTS GlobalMotionEstimatorBase
96 GlobalMotionEstimatorBase() : motionModel_(UNKNOWN) {}
97 virtual ~GlobalMotionEstimatorBase() {}
99 virtual void setMotionModel(MotionModel val) { motionModel_ = val; }
100 virtual MotionModel motionModel() const { return motionModel_; }
102 virtual Mat estimate(const Mat &frame0, const Mat &frame1) = 0;
105 MotionModel motionModel_;
108 class CV_EXPORTS EyeMotionEstimator : public GlobalMotionEstimatorBase
111 virtual Mat estimate(const Mat &/*frame0*/, const Mat &/*frame1*/)
113 return Mat::eye(3, 3, CV_32F);
117 class CV_EXPORTS PyrLkRobustMotionEstimator : public GlobalMotionEstimatorBase
120 PyrLkRobustMotionEstimator();
122 void setDetector(Ptr<FeatureDetector> val) { detector_ = val; }
123 Ptr<FeatureDetector> detector() const { return detector_; }
125 void setOptFlowEstimator(Ptr<ISparseOptFlowEstimator> val) { optFlowEstimator_ = val; }
126 Ptr<ISparseOptFlowEstimator> optFlowEstimator() const { return optFlowEstimator_; }
128 void setRansacParams(const RansacParams &val) { ransacParams_ = val; }
129 RansacParams ransacParams() const { return ransacParams_; }
131 void setMaxRmse(float val) { maxRmse_ = val; }
132 float maxRmse() const { return maxRmse_; }
134 void setMinInlierRatio(float val) { minInlierRatio_ = val; }
135 float minInlierRatio() const { return minInlierRatio_; }
137 virtual Mat estimate(const Mat &frame0, const Mat &frame1);
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_;
148 float minInlierRatio_;
151 CV_EXPORTS Mat getMotion(int from, int to, const std::vector<Mat> &motions);
153 } // namespace videostab