From a7da9681c38d37bd0b00d32d56659cd5513396ed Mon Sep 17 00:00:00 2001 From: Alexey Spizhevoy Date: Wed, 4 Apr 2012 13:58:38 +0000 Subject: [PATCH] Added motion stabilization pipeline (videostab module) --- .../opencv2/videostab/motion_stabilizing.hpp | 16 ++++++++++++- modules/videostab/src/motion_stabilizing.cpp | 27 ++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/modules/videostab/include/opencv2/videostab/motion_stabilizing.hpp b/modules/videostab/include/opencv2/videostab/motion_stabilizing.hpp index f9775b1..d51f3a5 100644 --- a/modules/videostab/include/opencv2/videostab/motion_stabilizing.hpp +++ b/modules/videostab/include/opencv2/videostab/motion_stabilizing.hpp @@ -57,12 +57,26 @@ class CV_EXPORTS IMotionStabilizer public: virtual ~IMotionStabilizer() {} - // assumes that [range.first, range.second) is in or equals to [0, size-2] + // assumes that [0, size-1) is in or equals to [range.first, range.second) virtual void stabilize( int size, const std::vector &motions, std::pair range, Mat *stabilizationMotions) const = 0; }; +class CV_EXPORTS MotionStabilizationPipeline : public IMotionStabilizer +{ +public: + void pushBack(Ptr stabilizer) { stabilizers_.push_back(stabilizer); } + bool empty() const { return stabilizers_.empty(); } + + virtual void stabilize( + int size, const std::vector &motions, std::pair range, + Mat *stabilizationMotions) const; + +private: + std::vector > stabilizers_; +}; + class CV_EXPORTS MotionFilterBase : public IMotionStabilizer { public: diff --git a/modules/videostab/src/motion_stabilizing.cpp b/modules/videostab/src/motion_stabilizing.cpp index eb16749..e255ff8 100644 --- a/modules/videostab/src/motion_stabilizing.cpp +++ b/modules/videostab/src/motion_stabilizing.cpp @@ -52,6 +52,33 @@ namespace cv namespace videostab { +void MotionStabilizationPipeline::stabilize( + int size, const vector &motions, pair range, + Mat *stabilizationMotions) const +{ + vector updatedMotions(motions); + vector stabilizationMotions_(size); + + for (int i = 0; i < size; ++i) + stabilizationMotions[i] = Mat::eye(3, 3, CV_32F); + + for (size_t i = 0; i < stabilizers_.size(); ++i) + { + stabilizers_[i]->stabilize(size, updatedMotions, range, &stabilizationMotions_[0]); + + for (int i = 0; i < size; ++i) + stabilizationMotions[i] = stabilizationMotions_[i] * stabilizationMotions[i]; + + for (int j = 0; j + 1 < size; ++j) + { + Mat S0 = stabilizationMotions[j]; + Mat S1 = stabilizationMotions[j+1]; + at(j, updatedMotions) = S1 * at(j, updatedMotions) * S0.inv(); + } + } +} + + void MotionFilterBase::stabilize( int size, const vector &motions, pair range, Mat *stabilizationMotions) const { -- 2.7.4