123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- #ifndef OPENCV_VIDEOSTAB_GLOBAL_MOTION_HPP
- #define OPENCV_VIDEOSTAB_GLOBAL_MOTION_HPP
- #include <vector>
- #include <fstream>
- #include "opencv2/core.hpp"
- #include "opencv2/features2d.hpp"
- #include "opencv2/opencv_modules.hpp"
- #include "opencv2/videostab/optical_flow.hpp"
- #include "opencv2/videostab/motion_core.hpp"
- #include "opencv2/videostab/outlier_rejection.hpp"
- #ifdef HAVE_OPENCV_CUDAIMGPROC
- # include "opencv2/cudaimgproc.hpp"
- #endif
- namespace cv
- {
- namespace videostab
- {
- CV_EXPORTS Mat estimateGlobalMotionLeastSquares(
- InputOutputArray points0, InputOutputArray points1, int model = MM_AFFINE,
- float *rmse = 0);
- CV_EXPORTS Mat estimateGlobalMotionRansac(
- InputArray points0, InputArray points1, int model = MM_AFFINE,
- const RansacParams ¶ms = RansacParams::default2dMotion(MM_AFFINE),
- float *rmse = 0, int *ninliers = 0);
- class CV_EXPORTS MotionEstimatorBase
- {
- public:
- virtual ~MotionEstimatorBase() {}
-
- virtual void setMotionModel(MotionModel val) { motionModel_ = val; }
-
- virtual MotionModel motionModel() const { return motionModel_; }
-
- virtual Mat estimate(InputArray points0, InputArray points1, bool *ok = 0) = 0;
- protected:
- MotionEstimatorBase(MotionModel model) { setMotionModel(model); }
- private:
- MotionModel motionModel_;
- };
- class CV_EXPORTS MotionEstimatorRansacL2 : public MotionEstimatorBase
- {
- public:
- MotionEstimatorRansacL2(MotionModel model = MM_AFFINE);
- void setRansacParams(const RansacParams &val) { ransacParams_ = val; }
- RansacParams ransacParams() const { return ransacParams_; }
- void setMinInlierRatio(float val) { minInlierRatio_ = val; }
- float minInlierRatio() const { return minInlierRatio_; }
- virtual Mat estimate(InputArray points0, InputArray points1, bool *ok = 0);
- private:
- RansacParams ransacParams_;
- float minInlierRatio_;
- };
- class CV_EXPORTS MotionEstimatorL1 : public MotionEstimatorBase
- {
- public:
- MotionEstimatorL1(MotionModel model = MM_AFFINE);
- virtual Mat estimate(InputArray points0, InputArray points1, bool *ok = 0);
- private:
- std::vector<double> obj_, collb_, colub_;
- std::vector<double> elems_, rowlb_, rowub_;
- std::vector<int> rows_, cols_;
- void set(int row, int col, double coef)
- {
- rows_.push_back(row);
- cols_.push_back(col);
- elems_.push_back(coef);
- }
- };
- class CV_EXPORTS ImageMotionEstimatorBase
- {
- public:
- virtual ~ImageMotionEstimatorBase() {}
- virtual void setMotionModel(MotionModel val) { motionModel_ = val; }
- virtual MotionModel motionModel() const { return motionModel_; }
- virtual Mat estimate(const Mat &frame0, const Mat &frame1, bool *ok = 0) = 0;
- protected:
- ImageMotionEstimatorBase(MotionModel model) { setMotionModel(model); }
- private:
- MotionModel motionModel_;
- };
- class CV_EXPORTS FromFileMotionReader : public ImageMotionEstimatorBase
- {
- public:
- FromFileMotionReader(const String &path);
- virtual Mat estimate(const Mat &frame0, const Mat &frame1, bool *ok = 0);
- private:
- std::ifstream file_;
- };
- class CV_EXPORTS ToFileMotionWriter : public ImageMotionEstimatorBase
- {
- public:
- ToFileMotionWriter(const String &path, Ptr<ImageMotionEstimatorBase> estimator);
- virtual void setMotionModel(MotionModel val) { motionEstimator_->setMotionModel(val); }
- virtual MotionModel motionModel() const { return motionEstimator_->motionModel(); }
- virtual Mat estimate(const Mat &frame0, const Mat &frame1, bool *ok = 0);
- private:
- std::ofstream file_;
- Ptr<ImageMotionEstimatorBase> motionEstimator_;
- };
- class CV_EXPORTS KeypointBasedMotionEstimator : public ImageMotionEstimatorBase
- {
- public:
- KeypointBasedMotionEstimator(Ptr<MotionEstimatorBase> estimator);
- virtual void setMotionModel(MotionModel val) { motionEstimator_->setMotionModel(val); }
- virtual MotionModel motionModel() const { return motionEstimator_->motionModel(); }
- void setDetector(Ptr<FeatureDetector> val) { detector_ = val; }
- Ptr<FeatureDetector> detector() const { return detector_; }
- void setOpticalFlowEstimator(Ptr<ISparseOptFlowEstimator> val) { optFlowEstimator_ = val; }
- Ptr<ISparseOptFlowEstimator> opticalFlowEstimator() const { return optFlowEstimator_; }
- void setOutlierRejector(Ptr<IOutlierRejector> val) { outlierRejector_ = val; }
- Ptr<IOutlierRejector> outlierRejector() const { return outlierRejector_; }
- virtual Mat estimate(const Mat &frame0, const Mat &frame1, bool *ok = 0);
- Mat estimate(InputArray frame0, InputArray frame1, bool *ok = 0);
- private:
- Ptr<MotionEstimatorBase> motionEstimator_;
- Ptr<FeatureDetector> detector_;
- Ptr<ISparseOptFlowEstimator> optFlowEstimator_;
- Ptr<IOutlierRejector> outlierRejector_;
- std::vector<uchar> status_;
- std::vector<KeyPoint> keypointsPrev_;
- std::vector<Point2f> pointsPrev_, points_;
- std::vector<Point2f> pointsPrevGood_, pointsGood_;
- };
- #if defined(HAVE_OPENCV_CUDAIMGPROC) && defined(HAVE_OPENCV_CUDAOPTFLOW)
- class CV_EXPORTS KeypointBasedMotionEstimatorGpu : public ImageMotionEstimatorBase
- {
- public:
- KeypointBasedMotionEstimatorGpu(Ptr<MotionEstimatorBase> estimator);
- virtual void setMotionModel(MotionModel val) { motionEstimator_->setMotionModel(val); }
- virtual MotionModel motionModel() const { return motionEstimator_->motionModel(); }
- void setOutlierRejector(Ptr<IOutlierRejector> val) { outlierRejector_ = val; }
- Ptr<IOutlierRejector> outlierRejector() const { return outlierRejector_; }
- virtual Mat estimate(const Mat &frame0, const Mat &frame1, bool *ok = 0);
- Mat estimate(const cuda::GpuMat &frame0, const cuda::GpuMat &frame1, bool *ok = 0);
- private:
- Ptr<MotionEstimatorBase> motionEstimator_;
- Ptr<cuda::CornersDetector> detector_;
- SparsePyrLkOptFlowEstimatorGpu optFlowEstimator_;
- Ptr<IOutlierRejector> outlierRejector_;
- cuda::GpuMat frame0_, grayFrame0_, frame1_;
- cuda::GpuMat pointsPrev_, points_;
- cuda::GpuMat status_;
- Mat hostPointsPrev_, hostPoints_;
- std::vector<Point2f> hostPointsPrevTmp_, hostPointsTmp_;
- std::vector<uchar> rejectionStatus_;
- };
- #endif
- CV_EXPORTS Mat getMotion(int from, int to, const std::vector<Mat> &motions);
- }
- }
- #endif
|