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.
43 #include "perf_precomp.hpp"
46 using namespace testing;
49 //////////////////////////////////////////////////////////////////////
54 struct Vec4iComparator
56 bool operator()(const cv::Vec4i& a, const cv::Vec4i b) const
58 if (a[0] != b[0]) return a[0] < b[0];
59 else if(a[1] != b[1]) return a[1] < b[1];
60 else if(a[2] != b[2]) return a[2] < b[2];
61 else return a[3] < b[3];
64 struct Vec3fComparator
66 bool operator()(const cv::Vec3f& a, const cv::Vec3f b) const
68 if(a[0] != b[0]) return a[0] < b[0];
69 else if(a[1] != b[1]) return a[1] < b[1];
70 else return a[2] < b[2];
73 struct Vec2fComparator
75 bool operator()(const cv::Vec2f& a, const cv::Vec2f b) const
77 if(a[0] != b[0]) return a[0] < b[0];
78 else return a[1] < b[1];
83 PERF_TEST_P(Sz, HoughLines,
84 CUDA_TYPICAL_MAT_SIZES)
88 const cv::Size size = GetParam();
90 const float rho = 1.0f;
91 const float theta = static_cast<float>(CV_PI / 180.0);
92 const int threshold = 300;
94 cv::Mat src(size, CV_8UC1, cv::Scalar::all(0));
95 cv::line(src, cv::Point(0, 100), cv::Point(src.cols, 100), cv::Scalar::all(255), 1);
96 cv::line(src, cv::Point(0, 200), cv::Point(src.cols, 200), cv::Scalar::all(255), 1);
97 cv::line(src, cv::Point(0, 400), cv::Point(src.cols, 400), cv::Scalar::all(255), 1);
98 cv::line(src, cv::Point(100, 0), cv::Point(100, src.rows), cv::Scalar::all(255), 1);
99 cv::line(src, cv::Point(200, 0), cv::Point(200, src.rows), cv::Scalar::all(255), 1);
100 cv::line(src, cv::Point(400, 0), cv::Point(400, src.rows), cv::Scalar::all(255), 1);
104 const cv::cuda::GpuMat d_src(src);
105 cv::cuda::GpuMat d_lines;
107 cv::Ptr<cv::cuda::HoughLinesDetector> hough = cv::cuda::createHoughLinesDetector(rho, theta, threshold);
109 TEST_CYCLE() hough->detect(d_src, d_lines);
111 cv::Mat gpu_lines(d_lines.row(0));
112 cv::Vec2f* begin = gpu_lines.ptr<cv::Vec2f>(0);
113 cv::Vec2f* end = begin + gpu_lines.cols;
114 std::sort(begin, end, Vec2fComparator());
115 SANITY_CHECK(gpu_lines);
119 std::vector<cv::Vec2f> cpu_lines;
121 TEST_CYCLE() cv::HoughLines(src, cpu_lines, rho, theta, threshold);
123 SANITY_CHECK(cpu_lines);
127 //////////////////////////////////////////////////////////////////////
130 DEF_PARAM_TEST_1(Image, std::string);
132 PERF_TEST_P(Image, HoughLinesP,
133 testing::Values("cv/shared/pic5.png", "stitching/a1.png"))
137 const std::string fileName = getDataPath(GetParam());
139 const float rho = 1.0f;
140 const float theta = static_cast<float>(CV_PI / 180.0);
141 const int threshold = 100;
142 const int minLineLenght = 50;
143 const int maxLineGap = 5;
145 const cv::Mat image = cv::imread(fileName, cv::IMREAD_GRAYSCALE);
146 ASSERT_FALSE(image.empty());
149 cv::Canny(image, mask, 50, 100);
153 const cv::cuda::GpuMat d_mask(mask);
154 cv::cuda::GpuMat d_lines;
156 cv::Ptr<cv::cuda::HoughSegmentDetector> hough = cv::cuda::createHoughSegmentDetector(rho, theta, minLineLenght, maxLineGap);
158 TEST_CYCLE() hough->detect(d_mask, d_lines);
160 cv::Mat gpu_lines(d_lines);
161 cv::Vec4i* begin = gpu_lines.ptr<cv::Vec4i>();
162 cv::Vec4i* end = begin + gpu_lines.cols;
163 std::sort(begin, end, Vec4iComparator());
164 SANITY_CHECK(gpu_lines);
168 std::vector<cv::Vec4i> cpu_lines;
170 TEST_CYCLE() cv::HoughLinesP(mask, cpu_lines, rho, theta, threshold, minLineLenght, maxLineGap);
172 SANITY_CHECK(cpu_lines);
176 //////////////////////////////////////////////////////////////////////
179 DEF_PARAM_TEST(Sz_Dp_MinDist, cv::Size, float, float);
181 PERF_TEST_P(Sz_Dp_MinDist, HoughCircles,
182 Combine(CUDA_TYPICAL_MAT_SIZES,
183 Values(1.0f, 2.0f, 4.0f),
188 const cv::Size size = GET_PARAM(0);
189 const float dp = GET_PARAM(1);
190 const float minDist = GET_PARAM(2);
192 const int minRadius = 10;
193 const int maxRadius = 30;
194 const int cannyThreshold = 100;
195 const int votesThreshold = 15;
197 cv::Mat src(size, CV_8UC1, cv::Scalar::all(0));
198 cv::circle(src, cv::Point(100, 100), 20, cv::Scalar::all(255), -1);
199 cv::circle(src, cv::Point(200, 200), 25, cv::Scalar::all(255), -1);
200 cv::circle(src, cv::Point(200, 100), 25, cv::Scalar::all(255), -1);
204 const cv::cuda::GpuMat d_src(src);
205 cv::cuda::GpuMat d_circles;
207 cv::Ptr<cv::cuda::HoughCirclesDetector> houghCircles = cv::cuda::createHoughCirclesDetector(dp, minDist, cannyThreshold, votesThreshold, minRadius, maxRadius);
209 TEST_CYCLE() houghCircles->detect(d_src, d_circles);
211 cv::Mat gpu_circles(d_circles);
212 cv::Vec3f* begin = gpu_circles.ptr<cv::Vec3f>(0);
213 cv::Vec3f* end = begin + gpu_circles.cols;
214 std::sort(begin, end, Vec3fComparator());
215 SANITY_CHECK(gpu_circles);
219 std::vector<cv::Vec3f> cpu_circles;
221 TEST_CYCLE() cv::HoughCircles(src, cpu_circles, cv::HOUGH_GRADIENT, dp, minDist, cannyThreshold, votesThreshold, minRadius, maxRadius);
223 SANITY_CHECK(cpu_circles);
227 //////////////////////////////////////////////////////////////////////
230 PERF_TEST_P(Sz, GeneralizedHoughBallard, CUDA_TYPICAL_MAT_SIZES)
234 const cv::Size imageSize = GetParam();
236 const cv::Mat templ = readImage("cv/shared/templ.png", cv::IMREAD_GRAYSCALE);
237 ASSERT_FALSE(templ.empty());
239 cv::Mat image(imageSize, CV_8UC1, cv::Scalar::all(0));
240 templ.copyTo(image(cv::Rect(50, 50, templ.cols, templ.rows)));
243 cv::Canny(image, edges, 50, 100);
246 cv::Sobel(image, dx, CV_32F, 1, 0);
247 cv::Sobel(image, dy, CV_32F, 0, 1);
251 cv::Ptr<cv::GeneralizedHoughBallard> alg = cv::cuda::createGeneralizedHoughBallard();
253 const cv::cuda::GpuMat d_edges(edges);
254 const cv::cuda::GpuMat d_dx(dx);
255 const cv::cuda::GpuMat d_dy(dy);
256 cv::cuda::GpuMat positions;
258 alg->setTemplate(cv::cuda::GpuMat(templ));
260 TEST_CYCLE() alg->detect(d_edges, d_dx, d_dy, positions);
262 CUDA_SANITY_CHECK(positions);
266 cv::Ptr<cv::GeneralizedHoughBallard> alg = cv::createGeneralizedHoughBallard();
270 alg->setTemplate(templ);
272 TEST_CYCLE() alg->detect(edges, dx, dy, positions);
274 CPU_SANITY_CHECK(positions);
278 PERF_TEST_P(Sz, GeneralizedHoughGuil, CUDA_TYPICAL_MAT_SIZES)
282 const cv::Size imageSize = GetParam();
284 const cv::Mat templ = readImage("cv/shared/templ.png", cv::IMREAD_GRAYSCALE);
285 ASSERT_FALSE(templ.empty());
287 cv::Mat image(imageSize, CV_8UC1, cv::Scalar::all(0));
288 templ.copyTo(image(cv::Rect(50, 50, templ.cols, templ.rows)));
290 cv::RNG rng(123456789);
291 const int objCount = rng.uniform(5, 15);
292 for (int i = 0; i < objCount; ++i)
294 double scale = rng.uniform(0.7, 1.3);
295 bool rotate = 1 == rng.uniform(0, 2);
298 cv::resize(templ, obj, cv::Size(), scale, scale);
304 pos.x = rng.uniform(0, image.cols - obj.cols);
305 pos.y = rng.uniform(0, image.rows - obj.rows);
307 cv::Mat roi = image(cv::Rect(pos, obj.size()));
308 cv::add(roi, obj, roi);
312 cv::Canny(image, edges, 50, 100);
315 cv::Sobel(image, dx, CV_32F, 1, 0);
316 cv::Sobel(image, dy, CV_32F, 0, 1);
320 cv::Ptr<cv::GeneralizedHoughGuil> alg = cv::cuda::createGeneralizedHoughGuil();
321 alg->setMaxAngle(90.0);
322 alg->setAngleStep(2.0);
324 const cv::cuda::GpuMat d_edges(edges);
325 const cv::cuda::GpuMat d_dx(dx);
326 const cv::cuda::GpuMat d_dy(dy);
327 cv::cuda::GpuMat positions;
329 alg->setTemplate(cv::cuda::GpuMat(templ));
331 TEST_CYCLE() alg->detect(d_edges, d_dx, d_dy, positions);
333 CUDA_SANITY_CHECK(positions);
337 cv::Ptr<cv::GeneralizedHoughGuil> alg = cv::createGeneralizedHoughGuil();
338 alg->setMaxAngle(90.0);
339 alg->setAngleStep(2.0);
343 alg->setTemplate(templ);
345 TEST_CYCLE() alg->detect(edges, dx, dy, positions);
347 CPU_SANITY_CHECK(positions);