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__
49 #include "opencv2/core/core.hpp"
50 #include "opencv2/features2d/features2d.hpp"
51 #include "opencv2/opencv_modules.hpp"
52 #include "opencv2/videostab/optical_flow.hpp"
53 #include "opencv2/videostab/motion_core.hpp"
54 #include "opencv2/videostab/outlier_rejection.hpp"
56 #ifdef HAVE_OPENCV_GPU
57 #include "opencv2/gpu/gpu.hpp"
65 CV_EXPORTS Mat estimateGlobalMotionLeastSquares(
66 InputOutputArray points0, InputOutputArray points1, int model = MM_AFFINE,
69 CV_EXPORTS Mat estimateGlobalMotionRansac(
70 InputArray points0, InputArray points1, int model = MM_AFFINE,
71 const RansacParams ¶ms = RansacParams::default2dMotion(MM_AFFINE),
72 float *rmse = 0, int *ninliers = 0);
74 class CV_EXPORTS MotionEstimatorBase
77 virtual ~MotionEstimatorBase() {}
79 virtual void setMotionModel(MotionModel val) { motionModel_ = val; }
80 virtual MotionModel motionModel() const { return motionModel_; }
82 virtual Mat estimate(InputArray points0, InputArray points1, bool *ok = 0) = 0;
85 MotionEstimatorBase(MotionModel model) { setMotionModel(model); }
88 MotionModel motionModel_;
91 class CV_EXPORTS MotionEstimatorRansacL2 : public MotionEstimatorBase
94 MotionEstimatorRansacL2(MotionModel model = MM_AFFINE);
96 void setRansacParams(const RansacParams &val) { ransacParams_ = val; }
97 RansacParams ransacParams() const { return ransacParams_; }
99 void setMinInlierRatio(float val) { minInlierRatio_ = val; }
100 float minInlierRatio() const { return minInlierRatio_; }
102 virtual Mat estimate(InputArray points0, InputArray points1, bool *ok = 0);
105 RansacParams ransacParams_;
106 float minInlierRatio_;
109 class CV_EXPORTS MotionEstimatorL1 : public MotionEstimatorBase
112 MotionEstimatorL1(MotionModel model = MM_AFFINE);
114 virtual Mat estimate(InputArray points0, InputArray points1, bool *ok = 0);
117 std::vector<double> obj_, collb_, colub_;
118 std::vector<double> elems_, rowlb_, rowub_;
119 std::vector<int> rows_, cols_;
121 void set(int row, int col, double coef)
123 rows_.push_back(row);
124 cols_.push_back(col);
125 elems_.push_back(coef);
129 class CV_EXPORTS ImageMotionEstimatorBase
132 virtual ~ImageMotionEstimatorBase() {}
134 virtual void setMotionModel(MotionModel val) { motionModel_ = val; }
135 virtual MotionModel motionModel() const { return motionModel_; }
137 virtual Mat estimate(const Mat &frame0, const Mat &frame1, bool *ok = 0) = 0;
140 ImageMotionEstimatorBase(MotionModel model) { setMotionModel(model); }
143 MotionModel motionModel_;
146 class CV_EXPORTS FromFileMotionReader : public ImageMotionEstimatorBase
149 FromFileMotionReader(const std::string &path);
151 virtual Mat estimate(const Mat &frame0, const Mat &frame1, bool *ok = 0);
157 class CV_EXPORTS ToFileMotionWriter : public ImageMotionEstimatorBase
160 ToFileMotionWriter(const std::string &path, Ptr<ImageMotionEstimatorBase> estimator);
162 virtual void setMotionModel(MotionModel val) { motionEstimator_->setMotionModel(val); }
163 virtual MotionModel motionModel() const { return motionEstimator_->motionModel(); }
165 virtual Mat estimate(const Mat &frame0, const Mat &frame1, bool *ok = 0);
169 Ptr<ImageMotionEstimatorBase> motionEstimator_;
172 class CV_EXPORTS KeypointBasedMotionEstimator : public ImageMotionEstimatorBase
175 KeypointBasedMotionEstimator(Ptr<MotionEstimatorBase> estimator);
177 virtual void setMotionModel(MotionModel val) { motionEstimator_->setMotionModel(val); }
178 virtual MotionModel motionModel() const { return motionEstimator_->motionModel(); }
180 void setDetector(Ptr<FeatureDetector> val) { detector_ = val; }
181 Ptr<FeatureDetector> detector() const { return detector_; }
183 void setOpticalFlowEstimator(Ptr<ISparseOptFlowEstimator> val) { optFlowEstimator_ = val; }
184 Ptr<ISparseOptFlowEstimator> opticalFlowEstimator() const { return optFlowEstimator_; }
186 void setOutlierRejector(Ptr<IOutlierRejector> val) { outlierRejector_ = val; }
187 Ptr<IOutlierRejector> outlierRejector() const { return outlierRejector_; }
189 virtual Mat estimate(const Mat &frame0, const Mat &frame1, bool *ok = 0);
192 Ptr<MotionEstimatorBase> motionEstimator_;
193 Ptr<FeatureDetector> detector_;
194 Ptr<ISparseOptFlowEstimator> optFlowEstimator_;
195 Ptr<IOutlierRejector> outlierRejector_;
197 std::vector<uchar> status_;
198 std::vector<KeyPoint> keypointsPrev_;
199 std::vector<Point2f> pointsPrev_, points_;
200 std::vector<Point2f> pointsPrevGood_, pointsGood_;
203 #ifdef HAVE_OPENCV_GPU
204 class CV_EXPORTS KeypointBasedMotionEstimatorGpu : public ImageMotionEstimatorBase
207 KeypointBasedMotionEstimatorGpu(Ptr<MotionEstimatorBase> estimator);
209 virtual void setMotionModel(MotionModel val) { motionEstimator_->setMotionModel(val); }
210 virtual MotionModel motionModel() const { return motionEstimator_->motionModel(); }
212 void setOutlierRejector(Ptr<IOutlierRejector> val) { outlierRejector_ = val; }
213 Ptr<IOutlierRejector> outlierRejector() const { return outlierRejector_; }
215 virtual Mat estimate(const Mat &frame0, const Mat &frame1, bool *ok = 0);
216 Mat estimate(const gpu::GpuMat &frame0, const gpu::GpuMat &frame1, bool *ok = 0);
219 Ptr<MotionEstimatorBase> motionEstimator_;
220 gpu::GoodFeaturesToTrackDetector_GPU detector_;
221 SparsePyrLkOptFlowEstimatorGpu optFlowEstimator_;
222 Ptr<IOutlierRejector> outlierRejector_;
224 gpu::GpuMat frame0_, grayFrame0_, frame1_;
225 gpu::GpuMat pointsPrev_, points_;
228 Mat hostPointsPrev_, hostPoints_;
229 std::vector<Point2f> hostPointsPrevTmp_, hostPointsTmp_;
230 std::vector<uchar> rejectionStatus_;
234 CV_EXPORTS Mat getMotion(int from, int to, const std::vector<Mat> &motions);
236 } // namespace videostab