include/opencv2/videostab/global_motion.hpp
Go to the documentation of this file.
00001 /*M///////////////////////////////////////////////////////////////////////////////////////
00002 //
00003 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
00004 //
00005 //  By downloading, copying, installing or using the software you agree to this license.
00006 //  If you do not agree to this license, do not download, install,
00007 //  copy or use the software.
00008 //
00009 //
00010 //                           License Agreement
00011 //                For Open Source Computer Vision Library
00012 //
00013 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
00014 // Copyright (C) 2009-2011, Willow Garage Inc., all rights reserved.
00015 // Third party copyrights are property of their respective owners.
00016 //
00017 // Redistribution and use in source and binary forms, with or without modification,
00018 // are permitted provided that the following conditions are met:
00019 //
00020 //   * Redistribution's of source code must retain the above copyright notice,
00021 //     this list of conditions and the following disclaimer.
00022 //
00023 //   * Redistribution's in binary form must reproduce the above copyright notice,
00024 //     this list of conditions and the following disclaimer in the documentation
00025 //     and/or other materials provided with the distribution.
00026 //
00027 //   * The name of the copyright holders may not be used to endorse or promote products
00028 //     derived from this software without specific prior written permission.
00029 //
00030 // This software is provided by the copyright holders and contributors "as is" and
00031 // any express or implied warranties, including, but not limited to, the implied
00032 // warranties of merchantability and fitness for a particular purpose are disclaimed.
00033 // In no event shall the Intel Corporation or contributors be liable for any direct,
00034 // indirect, incidental, special, exemplary, or consequential damages
00035 // (including, but not limited to, procurement of substitute goods or services;
00036 // loss of use, data, or profits; or business interruption) however caused
00037 // and on any theory of liability, whether in contract, strict liability,
00038 // or tort (including negligence or otherwise) arising in any way out of
00039 // the use of this software, even if advised of the possibility of such damage.
00040 //
00041 //M*/
00042 
00043 #ifndef __OPENCV_VIDEOSTAB_GLOBAL_MOTION_HPP__
00044 #define __OPENCV_VIDEOSTAB_GLOBAL_MOTION_HPP__
00045 
00046 #include <vector>
00047 #include "opencv2/core/core.hpp"
00048 #include "opencv2/features2d/features2d.hpp"
00049 #include "opencv2/videostab/optical_flow.hpp"
00050 
00051 namespace cv
00052 {
00053 namespace videostab
00054 {
00055 
00056 enum MotionModel
00057 {
00058     TRANSLATION = 0,
00059     TRANSLATION_AND_SCALE = 1,
00060     LINEAR_SIMILARITY = 2,
00061     AFFINE = 3
00062 };
00063 
00064 CV_EXPORTS Mat estimateGlobalMotionLeastSquares(
00065         const std::vector<Point2f> &points0, const std::vector<Point2f> &points1,
00066         int model = AFFINE, float *rmse = 0);
00067 
00068 struct CV_EXPORTS RansacParams
00069 {
00070     int size; // subset size
00071     float thresh; // max error to classify as inlier
00072     float eps; // max outliers ratio
00073     float prob; // probability of success
00074 
00075     RansacParams(int _size, float _thresh, float _eps, float _prob)
00076         : size(_size), thresh(_thresh), eps(_eps), prob(_prob) {}
00077 
00078     static RansacParams translationMotionStd() { return RansacParams(2, 0.5f, 0.5f, 0.99f); }
00079     static RansacParams translationAndScale2dMotionStd() { return RansacParams(3, 0.5f, 0.5f, 0.99f); }
00080     static RansacParams linearSimilarityMotionStd() { return RansacParams(4, 0.5f, 0.5f, 0.99f); }
00081     static RansacParams affine2dMotionStd() { return RansacParams(6, 0.5f, 0.5f, 0.99f); }
00082 };
00083 
00084 CV_EXPORTS Mat estimateGlobalMotionRobust(
00085         const std::vector<Point2f> &points0, const std::vector<Point2f> &points1,
00086         int model = AFFINE, const RansacParams &params = RansacParams::affine2dMotionStd(),
00087         float *rmse = 0, int *ninliers = 0);
00088 
00089 class CV_EXPORTS IGlobalMotionEstimator
00090 {
00091 public:
00092     virtual ~IGlobalMotionEstimator() {}
00093     virtual Mat estimate(const Mat &frame0, const Mat &frame1) = 0;
00094 };
00095 
00096 class CV_EXPORTS PyrLkRobustMotionEstimator : public IGlobalMotionEstimator
00097 {
00098 public:
00099     PyrLkRobustMotionEstimator();
00100 
00101     void setDetector(Ptr<FeatureDetector> val) { detector_ = val; }
00102     Ptr<FeatureDetector> detector() const { return detector_; }
00103 
00104     void setOptFlowEstimator(Ptr<ISparseOptFlowEstimator> val) { optFlowEstimator_ = val; }
00105     Ptr<ISparseOptFlowEstimator> optFlowEstimator() const { return optFlowEstimator_; }
00106 
00107     void setMotionModel(MotionModel val) { motionModel_ = val; }
00108     MotionModel motionModel() const { return motionModel_; }
00109 
00110     void setRansacParams(const RansacParams &val) { ransacParams_ = val; }
00111     RansacParams ransacParams() const { return ransacParams_; }
00112 
00113     void setMaxRmse(float val) { maxRmse_ = val; }
00114     float maxRmse() const { return maxRmse_; }
00115 
00116     void setMinInlierRatio(float val) { minInlierRatio_ = val; }
00117     float minInlierRatio() const { return minInlierRatio_; }
00118 
00119     virtual Mat estimate(const Mat &frame0, const Mat &frame1);
00120 
00121 private:
00122     Ptr<FeatureDetector> detector_;
00123     Ptr<ISparseOptFlowEstimator> optFlowEstimator_;
00124     MotionModel motionModel_;
00125     RansacParams ransacParams_;
00126     std::vector<uchar> status_;
00127     std::vector<KeyPoint> keypointsPrev_;
00128     std::vector<Point2f> pointsPrev_, points_;
00129     std::vector<Point2f> pointsPrevGood_, pointsGood_;
00130     float maxRmse_;
00131     float minInlierRatio_;
00132 };
00133 
00134 CV_EXPORTS Mat getMotion(int from, int to, const Mat *motions, int size);
00135 
00136 CV_EXPORTS Mat getMotion(int from, int to, const std::vector<Mat> &motions);
00137 
00138 } // namespace videostab
00139 } // namespace cv
00140 
00141 #endif