From: Alexey Spizhevoy Date: Wed, 4 Apr 2012 09:28:47 +0000 (+0000) Subject: Fixed some bugs in videostab module and refactored it X-Git-Tag: accepted/2.0/20130307.220821~364^2~951 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=ecb1f0e288514083d4c361e7de89d56b00764418;p=profile%2Fivi%2Fopencv.git Fixed some bugs in videostab module and refactored it --- diff --git a/modules/videostab/include/opencv2/videostab/global_motion.hpp b/modules/videostab/include/opencv2/videostab/global_motion.hpp index 2acaad2..2bf0781 100644 --- a/modules/videostab/include/opencv2/videostab/global_motion.hpp +++ b/modules/videostab/include/opencv2/videostab/global_motion.hpp @@ -132,8 +132,6 @@ private: float minInlierRatio_; }; -CV_EXPORTS Mat getMotion(int from, int to, const Mat *motions, int size); - CV_EXPORTS Mat getMotion(int from, int to, const std::vector &motions); } // namespace videostab diff --git a/modules/videostab/include/opencv2/videostab/motion_stabilizing.hpp b/modules/videostab/include/opencv2/videostab/motion_stabilizing.hpp index fa6aeb2..f9775b1 100644 --- a/modules/videostab/include/opencv2/videostab/motion_stabilizing.hpp +++ b/modules/videostab/include/opencv2/videostab/motion_stabilizing.hpp @@ -44,6 +44,7 @@ #define __OPENCV_VIDEOSTAB_MOTION_STABILIZING_HPP__ #include +#include #include "opencv2/core/core.hpp" namespace cv @@ -55,15 +56,24 @@ class CV_EXPORTS IMotionStabilizer { public: virtual ~IMotionStabilizer() {} - virtual void stabilize(const Mat *motions, int size, Mat *stabilizationMotions) const = 0; + + // assumes that [range.first, range.second) is in or equals to [0, size-2] + virtual void stabilize( + int size, const std::vector &motions, std::pair range, + Mat *stabilizationMotions) const = 0; }; class CV_EXPORTS MotionFilterBase : public IMotionStabilizer { public: virtual ~MotionFilterBase() {} - virtual Mat stabilize(int index, const Mat *motions, int size) const = 0; - virtual void stabilize(const Mat *motions, int size, Mat *stabilizationMotions) const; + + virtual Mat stabilize( + int idx, const std::vector &motions, std::pair range) const = 0; + + virtual void stabilize( + int size, const std::vector &motions, std::pair range, + Mat *stabilizationMotions) const; }; class CV_EXPORTS GaussianMotionFilter : public MotionFilterBase @@ -75,7 +85,8 @@ public: int radius() const { return radius_; } float stdev() const { return stdev_; } - virtual Mat stabilize(int index, const Mat *motions, int size) const; + virtual Mat stabilize( + int idx, const std::vector &motions, std::pair range) const; private: int radius_; diff --git a/modules/videostab/include/opencv2/videostab/ring_buffer.hpp b/modules/videostab/include/opencv2/videostab/ring_buffer.hpp new file mode 100644 index 0000000..c03052c --- /dev/null +++ b/modules/videostab/include/opencv2/videostab/ring_buffer.hpp @@ -0,0 +1,67 @@ +/*M/////////////////////////////////////////////////////////////////////////////////////// +// +// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. +// +// By downloading, copying, installing or using the software you agree to this license. +// If you do not agree to this license, do not download, install, +// copy or use the software. +// +// +// License Agreement +// For Open Source Computer Vision Library +// +// Copyright (C) 2000-2008, Intel Corporation, all rights reserved. +// Copyright (C) 2009-2011, Willow Garage Inc., all rights reserved. +// Third party copyrights are property of their respective owners. +// +// Redistribution and use in source and binary forms, with or without modification, +// are permitted provided that the following conditions are met: +// +// * Redistribution's of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// * Redistribution's in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// * The name of the copyright holders may not be used to endorse or promote products +// derived from this software without specific prior written permission. +// +// This software is provided by the copyright holders and contributors "as is" and +// any express or implied warranties, including, but not limited to, the implied +// warranties of merchantability and fitness for a particular purpose are disclaimed. +// In no event shall the Intel Corporation or contributors be liable for any direct, +// indirect, incidental, special, exemplary, or consequential damages +// (including, but not limited to, procurement of substitute goods or services; +// loss of use, data, or profits; or business interruption) however caused +// and on any theory of liability, whether in contract, strict liability, +// or tort (including negligence or otherwise) arising in any way out of +// the use of this software, even if advised of the possibility of such damage. +// +//M*/ + +#ifndef __OPENCV_VIDEOSTAB_RING_BUFFER_HPP__ +#define __OPENCV_VIDEOSTAB_RING_BUFFER_HPP__ + +#include +#include "opencv2/imgproc/imgproc.hpp" + +namespace cv +{ +namespace videostab +{ + +template inline T& at(int idx, std::vector &items) +{ + return items[cv::borderInterpolate(idx, static_cast(items.size()), cv::BORDER_WRAP)]; +} + +template inline const T& at(int idx, const std::vector &items) +{ + return items[cv::borderInterpolate(idx, static_cast(items.size()), cv::BORDER_WRAP)]; +} + +} // namespace videostab +} // namespace cv + +#endif diff --git a/modules/videostab/include/opencv2/videostab/videostab.hpp b/modules/videostab/include/opencv2/videostab/videostab.hpp index 3ea34a8..35f9b22 100644 --- a/modules/videostab/include/opencv2/videostab/videostab.hpp +++ b/modules/videostab/include/opencv2/videostab/videostab.hpp @@ -44,5 +44,6 @@ #define __OPENCV_VIDEOSTAB_HPP__ #include "opencv2/videostab/stabilizer.hpp" +#include "opencv2/videostab/ring_buffer.hpp" #endif diff --git a/modules/videostab/src/deblurring.cpp b/modules/videostab/src/deblurring.cpp index 813e908..e26c4b3 100644 --- a/modules/videostab/src/deblurring.cpp +++ b/modules/videostab/src/deblurring.cpp @@ -43,6 +43,7 @@ #include "precomp.hpp" #include "opencv2/videostab/deblurring.hpp" #include "opencv2/videostab/global_motion.hpp" +#include "opencv2/videostab/ring_buffer.hpp" using namespace std; diff --git a/modules/videostab/src/fast_marching.cpp b/modules/videostab/src/fast_marching.cpp index dd18e2f..85bb26d 100644 --- a/modules/videostab/src/fast_marching.cpp +++ b/modules/videostab/src/fast_marching.cpp @@ -42,6 +42,7 @@ #include "precomp.hpp" #include "opencv2/videostab/fast_marching.hpp" +#include "opencv2/videostab/ring_buffer.hpp" using namespace std; diff --git a/modules/videostab/src/frame_source.cpp b/modules/videostab/src/frame_source.cpp index b363016..4d6a30e 100644 --- a/modules/videostab/src/frame_source.cpp +++ b/modules/videostab/src/frame_source.cpp @@ -42,6 +42,7 @@ #include "precomp.hpp" #include "opencv2/videostab/frame_source.hpp" +#include "opencv2/videostab/ring_buffer.hpp" using namespace std; diff --git a/modules/videostab/src/global_motion.cpp b/modules/videostab/src/global_motion.cpp index c522a76..a6c051b 100644 --- a/modules/videostab/src/global_motion.cpp +++ b/modules/videostab/src/global_motion.cpp @@ -43,6 +43,7 @@ #include "precomp.hpp" #include "opencv2/highgui/highgui.hpp" #include "opencv2/videostab/global_motion.hpp" +#include "opencv2/videostab/ring_buffer.hpp" using namespace std; @@ -333,28 +334,22 @@ Mat PyrLkRobustMotionEstimator::estimate(const Mat &frame0, const Mat &frame1) } -Mat getMotion(int from, int to, const Mat *motions, int size) +Mat getMotion(int from, int to, const vector &motions) { Mat M = Mat::eye(3, 3, CV_32F); if (to > from) { for (int i = from; i < to; ++i) - M = at(i, motions, size) * M; + M = at(i, motions) * M; } else if (from > to) { for (int i = to; i < from; ++i) - M = at(i, motions, size) * M; + M = at(i, motions) * M; M = M.inv(); } return M; } - -Mat getMotion(int from, int to, const vector &motions) -{ - return getMotion(from, to, &motions[0], motions.size()); -} - } // namespace videostab } // namespace cv diff --git a/modules/videostab/src/inpainting.cpp b/modules/videostab/src/inpainting.cpp index a2e3c2c..c465a22 100644 --- a/modules/videostab/src/inpainting.cpp +++ b/modules/videostab/src/inpainting.cpp @@ -45,6 +45,7 @@ #include "opencv2/videostab/inpainting.hpp" #include "opencv2/videostab/global_motion.hpp" #include "opencv2/videostab/fast_marching.hpp" +#include "opencv2/videostab/ring_buffer.hpp" using namespace std; diff --git a/modules/videostab/src/motion_stabilizing.cpp b/modules/videostab/src/motion_stabilizing.cpp index b35a20a..eb16749 100644 --- a/modules/videostab/src/motion_stabilizing.cpp +++ b/modules/videostab/src/motion_stabilizing.cpp @@ -43,6 +43,7 @@ #include "precomp.hpp" #include "opencv2/videostab/motion_stabilizing.hpp" #include "opencv2/videostab/global_motion.hpp" +#include "opencv2/videostab/ring_buffer.hpp" using namespace std; @@ -51,17 +52,18 @@ namespace cv namespace videostab { -void MotionFilterBase::stabilize(const Mat *motions, int size, Mat *stabilizationMotions) const +void MotionFilterBase::stabilize( + int size, const vector &motions, pair range, Mat *stabilizationMotions) const { for (int i = 0; i < size; ++i) - stabilizationMotions[i] = stabilize(i, motions, size); + stabilizationMotions[i] = stabilize(i, motions, range); } void GaussianMotionFilter::setParams(int radius, float stdev) { radius_ = radius; - stdev_ = stdev > 0.f ? stdev : sqrt(static_cast(radius_)); + stdev_ = stdev > 0.f ? stdev : sqrt(static_cast(radius)); float sum = 0; weight_.resize(2*radius_ + 1); @@ -72,17 +74,19 @@ void GaussianMotionFilter::setParams(int radius, float stdev) } -Mat GaussianMotionFilter::stabilize(int index, const Mat *motions, int size) const +Mat GaussianMotionFilter::stabilize(int idx, const vector &motions, pair range) const { - const Mat &cur = at(index, motions, size); + const Mat &cur = at(idx, motions); Mat res = Mat::zeros(cur.size(), cur.type()); float sum = 0.f; - for (int i = std::max(index - radius_, 0); i <= index + radius_; ++i) + int iMin = std::max(idx - radius_, range.first); + int iMax = std::min(idx + radius_, range.second); + for (int i = iMin; i <= iMax; ++i) { - res += weight_[radius_ + i - index] * getMotion(index, i, motions, size); - sum += weight_[radius_ + i - index]; + res += weight_[radius_ + i - idx] * getMotion(idx, i, motions); + sum += weight_[radius_ + i - idx]; } - return res / sum; + return sum > 0.f ? res / sum : Mat::eye(cur.size(), cur.type()); } diff --git a/modules/videostab/src/optical_flow.cpp b/modules/videostab/src/optical_flow.cpp index 083c0e0..92c1780 100644 --- a/modules/videostab/src/optical_flow.cpp +++ b/modules/videostab/src/optical_flow.cpp @@ -43,6 +43,7 @@ #include "precomp.hpp" #include "opencv2/video/video.hpp" #include "opencv2/videostab/optical_flow.hpp" +#include "opencv2/videostab/ring_buffer.hpp" using namespace std; diff --git a/modules/videostab/src/precomp.hpp b/modules/videostab/src/precomp.hpp index bdf1e49..5c74f04 100644 --- a/modules/videostab/src/precomp.hpp +++ b/modules/videostab/src/precomp.hpp @@ -55,6 +55,8 @@ #include "opencv2/video/video.hpp" #include "opencv2/features2d/features2d.hpp" +// some aux. functions + inline float sqr(float x) { return x * x; } inline float intensity(const cv::Point3_ &bgr) @@ -62,25 +64,5 @@ inline float intensity(const cv::Point3_ &bgr) return 0.3f*bgr.x + 0.59f*bgr.y + 0.11f*bgr.z; } -template inline T& at(int index, T *items, int size) -{ - return items[cv::borderInterpolate(index, size, cv::BORDER_WRAP)]; -} - -template inline const T& at(int index, const T *items, int size) -{ - return items[cv::borderInterpolate(index, size, cv::BORDER_WRAP)]; -} - -template inline T& at(int index, std::vector &items) -{ - return at(index, &items[0], items.size()); -} - -template inline const T& at(int index, const std::vector &items) -{ - return items[cv::borderInterpolate(index, items.size(), cv::BORDER_WRAP)]; -} - #endif diff --git a/modules/videostab/src/stabilizer.cpp b/modules/videostab/src/stabilizer.cpp index bca316c..becc47e 100644 --- a/modules/videostab/src/stabilizer.cpp +++ b/modules/videostab/src/stabilizer.cpp @@ -42,6 +42,7 @@ #include "precomp.hpp" #include "opencv2/videostab/stabilizer.hpp" +#include "opencv2/videostab/ring_buffer.hpp" using namespace std; @@ -145,7 +146,7 @@ bool StabilizerBase::doOneIteration() { curStabilizedPos_++; at(curStabilizedPos_ + radius_, frames_) = at(curPos_, frames_); - at(curStabilizedPos_ + radius_ - 1, motions_) = at(curPos_ - 1, motions_); + at(curStabilizedPos_ + radius_ - 1, motions_) = Mat::eye(3, 3, CV_32F); stabilizeFrame(); log_->print("."); @@ -212,7 +213,7 @@ void OnePassStabilizer::reset() stabilizedFrames_.clear(); stabilizationMotions_.clear(); doDeblurring_ = false; - doInpainting_ = false; + doInpainting_ = false; } @@ -251,7 +252,7 @@ void OnePassStabilizer::estimateMotion() void OnePassStabilizer::stabilizeFrame() { - Mat stabilizationMotion = motionFilter_->stabilize(curStabilizedPos_, &motions_[0], motions_.size()); + Mat stabilizationMotion = motionFilter_->stabilize(curStabilizedPos_, motions_, make_pair(0, curPos_)); StabilizerBase::stabilizeFrame(stabilizationMotion); } @@ -326,7 +327,9 @@ void TwoPassStabilizer::runPrePassIfNecessary() log_->print("\n"); stabilizationMotions_.resize(frameCount_); - motionStabilizer_->stabilize(&motions_[0], frameCount_, &stabilizationMotions_[0]); + + motionStabilizer_->stabilize( + frameCount_, motions_, make_pair(0, frameCount_ - 1), &stabilizationMotions_[0]); if (mustEstTrimRatio_) {