Path: blob/master/modules/video/test/ocl/test_optflowpyrlk.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) 2010-2012, Institute Of Software Chinese Academy Of Science, all rights reserved.13// Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.14// Copyright (C) 2010-2012, Multicoreware, Inc., all rights reserved.15// Third party copyrights are property of their respective owners.16//17// Redistribution and use in source and binary forms, with or without modification,18// are permitted provided that the following conditions are met:19//20// * Redistribution's of source code must retain the above copyright notice,21// this list of conditions and the following disclaimer.22//23// * Redistribution's in binary form must reproduce the above copyright notice,24// this list of conditions and the following disclaimer in the documentation25// and/or other materials provided with the distribution.26//27// * The name of the copyright holders may not be used to endorse or promote products28// derived from this software without specific prior written permission.29//30// This software is provided by the copyright holders and contributors "as is" and31// any express or implied warranties, including, but not limited to, the implied32// warranties of merchantability and fitness for a particular purpose are disclaimed.33// In no event shall the Intel Corporation or contributors be liable for any direct,34// indirect, incidental, special, exemplary, or consequential damages35// (including, but not limited to, procurement of substitute goods or services;36// loss of use, data, or profits; or business interruption) however caused37// and on any theory of liability, whether in contract, strict liability,38// or tort (including negligence or otherwise) arising in any way out of39// the use of this software, even if advised of the possibility of such damage.40//41//M*/424344#include "../test_precomp.hpp"45#include "opencv2/ts/ocl_test.hpp"464748#ifdef HAVE_OPENCL4950namespace opencv_test { namespace ocl {5152/////////////////////////////////////////////////////////////////////////////////////////////////53// PyrLKOpticalFlow5455PARAM_TEST_CASE(PyrLKOpticalFlow, int, int)56{57Size winSize;58int maxLevel;59TermCriteria criteria;60int flags;61double minEigThreshold;6263virtual void SetUp()64{65winSize = Size(GET_PARAM(0), GET_PARAM(0));66maxLevel = GET_PARAM(1);67criteria = TermCriteria(TermCriteria::COUNT+TermCriteria::EPS, 30, 0.01);68flags = 0;69minEigThreshold = 1e-4f;70}71};7273OCL_TEST_P(PyrLKOpticalFlow, Mat)74{75static const int npoints = 1000;76static const float eps = 0.03f;77static const float erreps = 0.1f;7879cv::Mat frame0 = readImage("optflow/RubberWhale1.png", cv::IMREAD_GRAYSCALE);80ASSERT_FALSE(frame0.empty());81UMat umatFrame0; frame0.copyTo(umatFrame0);8283cv::Mat frame1 = readImage("optflow/RubberWhale2.png", cv::IMREAD_GRAYSCALE);84ASSERT_FALSE(frame1.empty());85UMat umatFrame1; frame1.copyTo(umatFrame1);8687// SKIP unstable tests88#ifdef __linux__89if (cvtest::skipUnstableTests && ocl::useOpenCL())90{91if (ocl::Device::getDefault().isIntel())92throw cvtest::SkipTestException("Skip unstable test");93}94#endif9596std::vector<cv::Point2f> pts;97cv::goodFeaturesToTrack(frame0, pts, npoints, 0.01, 0.0);9899std::vector<cv::Point2f> cpuNextPts;100std::vector<unsigned char> cpuStatusCPU;101std::vector<float> cpuErr;102OCL_OFF(cv::calcOpticalFlowPyrLK(frame0, frame1, pts, cpuNextPts, cpuStatusCPU, cpuErr, winSize, maxLevel, criteria, flags, minEigThreshold));103104UMat umatNextPts, umatStatus, umatErr;105OCL_ON(cv::calcOpticalFlowPyrLK(umatFrame0, umatFrame1, pts, umatNextPts, umatStatus, umatErr, winSize, maxLevel, criteria, flags, minEigThreshold));106std::vector<cv::Point2f> nextPts; umatNextPts.reshape(2, 1).copyTo(nextPts);107std::vector<unsigned char> status; umatStatus.reshape(1, 1).copyTo(status);108std::vector<float> err; umatErr.reshape(1, 1).copyTo(err);109110ASSERT_EQ(cpuNextPts.size(), nextPts.size());111ASSERT_EQ(cpuStatusCPU.size(), status.size());112113size_t mistmatch = 0;114size_t errmatch = 0;115116for (size_t i = 0; i < nextPts.size(); ++i)117{118if (status[i] != cpuStatusCPU[i])119{120++mistmatch;121continue;122}123124if (status[i])125{126cv::Point2i a = nextPts[i];127cv::Point2i b = cpuNextPts[i];128129bool eq = std::abs(a.x - b.x) < 1 && std::abs(a.y - b.y) < 1;130float errdiff = 0.0f;131132if (!eq || errdiff > 1e-1)133{134++mistmatch;135continue;136}137138eq = std::abs(cpuErr[i] - err[i]) <= (0.01 * std::max(1.0f, cpuErr[i]));139if(!eq)140++errmatch;141}142}143144double bad_ratio = static_cast<double>(mistmatch) / (nextPts.size());145double err_ratio = static_cast<double>(errmatch) / (nextPts.size());146147ASSERT_LE(bad_ratio, eps);148ASSERT_LE(err_ratio, erreps);149}150151OCL_INSTANTIATE_TEST_CASE_P(Video, PyrLKOpticalFlow,152Combine(153Values(11, 15, 21, 25),154Values(3, 5)155)156);157158159}} // namespace160#endif // HAVE_OPENCL161162163