Path: blob/master/modules/videostab/src/wobble_suppression.cpp
16344 views
/*M///////////////////////////////////////////////////////////////////////////////////////1//2// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.3//4// By downloading, copying, installing or using the software you agree to this license.5// If you do not agree to this license, do not download, install,6// copy or use the software.7//8//9// License Agreement10// For Open Source Computer Vision Library11//12// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.13// Copyright (C) 2009-2011, Willow Garage Inc., all rights reserved.14// Third party copyrights are property of their respective owners.15//16// Redistribution and use in source and binary forms, with or without modification,17// are permitted provided that the following conditions are met:18//19// * Redistribution's of source code must retain the above copyright notice,20// this list of conditions and the following disclaimer.21//22// * Redistribution's in binary form must reproduce the above copyright notice,23// this list of conditions and the following disclaimer in the documentation24// and/or other materials provided with the distribution.25//26// * The name of the copyright holders may not be used to endorse or promote products27// derived from this software without specific prior written permission.28//29// This software is provided by the copyright holders and contributors "as is" and30// any express or implied warranties, including, but not limited to, the implied31// warranties of merchantability and fitness for a particular purpose are disclaimed.32// In no event shall the Intel Corporation or contributors be liable for any direct,33// indirect, incidental, special, exemplary, or consequential damages34// (including, but not limited to, procurement of substitute goods or services;35// loss of use, data, or profits; or business interruption) however caused36// and on any theory of liability, whether in contract, strict liability,37// or tort (including negligence or otherwise) arising in any way out of38// the use of this software, even if advised of the possibility of such damage.39//40//M*/4142#include "precomp.hpp"43#include "opencv2/videostab/wobble_suppression.hpp"44#include "opencv2/videostab/ring_buffer.hpp"4546#include "opencv2/core/private.cuda.hpp"4748#ifdef HAVE_OPENCV_CUDAWARPING49# include "opencv2/cudawarping.hpp"50#endif5152#if defined(HAVE_OPENCV_CUDAWARPING)53#if !defined HAVE_CUDA || defined(CUDA_DISABLER)54namespace cv { namespace cuda {55static void calcWobbleSuppressionMaps(int, int, int, Size, const Mat&, const Mat&, GpuMat&, GpuMat&) { throw_no_cuda(); }56}}57#else58namespace cv { namespace cuda { namespace device { namespace globmotion {59void calcWobbleSuppressionMaps(60int left, int idx, int right, int width, int height,61const float *ml, const float *mr, PtrStepSzf mapx, PtrStepSzf mapy);62}}}}63namespace cv { namespace cuda {64static void calcWobbleSuppressionMaps(65int left, int idx, int right, Size size, const Mat &ml, const Mat &mr,66GpuMat &mapx, GpuMat &mapy)67{68CV_Assert(ml.size() == Size(3, 3) && ml.type() == CV_32F && ml.isContinuous());69CV_Assert(mr.size() == Size(3, 3) && mr.type() == CV_32F && mr.isContinuous());7071mapx.create(size, CV_32F);72mapy.create(size, CV_32F);7374cv::cuda::device::globmotion::calcWobbleSuppressionMaps(75left, idx, right, size.width, size.height,76ml.ptr<float>(), mr.ptr<float>(), mapx, mapy);77}78}}79#endif80#endif8182namespace cv83{84namespace videostab85{8687WobbleSuppressorBase::WobbleSuppressorBase() : frameCount_(0), motions_(0), motions2_(0), stabilizationMotions_(0)88{89setMotionEstimator(makePtr<KeypointBasedMotionEstimator>(makePtr<MotionEstimatorRansacL2>(MM_HOMOGRAPHY)));90}919293void NullWobbleSuppressor::suppress(int /*idx*/, const Mat &frame, Mat &result)94{95result = frame;96}979899void MoreAccurateMotionWobbleSuppressor::suppress(int idx, const Mat &frame, Mat &result)100{101CV_Assert(motions_ && stabilizationMotions_);102103if (idx % period_ == 0)104{105result = frame;106return;107}108109int k1 = idx / period_ * period_;110int k2 = std::min(k1 + period_, frameCount_ - 1);111112Mat S1 = (*stabilizationMotions_)[idx];113114Mat_<float> ML = S1 * getMotion(k1, idx, *motions2_) * getMotion(k1, idx, *motions_).inv() * S1.inv();115Mat_<float> MR = S1 * getMotion(idx, k2, *motions2_).inv() * getMotion(idx, k2, *motions_) * S1.inv();116117mapx_.create(frame.size());118mapy_.create(frame.size());119120float xl, yl, zl, wl;121float xr, yr, zr, wr;122123for (int y = 0; y < frame.rows; ++y)124{125for (int x = 0; x < frame.cols; ++x)126{127xl = ML(0,0)*x + ML(0,1)*y + ML(0,2);128yl = ML(1,0)*x + ML(1,1)*y + ML(1,2);129zl = ML(2,0)*x + ML(2,1)*y + ML(2,2);130xl /= zl; yl /= zl;131wl = float(idx - k1);132133xr = MR(0,0)*x + MR(0,1)*y + MR(0,2);134yr = MR(1,0)*x + MR(1,1)*y + MR(1,2);135zr = MR(2,0)*x + MR(2,1)*y + MR(2,2);136xr /= zr; yr /= zr;137wr = float(k2 - idx);138139mapx_(y,x) = (wr * xl + wl * xr) / (wl + wr);140mapy_(y,x) = (wr * yl + wl * yr) / (wl + wr);141}142}143144if (result.data == frame.data)145result = Mat(frame.size(), frame.type());146147remap(frame, result, mapx_, mapy_, INTER_LINEAR, BORDER_REPLICATE);148}149150#if defined(HAVE_OPENCV_CUDAWARPING)151void MoreAccurateMotionWobbleSuppressorGpu::suppress(int idx, const cuda::GpuMat &frame, cuda::GpuMat &result)152{153CV_Assert(motions_ && stabilizationMotions_);154155if (idx % period_ == 0)156{157result = frame;158return;159}160161int k1 = idx / period_ * period_;162int k2 = std::min(k1 + period_, frameCount_ - 1);163164Mat S1 = (*stabilizationMotions_)[idx];165166Mat ML = S1 * getMotion(k1, idx, *motions2_) * getMotion(k1, idx, *motions_).inv() * S1.inv();167Mat MR = S1 * getMotion(idx, k2, *motions2_).inv() * getMotion(idx, k2, *motions_) * S1.inv();168169cuda::calcWobbleSuppressionMaps(k1, idx, k2, frame.size(), ML, MR, mapx_, mapy_);170171if (result.data == frame.data)172result = cuda::GpuMat(frame.size(), frame.type());173174cuda::remap(frame, result, mapx_, mapy_, INTER_LINEAR, BORDER_REPLICATE);175}176177178void MoreAccurateMotionWobbleSuppressorGpu::suppress(int idx, const Mat &frame, Mat &result)179{180frameDevice_.upload(frame);181suppress(idx, frameDevice_, resultDevice_);182resultDevice_.download(result);183}184#endif185186} // namespace videostab187} // namespace cv188189190