1 /*M/////////////////////////////////////////////////////////////////////////////////////// 2 // 3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. 4 // 5 // By downloading, copying, installing or using the software you agree to this license. 6 // If you do not agree to this license, do not download, install, 7 // copy or use the software. 8 // 9 // 10 // License Agreement 11 // For Open Source Computer Vision Library 12 // 13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved. 14 // Copyright (C) 2009, Willow Garage 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 documentation 25 // and/or other materials provided with the distribution. 26 // 27 // * The name of the copyright holders may not be used to endorse or promote products 28 // derived from this software without specific prior written permission. 29 // 30 // This software is provided by the copyright holders and contributors "as is" and 31 // any express or implied warranties, including, but not limited to, the implied 32 // 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 damages 35 // (including, but not limited to, procurement of substitute goods or services; 36 // loss of use, data, or profits; or business interruption) however caused 37 // and on any theory of liability, whether in contract, strict liability, 38 // or tort (including negligence or otherwise) arising in any way out of 39 // the use of this software, even if advised of the possibility of such damage. 40 // 41 //M*/ 42 43 #include "perf_precomp.hpp" 44 45 using namespace std; 46 using namespace testing; 47 using namespace perf; 48 49 typedef pair<string, string> pair_string; 50 51 DEF_PARAM_TEST_1(ImagePair, pair_string); 52 53 ////////////////////////////////////////////////////// 54 // BroxOpticalFlow 55 56 PERF_TEST_P(ImagePair, BroxOpticalFlow, 57 Values<pair_string>(make_pair("gpu/opticalflow/frame0.png", "gpu/opticalflow/frame1.png"))) 58 { 59 declare.time(300); 60 61 cv::Mat frame0 = readImage(GetParam().first, cv::IMREAD_GRAYSCALE); 62 ASSERT_FALSE(frame0.empty()); 63 64 cv::Mat frame1 = readImage(GetParam().second, cv::IMREAD_GRAYSCALE); 65 ASSERT_FALSE(frame1.empty()); 66 67 frame0.convertTo(frame0, CV_32FC1, 1.0 / 255.0); 68 frame1.convertTo(frame1, CV_32FC1, 1.0 / 255.0); 69 70 if (PERF_RUN_CUDA()) 71 { 72 const cv::cuda::GpuMat d_frame0(frame0); 73 const cv::cuda::GpuMat d_frame1(frame1); 74 cv::cuda::GpuMat flow; 75 76 cv::Ptr<cv::cuda::BroxOpticalFlow> d_alg = 77 cv::cuda::BroxOpticalFlow::create(0.197 /*alpha*/, 50.0 /*gamma*/, 0.8 /*scale_factor*/, 78 10 /*inner_iterations*/, 77 /*outer_iterations*/, 10 /*solver_iterations*/); 79 80 TEST_CYCLE() d_alg->calc(d_frame0, d_frame1, flow); 81 82 cv::cuda::GpuMat flows[2]; 83 cv::cuda::split(flow, flows); 84 85 cv::cuda::GpuMat u = flows[0]; 86 cv::cuda::GpuMat v = flows[1]; 87 88 CUDA_SANITY_CHECK(u, 1e-1); 89 CUDA_SANITY_CHECK(v, 1e-1); 90 } 91 else 92 { 93 FAIL_NO_CPU(); 94 } 95 } 96 97 ////////////////////////////////////////////////////// 98 // PyrLKOpticalFlowSparse 99 100 DEF_PARAM_TEST(ImagePair_Gray_NPts_WinSz_Levels_Iters, pair_string, bool, int, int, int, int); 101 102 PERF_TEST_P(ImagePair_Gray_NPts_WinSz_Levels_Iters, PyrLKOpticalFlowSparse, 103 Combine(Values<pair_string>(make_pair("gpu/opticalflow/frame0.png", "gpu/opticalflow/frame1.png")), 104 Bool(), 105 Values(8000), 106 Values(21), 107 Values(1, 3), 108 Values(1, 30))) 109 { 110 declare.time(20.0); 111 112 const pair_string imagePair = GET_PARAM(0); 113 const bool useGray = GET_PARAM(1); 114 const int points = GET_PARAM(2); 115 const int winSize = GET_PARAM(3); 116 const int levels = GET_PARAM(4); 117 const int iters = GET_PARAM(5); 118 119 const cv::Mat frame0 = readImage(imagePair.first, useGray ? cv::IMREAD_GRAYSCALE : cv::IMREAD_COLOR); 120 ASSERT_FALSE(frame0.empty()); 121 122 const cv::Mat frame1 = readImage(imagePair.second, useGray ? cv::IMREAD_GRAYSCALE : cv::IMREAD_COLOR); 123 ASSERT_FALSE(frame1.empty()); 124 125 cv::Mat gray_frame; 126 if (useGray) 127 gray_frame = frame0; 128 else 129 cv::cvtColor(frame0, gray_frame, cv::COLOR_BGR2GRAY); 130 131 cv::Mat pts; 132 cv::goodFeaturesToTrack(gray_frame, pts, points, 0.01, 0.0); 133 134 if (PERF_RUN_CUDA()) 135 { 136 const cv::cuda::GpuMat d_pts(pts.reshape(2, 1)); 137 138 cv::Ptr<cv::cuda::SparsePyrLKOpticalFlow> d_pyrLK = 139 cv::cuda::SparsePyrLKOpticalFlow::create(cv::Size(winSize, winSize), 140 levels - 1, 141 iters); 142 143 const cv::cuda::GpuMat d_frame0(frame0); 144 const cv::cuda::GpuMat d_frame1(frame1); 145 cv::cuda::GpuMat nextPts; 146 cv::cuda::GpuMat status; 147 148 TEST_CYCLE() d_pyrLK->calc(d_frame0, d_frame1, d_pts, nextPts, status); 149 150 CUDA_SANITY_CHECK(nextPts); 151 CUDA_SANITY_CHECK(status); 152 } 153 else 154 { 155 cv::Mat nextPts; 156 cv::Mat status; 157 158 TEST_CYCLE() 159 { 160 cv::calcOpticalFlowPyrLK(frame0, frame1, pts, nextPts, status, cv::noArray(), 161 cv::Size(winSize, winSize), levels - 1, 162 cv::TermCriteria(cv::TermCriteria::COUNT + cv::TermCriteria::EPS, iters, 0.01)); 163 } 164 165 CPU_SANITY_CHECK(nextPts); 166 CPU_SANITY_CHECK(status); 167 } 168 } 169 170 ////////////////////////////////////////////////////// 171 // PyrLKOpticalFlowDense 172 173 DEF_PARAM_TEST(ImagePair_WinSz_Levels_Iters, pair_string, int, int, int); 174 175 PERF_TEST_P(ImagePair_WinSz_Levels_Iters, PyrLKOpticalFlowDense, 176 Combine(Values<pair_string>(make_pair("gpu/opticalflow/frame0.png", "gpu/opticalflow/frame1.png")), 177 Values(3, 5, 7, 9, 13, 17, 21), 178 Values(1, 3), 179 Values(1, 10))) 180 { 181 declare.time(30); 182 183 const pair_string imagePair = GET_PARAM(0); 184 const int winSize = GET_PARAM(1); 185 const int levels = GET_PARAM(2); 186 const int iters = GET_PARAM(3); 187 188 const cv::Mat frame0 = readImage(imagePair.first, cv::IMREAD_GRAYSCALE); 189 ASSERT_FALSE(frame0.empty()); 190 191 const cv::Mat frame1 = readImage(imagePair.second, cv::IMREAD_GRAYSCALE); 192 ASSERT_FALSE(frame1.empty()); 193 194 if (PERF_RUN_CUDA()) 195 { 196 const cv::cuda::GpuMat d_frame0(frame0); 197 const cv::cuda::GpuMat d_frame1(frame1); 198 cv::cuda::GpuMat flow; 199 200 cv::Ptr<cv::cuda::DensePyrLKOpticalFlow> d_pyrLK = 201 cv::cuda::DensePyrLKOpticalFlow::create(cv::Size(winSize, winSize), 202 levels - 1, 203 iters); 204 205 TEST_CYCLE() d_pyrLK->calc(d_frame0, d_frame1, flow); 206 207 cv::cuda::GpuMat flows[2]; 208 cv::cuda::split(flow, flows); 209 210 cv::cuda::GpuMat u = flows[0]; 211 cv::cuda::GpuMat v = flows[1]; 212 213 CUDA_SANITY_CHECK(u); 214 CUDA_SANITY_CHECK(v); 215 } 216 else 217 { 218 FAIL_NO_CPU(); 219 } 220 } 221 222 ////////////////////////////////////////////////////// 223 // FarnebackOpticalFlow 224 225 PERF_TEST_P(ImagePair, FarnebackOpticalFlow, 226 Values<pair_string>(make_pair("gpu/opticalflow/frame0.png", "gpu/opticalflow/frame1.png"))) 227 { 228 declare.time(10); 229 230 const cv::Mat frame0 = readImage(GetParam().first, cv::IMREAD_GRAYSCALE); 231 ASSERT_FALSE(frame0.empty()); 232 233 const cv::Mat frame1 = readImage(GetParam().second, cv::IMREAD_GRAYSCALE); 234 ASSERT_FALSE(frame1.empty()); 235 236 const int numLevels = 5; 237 const double pyrScale = 0.5; 238 const int winSize = 13; 239 const int numIters = 10; 240 const int polyN = 5; 241 const double polySigma = 1.1; 242 const int flags = 0; 243 244 if (PERF_RUN_CUDA()) 245 { 246 const cv::cuda::GpuMat d_frame0(frame0); 247 const cv::cuda::GpuMat d_frame1(frame1); 248 cv::cuda::GpuMat flow; 249 250 cv::Ptr<cv::cuda::FarnebackOpticalFlow> d_farneback = 251 cv::cuda::FarnebackOpticalFlow::create(numLevels, pyrScale, false, winSize, 252 numIters, polyN, polySigma, flags); 253 254 TEST_CYCLE() d_farneback->calc(d_frame0, d_frame1, flow); 255 256 cv::cuda::GpuMat flows[2]; 257 cv::cuda::split(flow, flows); 258 259 cv::cuda::GpuMat u = flows[0]; 260 cv::cuda::GpuMat v = flows[1]; 261 262 CUDA_SANITY_CHECK(u, 1e-4); 263 CUDA_SANITY_CHECK(v, 1e-4); 264 } 265 else 266 { 267 cv::Mat flow; 268 269 TEST_CYCLE() cv::calcOpticalFlowFarneback(frame0, frame1, flow, pyrScale, numLevels, winSize, numIters, polyN, polySigma, flags); 270 271 CPU_SANITY_CHECK(flow); 272 } 273 } 274 275 ////////////////////////////////////////////////////// 276 // OpticalFlowDual_TVL1 277 278 PERF_TEST_P(ImagePair, OpticalFlowDual_TVL1, 279 Values<pair_string>(make_pair("gpu/opticalflow/frame0.png", "gpu/opticalflow/frame1.png"))) 280 { 281 declare.time(20); 282 283 const cv::Mat frame0 = readImage(GetParam().first, cv::IMREAD_GRAYSCALE); 284 ASSERT_FALSE(frame0.empty()); 285 286 const cv::Mat frame1 = readImage(GetParam().second, cv::IMREAD_GRAYSCALE); 287 ASSERT_FALSE(frame1.empty()); 288 289 if (PERF_RUN_CUDA()) 290 { 291 const cv::cuda::GpuMat d_frame0(frame0); 292 const cv::cuda::GpuMat d_frame1(frame1); 293 cv::cuda::GpuMat flow; 294 295 cv::Ptr<cv::cuda::OpticalFlowDual_TVL1> d_alg = 296 cv::cuda::OpticalFlowDual_TVL1::create(); 297 298 TEST_CYCLE() d_alg->calc(d_frame0, d_frame1, flow); 299 300 cv::cuda::GpuMat flows[2]; 301 cv::cuda::split(flow, flows); 302 303 cv::cuda::GpuMat u = flows[0]; 304 cv::cuda::GpuMat v = flows[1]; 305 306 CUDA_SANITY_CHECK(u, 1e-1); 307 CUDA_SANITY_CHECK(v, 1e-1); 308 } 309 else 310 { 311 cv::Mat flow; 312 313 cv::Ptr<cv::DualTVL1OpticalFlow> alg = cv::createOptFlow_DualTVL1(); 314 alg->setMedianFiltering(1); 315 alg->setInnerIterations(1); 316 alg->setOuterIterations(300); 317 TEST_CYCLE() alg->calc(frame0, frame1, flow); 318 319 CPU_SANITY_CHECK(flow); 320 } 321 } 322