Added initial docs for the videostab module
[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 <string>
48 #include <fstream>
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"
55
56 #ifdef HAVE_OPENCV_GPU
57   #include "opencv2/gpu/gpu.hpp"
58 #endif
59
60 namespace cv
61 {
62 namespace videostab
63 {
64
65 CV_EXPORTS Mat estimateGlobalMotionLeastSquares(
66         InputOutputArray points0, InputOutputArray points1, int model = MM_AFFINE,
67         float *rmse = 0);
68
69 CV_EXPORTS Mat estimateGlobalMotionRansac(
70         InputArray points0, InputArray points1, int model = MM_AFFINE,
71         const RansacParams &params = RansacParams::default2dMotion(MM_AFFINE),
72         float *rmse = 0, int *ninliers = 0);
73
74 class CV_EXPORTS MotionEstimatorBase
75 {
76 public:
77     virtual ~MotionEstimatorBase() {}
78
79     virtual void setMotionModel(MotionModel val) { motionModel_ = val; }
80     virtual MotionModel motionModel() const { return motionModel_; }
81
82     virtual Mat estimate(InputArray points0, InputArray points1, bool *ok = 0) = 0;
83
84 protected:
85     MotionEstimatorBase(MotionModel model) { setMotionModel(model); }
86
87 private:
88     MotionModel motionModel_;
89 };
90
91 class CV_EXPORTS MotionEstimatorRansacL2 : public MotionEstimatorBase
92 {
93 public:
94     MotionEstimatorRansacL2(MotionModel model = MM_AFFINE);
95
96     void setRansacParams(const RansacParams &val) { ransacParams_ = val; }
97     RansacParams ransacParams() const { return ransacParams_; }
98
99     void setMinInlierRatio(float val) { minInlierRatio_ = val; }
100     float minInlierRatio() const { return minInlierRatio_; }
101
102     virtual Mat estimate(InputArray points0, InputArray points1, bool *ok = 0);
103
104 private:
105     RansacParams ransacParams_;
106     float minInlierRatio_;
107 };
108
109 class CV_EXPORTS MotionEstimatorL1 : public MotionEstimatorBase
110 {
111 public:
112     MotionEstimatorL1(MotionModel model = MM_AFFINE);
113
114     virtual Mat estimate(InputArray points0, InputArray points1, bool *ok = 0);
115
116 private:
117     std::vector<double> obj_, collb_, colub_;
118     std::vector<double> elems_, rowlb_, rowub_;
119     std::vector<int> rows_, cols_;
120
121     void set(int row, int col, double coef)
122     {
123         rows_.push_back(row);
124         cols_.push_back(col);
125         elems_.push_back(coef);
126     }
127 };
128
129 class CV_EXPORTS ImageMotionEstimatorBase
130 {
131 public:
132     virtual ~ImageMotionEstimatorBase() {}
133
134     virtual void setMotionModel(MotionModel val) { motionModel_ = val; }
135     virtual MotionModel motionModel() const { return motionModel_; }
136
137     virtual Mat estimate(const Mat &frame0, const Mat &frame1, bool *ok = 0) = 0;
138
139 protected:
140     ImageMotionEstimatorBase(MotionModel model) { setMotionModel(model); }
141
142 private:
143     MotionModel motionModel_;
144 };
145
146 class CV_EXPORTS FromFileMotionReader : public ImageMotionEstimatorBase
147 {
148 public:
149     FromFileMotionReader(const std::string &path);
150
151     virtual Mat estimate(const Mat &frame0, const Mat &frame1, bool *ok = 0);
152
153 private:
154     std::ifstream file_;
155 };
156
157 class CV_EXPORTS ToFileMotionWriter : public ImageMotionEstimatorBase
158 {
159 public:
160     ToFileMotionWriter(const std::string &path, Ptr<ImageMotionEstimatorBase> estimator);
161
162     virtual void setMotionModel(MotionModel val) { motionEstimator_->setMotionModel(val); }
163     virtual MotionModel motionModel() const { return motionEstimator_->motionModel(); }
164
165     virtual Mat estimate(const Mat &frame0, const Mat &frame1, bool *ok = 0);
166
167 private:
168     std::ofstream file_;
169     Ptr<ImageMotionEstimatorBase> motionEstimator_;
170 };
171
172 class CV_EXPORTS KeypointBasedMotionEstimator : public ImageMotionEstimatorBase
173 {
174 public:
175     KeypointBasedMotionEstimator(Ptr<MotionEstimatorBase> estimator);
176
177     virtual void setMotionModel(MotionModel val) { motionEstimator_->setMotionModel(val); }
178     virtual MotionModel motionModel() const { return motionEstimator_->motionModel(); }
179
180     void setDetector(Ptr<FeatureDetector> val) { detector_ = val; }
181     Ptr<FeatureDetector> detector() const { return detector_; }
182
183     void setOpticalFlowEstimator(Ptr<ISparseOptFlowEstimator> val) { optFlowEstimator_ = val; }
184     Ptr<ISparseOptFlowEstimator> opticalFlowEstimator() const { return optFlowEstimator_; }
185
186     void setOutlierRejector(Ptr<IOutlierRejector> val) { outlierRejector_ = val; }
187     Ptr<IOutlierRejector> outlierRejector() const { return outlierRejector_; }
188
189     virtual Mat estimate(const Mat &frame0, const Mat &frame1, bool *ok = 0);
190
191 private:
192     Ptr<MotionEstimatorBase> motionEstimator_;
193     Ptr<FeatureDetector> detector_;
194     Ptr<ISparseOptFlowEstimator> optFlowEstimator_;
195     Ptr<IOutlierRejector> outlierRejector_;
196
197     std::vector<uchar> status_;
198     std::vector<KeyPoint> keypointsPrev_;
199     std::vector<Point2f> pointsPrev_, points_;
200     std::vector<Point2f> pointsPrevGood_, pointsGood_;
201 };
202
203 #ifdef HAVE_OPENCV_GPU
204 class CV_EXPORTS KeypointBasedMotionEstimatorGpu : public ImageMotionEstimatorBase
205 {
206 public:
207     KeypointBasedMotionEstimatorGpu(Ptr<MotionEstimatorBase> estimator);
208
209     virtual void setMotionModel(MotionModel val) { motionEstimator_->setMotionModel(val); }
210     virtual MotionModel motionModel() const { return motionEstimator_->motionModel(); }
211
212     void setOutlierRejector(Ptr<IOutlierRejector> val) { outlierRejector_ = val; }
213     Ptr<IOutlierRejector> outlierRejector() const { return outlierRejector_; }
214
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);
217
218 private:
219     Ptr<MotionEstimatorBase> motionEstimator_;
220     gpu::GoodFeaturesToTrackDetector_GPU detector_;
221     SparsePyrLkOptFlowEstimatorGpu optFlowEstimator_;
222     Ptr<IOutlierRejector> outlierRejector_;
223
224     gpu::GpuMat frame0_, grayFrame0_, frame1_;
225     gpu::GpuMat pointsPrev_, points_;
226     gpu::GpuMat status_;
227
228     Mat hostPointsPrev_, hostPoints_;
229     std::vector<Point2f> hostPointsPrevTmp_, hostPointsTmp_;
230     std::vector<uchar> rejectionStatus_;
231 };
232 #endif
233
234 CV_EXPORTS Mat getMotion(int from, int to, const std::vector<Mat> &motions);
235
236 } // namespace videostab
237 } // namespace cv
238
239 #endif