1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
11 // For Open Source Computer Vision Library
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.
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
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.
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.
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.
45 #ifdef HAVE_CVCONFIG_H
49 #include "opencv2/ts/ts.hpp"
50 #include "opencv2/ts/gpu_perf.hpp"
52 #include "opencv2/core/core.hpp"
53 #include "opencv2/gpu/gpu.hpp"
54 #include "opencv2/highgui/highgui.hpp"
55 #include "opencv2/video/video.hpp"
56 #include "opencv2/legacy/legacy.hpp"
58 int main(int argc, char* argv[])
60 perf::printCudaInfo();
62 perf::Regression::Init("gpu_perf4au");
63 perf::TestBase::Init(argc, argv);
64 testing::InitGoogleTest(&argc, argv);
66 return RUN_ALL_TESTS();
69 //////////////////////////////////////////////////////////
72 DEF_PARAM_TEST_1(Image, std::string);
74 PERF_TEST_P(Image, HoughLinesP, testing::Values(std::string("im1_1280x800.jpg")))
78 std::string fileName = GetParam();
80 const float rho = 1.f;
81 const float theta = 1.f;
82 const int threshold = 40;
83 const int minLineLenght = 20;
84 const int maxLineGap = 5;
86 cv::Mat image = cv::imread(fileName, cv::IMREAD_GRAYSCALE);
90 cv::gpu::GpuMat d_image(image);
91 cv::gpu::GpuMat d_lines;
92 cv::gpu::HoughLinesBuf d_buf;
94 cv::gpu::HoughLinesP(d_image, d_lines, d_buf, rho, theta, minLineLenght, maxLineGap);
98 cv::gpu::HoughLinesP(d_image, d_lines, d_buf, rho, theta, minLineLenght, maxLineGap);
104 cv::Canny(image, mask, 50, 100);
106 std::vector<cv::Vec4i> lines;
107 cv::HoughLinesP(mask, lines, rho, theta, threshold, minLineLenght, maxLineGap);
111 cv::HoughLinesP(mask, lines, rho, theta, threshold, minLineLenght, maxLineGap);
118 //////////////////////////////////////////////////////////
119 // GoodFeaturesToTrack
121 DEF_PARAM_TEST(Image_Depth, std::string, perf::MatDepth);
123 PERF_TEST_P(Image_Depth, GoodFeaturesToTrack,
125 testing::Values(std::string("im1_1280x800.jpg")),
126 testing::Values(CV_8U, CV_16U)
131 const std::string fileName = std::tr1::get<0>(GetParam());
132 const int depth = std::tr1::get<1>(GetParam());
134 const int maxCorners = 5000;
135 const double qualityLevel = 0.05;
136 const int minDistance = 5;
137 const int blockSize = 3;
138 const bool useHarrisDetector = true;
139 const double k = 0.05;
141 cv::Mat src = cv::imread(fileName, cv::IMREAD_GRAYSCALE);
143 FAIL() << "Unable to load source image [" << fileName << "]";
146 src.convertTo(src, depth);
148 cv::Mat mask(src.size(), CV_8UC1, cv::Scalar::all(1));
149 mask(cv::Rect(0, 0, 100, 100)).setTo(cv::Scalar::all(0));
153 cv::gpu::GoodFeaturesToTrackDetector_GPU d_detector(maxCorners, qualityLevel, minDistance, blockSize, useHarrisDetector, k);
155 cv::gpu::GpuMat d_src(src);
156 cv::gpu::GpuMat d_mask(mask);
157 cv::gpu::GpuMat d_pts;
159 d_detector(d_src, d_pts, d_mask);
163 d_detector(d_src, d_pts, d_mask);
169 FAIL() << "Unsupported depth";
173 cv::goodFeaturesToTrack(src, pts, maxCorners, qualityLevel, minDistance, mask, blockSize, useHarrisDetector, k);
177 cv::goodFeaturesToTrack(src, pts, maxCorners, qualityLevel, minDistance, mask, blockSize, useHarrisDetector, k);
184 //////////////////////////////////////////////////////////
185 // OpticalFlowPyrLKSparse
187 typedef std::pair<std::string, std::string> string_pair;
189 DEF_PARAM_TEST(ImagePair_Depth_GraySource, string_pair, perf::MatDepth, bool);
191 PERF_TEST_P(ImagePair_Depth_GraySource, OpticalFlowPyrLKSparse,
193 testing::Values(string_pair("im1_1280x800.jpg", "im2_1280x800.jpg")),
194 testing::Values(CV_8U, CV_16U),
200 const string_pair fileNames = std::tr1::get<0>(GetParam());
201 const int depth = std::tr1::get<1>(GetParam());
202 const bool graySource = std::tr1::get<2>(GetParam());
205 const cv::Size winSize(15, 15);
206 const int maxLevel = 5;
207 const cv::TermCriteria criteria(cv::TermCriteria::COUNT + cv::TermCriteria::EPS, 30, 0.01);
209 // GoodFeaturesToTrack params
210 const int maxCorners = 5000;
211 const double qualityLevel = 0.05;
212 const int minDistance = 5;
213 const int blockSize = 3;
214 const bool useHarrisDetector = true;
215 const double k = 0.05;
217 cv::Mat src1 = cv::imread(fileNames.first, graySource ? cv::IMREAD_GRAYSCALE : cv::IMREAD_COLOR);
219 FAIL() << "Unable to load source image [" << fileNames.first << "]";
221 cv::Mat src2 = cv::imread(fileNames.second, graySource ? cv::IMREAD_GRAYSCALE : cv::IMREAD_COLOR);
223 FAIL() << "Unable to load source image [" << fileNames.second << "]";
229 cv::cvtColor(src1, gray_src, cv::COLOR_BGR2GRAY);
232 cv::goodFeaturesToTrack(gray_src, pts, maxCorners, qualityLevel, minDistance, cv::noArray(), blockSize, useHarrisDetector, k);
236 src1.convertTo(src1, depth);
237 src2.convertTo(src2, depth);
242 cv::gpu::GpuMat d_src1(src1);
243 cv::gpu::GpuMat d_src2(src2);
244 cv::gpu::GpuMat d_pts(pts.reshape(2, 1));
245 cv::gpu::GpuMat d_nextPts;
246 cv::gpu::GpuMat d_status;
248 cv::gpu::PyrLKOpticalFlow d_pyrLK;
249 d_pyrLK.winSize = winSize;
250 d_pyrLK.maxLevel = maxLevel;
251 d_pyrLK.iters = criteria.maxCount;
252 d_pyrLK.useInitialFlow = false;
254 d_pyrLK.sparse(d_src1, d_src2, d_pts, d_nextPts, d_status);
258 d_pyrLK.sparse(d_src1, d_src2, d_pts, d_nextPts, d_status);
264 FAIL() << "Unsupported depth";
269 cv::calcOpticalFlowPyrLK(src1, src2, pts, nextPts, status, cv::noArray(), winSize, maxLevel, criteria);
273 cv::calcOpticalFlowPyrLK(src1, src2, pts, nextPts, status, cv::noArray(), winSize, maxLevel, criteria);
280 //////////////////////////////////////////////////////////
281 // OpticalFlowFarneback
283 DEF_PARAM_TEST(ImagePair_Depth, string_pair, perf::MatDepth);
285 PERF_TEST_P(ImagePair_Depth, OpticalFlowFarneback,
287 testing::Values(string_pair("im1_1280x800.jpg", "im2_1280x800.jpg")),
288 testing::Values(CV_8U, CV_16U)
293 const string_pair fileNames = std::tr1::get<0>(GetParam());
294 const int depth = std::tr1::get<1>(GetParam());
296 const double pyrScale = 0.5;
297 const int numLevels = 6;
298 const int winSize = 7;
299 const int numIters = 15;
301 const double polySigma = 1.5;
302 const int flags = cv::OPTFLOW_USE_INITIAL_FLOW;
304 cv::Mat src1 = cv::imread(fileNames.first, cv::IMREAD_GRAYSCALE);
306 FAIL() << "Unable to load source image [" << fileNames.first << "]";
308 cv::Mat src2 = cv::imread(fileNames.second, cv::IMREAD_GRAYSCALE);
310 FAIL() << "Unable to load source image [" << fileNames.second << "]";
314 src1.convertTo(src1, depth);
315 src2.convertTo(src2, depth);
320 cv::gpu::GpuMat d_src1(src1);
321 cv::gpu::GpuMat d_src2(src2);
322 cv::gpu::GpuMat d_u(src1.size(), CV_32FC1, cv::Scalar::all(0));
323 cv::gpu::GpuMat d_v(src1.size(), CV_32FC1, cv::Scalar::all(0));
325 cv::gpu::FarnebackOpticalFlow d_farneback;
326 d_farneback.pyrScale = pyrScale;
327 d_farneback.numLevels = numLevels;
328 d_farneback.winSize = winSize;
329 d_farneback.numIters = numIters;
330 d_farneback.polyN = polyN;
331 d_farneback.polySigma = polySigma;
332 d_farneback.flags = flags;
334 d_farneback(d_src1, d_src2, d_u, d_v);
338 d_farneback(d_src1, d_src2, d_u, d_v);
344 FAIL() << "Unsupported depth";
346 cv::Mat flow(src1.size(), CV_32FC2, cv::Scalar::all(0));
348 cv::calcOpticalFlowFarneback(src1, src2, flow, pyrScale, numLevels, winSize, numIters, polyN, polySigma, flags);
352 cv::calcOpticalFlowFarneback(src1, src2, flow, pyrScale, numLevels, winSize, numIters, polyN, polySigma, flags);
359 //////////////////////////////////////////////////////////
362 void calcOpticalFlowBM(const cv::Mat& prev, const cv::Mat& curr,
363 cv::Size bSize, cv::Size shiftSize, cv::Size maxRange, int usePrevious,
364 cv::Mat& velx, cv::Mat& vely)
366 cv::Size sz((curr.cols - bSize.width + shiftSize.width)/shiftSize.width, (curr.rows - bSize.height + shiftSize.height)/shiftSize.height);
368 velx.create(sz, CV_32FC1);
369 vely.create(sz, CV_32FC1);
377 cvCalcOpticalFlowBM(&cvprev, &cvcurr, bSize, shiftSize, maxRange, usePrevious, &cvvelx, &cvvely);
380 DEF_PARAM_TEST(ImagePair_BlockSize_ShiftSize_MaxRange, string_pair, cv::Size, cv::Size, cv::Size);
382 PERF_TEST_P(ImagePair_BlockSize_ShiftSize_MaxRange, OpticalFlowBM,
384 testing::Values(string_pair("im1_1280x800.jpg", "im2_1280x800.jpg")),
385 testing::Values(cv::Size(16, 16)),
386 testing::Values(cv::Size(2, 2)),
387 testing::Values(cv::Size(16, 16))
392 const string_pair fileNames = std::tr1::get<0>(GetParam());
393 const cv::Size block_size = std::tr1::get<1>(GetParam());
394 const cv::Size shift_size = std::tr1::get<2>(GetParam());
395 const cv::Size max_range = std::tr1::get<3>(GetParam());
397 cv::Mat src1 = cv::imread(fileNames.first, cv::IMREAD_GRAYSCALE);
399 FAIL() << "Unable to load source image [" << fileNames.first << "]";
401 cv::Mat src2 = cv::imread(fileNames.second, cv::IMREAD_GRAYSCALE);
403 FAIL() << "Unable to load source image [" << fileNames.second << "]";
407 cv::gpu::GpuMat d_src1(src1);
408 cv::gpu::GpuMat d_src2(src2);
409 cv::gpu::GpuMat d_velx, d_vely, buf;
411 cv::gpu::calcOpticalFlowBM(d_src1, d_src2, block_size, shift_size, max_range, false, d_velx, d_vely, buf);
415 cv::gpu::calcOpticalFlowBM(d_src1, d_src2, block_size, shift_size, max_range, false, d_velx, d_vely, buf);
422 calcOpticalFlowBM(src1, src2, block_size, shift_size, max_range, false, velx, vely);
426 calcOpticalFlowBM(src1, src2, block_size, shift_size, max_range, false, velx, vely);
433 PERF_TEST_P(ImagePair_BlockSize_ShiftSize_MaxRange, FastOpticalFlowBM,
435 testing::Values(string_pair("im1_1280x800.jpg", "im2_1280x800.jpg")),
436 testing::Values(cv::Size(16, 16)),
437 testing::Values(cv::Size(1, 1)),
438 testing::Values(cv::Size(16, 16))
443 const string_pair fileNames = std::tr1::get<0>(GetParam());
444 const cv::Size block_size = std::tr1::get<1>(GetParam());
445 const cv::Size shift_size = std::tr1::get<2>(GetParam());
446 const cv::Size max_range = std::tr1::get<3>(GetParam());
448 cv::Mat src1 = cv::imread(fileNames.first, cv::IMREAD_GRAYSCALE);
450 FAIL() << "Unable to load source image [" << fileNames.first << "]";
452 cv::Mat src2 = cv::imread(fileNames.second, cv::IMREAD_GRAYSCALE);
454 FAIL() << "Unable to load source image [" << fileNames.second << "]";
458 cv::gpu::GpuMat d_src1(src1);
459 cv::gpu::GpuMat d_src2(src2);
460 cv::gpu::GpuMat d_velx, d_vely;
462 cv::gpu::FastOpticalFlowBM fastBM;
464 fastBM(d_src1, d_src2, d_velx, d_vely, max_range.width, block_size.width);
468 fastBM(d_src1, d_src2, d_velx, d_vely, max_range.width, block_size.width);
475 calcOpticalFlowBM(src1, src2, block_size, shift_size, max_range, false, velx, vely);
479 calcOpticalFlowBM(src1, src2, block_size, shift_size, max_range, false, velx, vely);