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.
47 #include "opencv2/ts/ts.hpp"
48 #include "opencv2/ts/gpu_perf.hpp"
50 #include "opencv2/core/core.hpp"
51 #include "opencv2/gpu/gpu.hpp"
52 #include "opencv2/highgui/highgui.hpp"
53 #include "opencv2/video/video.hpp"
54 #include "opencv2/legacy/legacy.hpp"
56 static const char * impls[] = {
61 CV_PERF_TEST_MAIN_WITH_IMPLS(gpu_perf4au, impls, perf::printCudaInfo())
63 //////////////////////////////////////////////////////////
66 DEF_PARAM_TEST_1(Image, std::string);
68 PERF_TEST_P(Image, HoughLinesP, testing::Values(std::string("im1_1280x800.jpg")))
72 std::string fileName = GetParam();
74 const float rho = 1.f;
75 const float theta = 1.f;
76 const int threshold = 40;
77 const int minLineLenght = 20;
78 const int maxLineGap = 5;
80 cv::Mat image = cv::imread(fileName, cv::IMREAD_GRAYSCALE);
84 cv::gpu::GpuMat d_image(image);
85 cv::gpu::GpuMat d_lines;
86 cv::gpu::HoughLinesBuf d_buf;
88 cv::gpu::HoughLinesP(d_image, d_lines, d_buf, rho, theta, minLineLenght, maxLineGap);
92 cv::gpu::HoughLinesP(d_image, d_lines, d_buf, rho, theta, minLineLenght, maxLineGap);
98 cv::Canny(image, mask, 50, 100);
100 std::vector<cv::Vec4i> lines;
101 cv::HoughLinesP(mask, lines, rho, theta, threshold, minLineLenght, maxLineGap);
105 cv::HoughLinesP(mask, lines, rho, theta, threshold, minLineLenght, maxLineGap);
112 //////////////////////////////////////////////////////////
113 // GoodFeaturesToTrack
115 DEF_PARAM_TEST(Image_Depth, std::string, perf::MatDepth);
117 PERF_TEST_P(Image_Depth, GoodFeaturesToTrack,
119 testing::Values(std::string("im1_1280x800.jpg")),
120 testing::Values(CV_8U, CV_16U)
125 const std::string fileName = std::tr1::get<0>(GetParam());
126 const int depth = std::tr1::get<1>(GetParam());
128 const int maxCorners = 5000;
129 const double qualityLevel = 0.05;
130 const int minDistance = 5;
131 const int blockSize = 3;
132 const bool useHarrisDetector = true;
133 const double k = 0.05;
135 cv::Mat src = cv::imread(fileName, cv::IMREAD_GRAYSCALE);
137 FAIL() << "Unable to load source image [" << fileName << "]";
140 src.convertTo(src, depth);
142 cv::Mat mask(src.size(), CV_8UC1, cv::Scalar::all(1));
143 mask(cv::Rect(0, 0, 100, 100)).setTo(cv::Scalar::all(0));
147 cv::gpu::GoodFeaturesToTrackDetector_GPU d_detector(maxCorners, qualityLevel, minDistance, blockSize, useHarrisDetector, k);
149 cv::gpu::GpuMat d_src(src);
150 cv::gpu::GpuMat d_mask(mask);
151 cv::gpu::GpuMat d_pts;
153 d_detector(d_src, d_pts, d_mask);
157 d_detector(d_src, d_pts, d_mask);
163 FAIL() << "Unsupported depth";
167 cv::goodFeaturesToTrack(src, pts, maxCorners, qualityLevel, minDistance, mask, blockSize, useHarrisDetector, k);
171 cv::goodFeaturesToTrack(src, pts, maxCorners, qualityLevel, minDistance, mask, blockSize, useHarrisDetector, k);
178 //////////////////////////////////////////////////////////
179 // OpticalFlowPyrLKSparse
181 typedef std::pair<std::string, std::string> string_pair;
183 DEF_PARAM_TEST(ImagePair_Depth_GraySource, string_pair, perf::MatDepth, bool);
185 PERF_TEST_P(ImagePair_Depth_GraySource, OpticalFlowPyrLKSparse,
187 testing::Values(string_pair("im1_1280x800.jpg", "im2_1280x800.jpg")),
188 testing::Values(CV_8U, CV_16U),
194 const string_pair fileNames = std::tr1::get<0>(GetParam());
195 const int depth = std::tr1::get<1>(GetParam());
196 const bool graySource = std::tr1::get<2>(GetParam());
199 const cv::Size winSize(15, 15);
200 const int maxLevel = 5;
201 const cv::TermCriteria criteria(cv::TermCriteria::COUNT + cv::TermCriteria::EPS, 30, 0.01);
203 // GoodFeaturesToTrack params
204 const int maxCorners = 5000;
205 const double qualityLevel = 0.05;
206 const int minDistance = 5;
207 const int blockSize = 3;
208 const bool useHarrisDetector = true;
209 const double k = 0.05;
211 cv::Mat src1 = cv::imread(fileNames.first, graySource ? cv::IMREAD_GRAYSCALE : cv::IMREAD_COLOR);
213 FAIL() << "Unable to load source image [" << fileNames.first << "]";
215 cv::Mat src2 = cv::imread(fileNames.second, graySource ? cv::IMREAD_GRAYSCALE : cv::IMREAD_COLOR);
217 FAIL() << "Unable to load source image [" << fileNames.second << "]";
223 cv::cvtColor(src1, gray_src, cv::COLOR_BGR2GRAY);
226 cv::goodFeaturesToTrack(gray_src, pts, maxCorners, qualityLevel, minDistance, cv::noArray(), blockSize, useHarrisDetector, k);
230 src1.convertTo(src1, depth);
231 src2.convertTo(src2, depth);
236 cv::gpu::GpuMat d_src1(src1);
237 cv::gpu::GpuMat d_src2(src2);
238 cv::gpu::GpuMat d_pts(pts.reshape(2, 1));
239 cv::gpu::GpuMat d_nextPts;
240 cv::gpu::GpuMat d_status;
242 cv::gpu::PyrLKOpticalFlow d_pyrLK;
243 d_pyrLK.winSize = winSize;
244 d_pyrLK.maxLevel = maxLevel;
245 d_pyrLK.iters = criteria.maxCount;
246 d_pyrLK.useInitialFlow = false;
248 d_pyrLK.sparse(d_src1, d_src2, d_pts, d_nextPts, d_status);
252 d_pyrLK.sparse(d_src1, d_src2, d_pts, d_nextPts, d_status);
258 FAIL() << "Unsupported depth";
263 cv::calcOpticalFlowPyrLK(src1, src2, pts, nextPts, status, cv::noArray(), winSize, maxLevel, criteria);
267 cv::calcOpticalFlowPyrLK(src1, src2, pts, nextPts, status, cv::noArray(), winSize, maxLevel, criteria);
274 //////////////////////////////////////////////////////////
275 // OpticalFlowFarneback
277 DEF_PARAM_TEST(ImagePair_Depth, string_pair, perf::MatDepth);
279 PERF_TEST_P(ImagePair_Depth, OpticalFlowFarneback,
281 testing::Values(string_pair("im1_1280x800.jpg", "im2_1280x800.jpg")),
282 testing::Values(CV_8U, CV_16U)
287 const string_pair fileNames = std::tr1::get<0>(GetParam());
288 const int depth = std::tr1::get<1>(GetParam());
290 const double pyrScale = 0.5;
291 const int numLevels = 6;
292 const int winSize = 7;
293 const int numIters = 15;
295 const double polySigma = 1.5;
296 const int flags = cv::OPTFLOW_USE_INITIAL_FLOW;
298 cv::Mat src1 = cv::imread(fileNames.first, cv::IMREAD_GRAYSCALE);
300 FAIL() << "Unable to load source image [" << fileNames.first << "]";
302 cv::Mat src2 = cv::imread(fileNames.second, cv::IMREAD_GRAYSCALE);
304 FAIL() << "Unable to load source image [" << fileNames.second << "]";
308 src1.convertTo(src1, depth);
309 src2.convertTo(src2, depth);
314 cv::gpu::GpuMat d_src1(src1);
315 cv::gpu::GpuMat d_src2(src2);
316 cv::gpu::GpuMat d_u(src1.size(), CV_32FC1, cv::Scalar::all(0));
317 cv::gpu::GpuMat d_v(src1.size(), CV_32FC1, cv::Scalar::all(0));
319 cv::gpu::FarnebackOpticalFlow d_farneback;
320 d_farneback.pyrScale = pyrScale;
321 d_farneback.numLevels = numLevels;
322 d_farneback.winSize = winSize;
323 d_farneback.numIters = numIters;
324 d_farneback.polyN = polyN;
325 d_farneback.polySigma = polySigma;
326 d_farneback.flags = flags;
328 d_farneback(d_src1, d_src2, d_u, d_v);
332 d_farneback(d_src1, d_src2, d_u, d_v);
338 FAIL() << "Unsupported depth";
340 cv::Mat flow(src1.size(), CV_32FC2, cv::Scalar::all(0));
342 cv::calcOpticalFlowFarneback(src1, src2, flow, pyrScale, numLevels, winSize, numIters, polyN, polySigma, flags);
346 cv::calcOpticalFlowFarneback(src1, src2, flow, pyrScale, numLevels, winSize, numIters, polyN, polySigma, flags);
353 //////////////////////////////////////////////////////////
356 void calcOpticalFlowBM(const cv::Mat& prev, const cv::Mat& curr,
357 cv::Size bSize, cv::Size shiftSize, cv::Size maxRange, int usePrevious,
358 cv::Mat& velx, cv::Mat& vely)
360 cv::Size sz((curr.cols - bSize.width + shiftSize.width)/shiftSize.width, (curr.rows - bSize.height + shiftSize.height)/shiftSize.height);
362 velx.create(sz, CV_32FC1);
363 vely.create(sz, CV_32FC1);
371 cvCalcOpticalFlowBM(&cvprev, &cvcurr, bSize, shiftSize, maxRange, usePrevious, &cvvelx, &cvvely);
374 DEF_PARAM_TEST(ImagePair_BlockSize_ShiftSize_MaxRange, string_pair, cv::Size, cv::Size, cv::Size);
376 PERF_TEST_P(ImagePair_BlockSize_ShiftSize_MaxRange, OpticalFlowBM,
378 testing::Values(string_pair("im1_1280x800.jpg", "im2_1280x800.jpg")),
379 testing::Values(cv::Size(16, 16)),
380 testing::Values(cv::Size(2, 2)),
381 testing::Values(cv::Size(16, 16))
386 const string_pair fileNames = std::tr1::get<0>(GetParam());
387 const cv::Size block_size = std::tr1::get<1>(GetParam());
388 const cv::Size shift_size = std::tr1::get<2>(GetParam());
389 const cv::Size max_range = std::tr1::get<3>(GetParam());
391 cv::Mat src1 = cv::imread(fileNames.first, cv::IMREAD_GRAYSCALE);
393 FAIL() << "Unable to load source image [" << fileNames.first << "]";
395 cv::Mat src2 = cv::imread(fileNames.second, cv::IMREAD_GRAYSCALE);
397 FAIL() << "Unable to load source image [" << fileNames.second << "]";
401 cv::gpu::GpuMat d_src1(src1);
402 cv::gpu::GpuMat d_src2(src2);
403 cv::gpu::GpuMat d_velx, d_vely, buf;
405 cv::gpu::calcOpticalFlowBM(d_src1, d_src2, block_size, shift_size, max_range, false, d_velx, d_vely, buf);
409 cv::gpu::calcOpticalFlowBM(d_src1, d_src2, block_size, shift_size, max_range, false, d_velx, d_vely, buf);
416 calcOpticalFlowBM(src1, src2, block_size, shift_size, max_range, false, velx, vely);
420 calcOpticalFlowBM(src1, src2, block_size, shift_size, max_range, false, velx, vely);
427 PERF_TEST_P(ImagePair_BlockSize_ShiftSize_MaxRange, FastOpticalFlowBM,
429 testing::Values(string_pair("im1_1280x800.jpg", "im2_1280x800.jpg")),
430 testing::Values(cv::Size(16, 16)),
431 testing::Values(cv::Size(1, 1)),
432 testing::Values(cv::Size(16, 16))
437 const string_pair fileNames = std::tr1::get<0>(GetParam());
438 const cv::Size block_size = std::tr1::get<1>(GetParam());
439 const cv::Size shift_size = std::tr1::get<2>(GetParam());
440 const cv::Size max_range = std::tr1::get<3>(GetParam());
442 cv::Mat src1 = cv::imread(fileNames.first, cv::IMREAD_GRAYSCALE);
444 FAIL() << "Unable to load source image [" << fileNames.first << "]";
446 cv::Mat src2 = cv::imread(fileNames.second, cv::IMREAD_GRAYSCALE);
448 FAIL() << "Unable to load source image [" << fileNames.second << "]";
452 cv::gpu::GpuMat d_src1(src1);
453 cv::gpu::GpuMat d_src2(src2);
454 cv::gpu::GpuMat d_velx, d_vely;
456 cv::gpu::FastOpticalFlowBM fastBM;
458 fastBM(d_src1, d_src2, d_velx, d_vely, max_range.width, block_size.width);
462 fastBM(d_src1, d_src2, d_velx, d_vely, max_range.width, block_size.width);
469 calcOpticalFlowBM(src1, src2, block_size, shift_size, max_range, false, velx, vely);
473 calcOpticalFlowBM(src1, src2, block_size, shift_size, max_range, false, velx, vely);