Path: blob/master/modules/videostab/src/optical_flow.cpp
16354 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/video.hpp"44#include "opencv2/videostab/optical_flow.hpp"45#include "opencv2/videostab/ring_buffer.hpp"4647#ifdef HAVE_OPENCV_CUDAARITHM48#include "opencv2/cudaarithm.hpp"49#endif5051namespace cv52{53namespace videostab54{5556void SparsePyrLkOptFlowEstimator::run(57InputArray frame0, InputArray frame1, InputArray points0, InputOutputArray points1,58OutputArray status, OutputArray errors)59{60calcOpticalFlowPyrLK(frame0, frame1, points0, points1, status, errors, winSize_, maxLevel_);61}626364#ifdef HAVE_OPENCV_CUDAOPTFLOW6566SparsePyrLkOptFlowEstimatorGpu::SparsePyrLkOptFlowEstimatorGpu()67{68CV_Assert(cuda::getCudaEnabledDeviceCount() > 0);69optFlowEstimator_ = cuda::SparsePyrLKOpticalFlow::create();70}717273void SparsePyrLkOptFlowEstimatorGpu::run(74InputArray frame0, InputArray frame1, InputArray points0, InputOutputArray points1,75OutputArray status, OutputArray errors)76{77frame0_.upload(frame0.getMat());78frame1_.upload(frame1.getMat());79points0_.upload(points0.getMat());8081if (errors.needed())82{83run(frame0_, frame1_, points0_, points1_, status_, errors_);84errors_.download(errors.getMatRef());85}86else87run(frame0_, frame1_, points0_, points1_, status_);8889points1_.download(points1.getMatRef());90status_.download(status.getMatRef());91}929394void SparsePyrLkOptFlowEstimatorGpu::run(95const cuda::GpuMat &frame0, const cuda::GpuMat &frame1, const cuda::GpuMat &points0,96cuda::GpuMat &points1, cuda::GpuMat &status, cuda::GpuMat &errors)97{98optFlowEstimator_->setWinSize(winSize_);99optFlowEstimator_->setMaxLevel(maxLevel_);100optFlowEstimator_->calc(frame0, frame1, points0, points1, status, errors);101}102103104void SparsePyrLkOptFlowEstimatorGpu::run(105const cuda::GpuMat &frame0, const cuda::GpuMat &frame1, const cuda::GpuMat &points0,106cuda::GpuMat &points1, cuda::GpuMat &status)107{108optFlowEstimator_->setWinSize(winSize_);109optFlowEstimator_->setMaxLevel(maxLevel_);110optFlowEstimator_->calc(frame0, frame1, points0, points1, status);111}112113114DensePyrLkOptFlowEstimatorGpu::DensePyrLkOptFlowEstimatorGpu()115{116CV_Assert(cuda::getCudaEnabledDeviceCount() > 0);117optFlowEstimator_ = cuda::DensePyrLKOpticalFlow::create();118}119120121void DensePyrLkOptFlowEstimatorGpu::run(122InputArray frame0, InputArray frame1, InputOutputArray flowX, InputOutputArray flowY,123OutputArray errors)124{125frame0_.upload(frame0.getMat());126frame1_.upload(frame1.getMat());127128optFlowEstimator_->setWinSize(winSize_);129optFlowEstimator_->setMaxLevel(maxLevel_);130131if (errors.needed())132{133CV_Error(Error::StsNotImplemented, "DensePyrLkOptFlowEstimatorGpu doesn't support errors calculation");134}135else136{137cuda::GpuMat flow;138optFlowEstimator_->calc(frame0_, frame1_, flow);139140cuda::GpuMat flows[2];141cuda::split(flow, flows);142143flowX_ = flows[0];144flowY_ = flows[1];145}146147flowX_.download(flowX.getMatRef());148flowY_.download(flowY.getMatRef());149}150151#endif // HAVE_OPENCV_CUDAOPTFLOW152153} // namespace videostab154} // namespace cv155156157