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.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
22 // * Redistribution's in binary form must reproduce the above copyright notice,
23 // this list of conditions and the following disclaimer in the documentation
24 // and/or other materials provided with the distribution.
26 // * The name of Intel Corporation may not be used to endorse or promote products
27 // derived from this software without specific prior written permission.
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
42 #include "test_precomp.hpp"
48 ///////////////////////////////////////////////////////////////////////////////////////////////////////
51 PARAM_TEST_CASE(HoughLines, cv::gpu::DeviceInfo, cv::Size, UseRoi)
53 static void generateLines(cv::Mat& img)
55 img.setTo(cv::Scalar::all(0));
57 cv::line(img, cv::Point(20, 0), cv::Point(20, img.rows), cv::Scalar::all(255));
58 cv::line(img, cv::Point(0, 50), cv::Point(img.cols, 50), cv::Scalar::all(255));
59 cv::line(img, cv::Point(0, 0), cv::Point(img.cols, img.rows), cv::Scalar::all(255));
60 cv::line(img, cv::Point(img.cols, 0), cv::Point(0, img.rows), cv::Scalar::all(255));
63 static void drawLines(cv::Mat& dst, const std::vector<cv::Vec2f>& lines)
65 dst.setTo(cv::Scalar::all(0));
67 for (size_t i = 0; i < lines.size(); ++i)
69 float rho = lines[i][0], theta = lines[i][1];
71 double a = std::cos(theta), b = std::sin(theta);
72 double x0 = a*rho, y0 = b*rho;
73 pt1.x = cvRound(x0 + 1000*(-b));
74 pt1.y = cvRound(y0 + 1000*(a));
75 pt2.x = cvRound(x0 - 1000*(-b));
76 pt2.y = cvRound(y0 - 1000*(a));
77 cv::line(dst, pt1, pt2, cv::Scalar::all(255));
82 TEST_P(HoughLines, Accuracy)
84 const cv::gpu::DeviceInfo devInfo = GET_PARAM(0);
85 cv::gpu::setDevice(devInfo.deviceID());
86 const cv::Size size = GET_PARAM(1);
87 const bool useRoi = GET_PARAM(2);
89 const float rho = 1.0f;
90 const float theta = 1.5f * CV_PI / 180.0f;
91 const int threshold = 100;
93 cv::Mat src(size, CV_8UC1);
96 cv::gpu::GpuMat d_lines;
97 cv::gpu::HoughLines(loadMat(src, useRoi), d_lines, rho, theta, threshold);
99 std::vector<cv::Vec2f> lines;
100 cv::gpu::HoughLinesDownload(d_lines, lines);
102 cv::Mat dst(size, CV_8UC1);
103 drawLines(dst, lines);
105 ASSERT_MAT_NEAR(src, dst, 0.0);
108 INSTANTIATE_TEST_CASE_P(GPU_ImgProc, HoughLines, testing::Combine(
113 ///////////////////////////////////////////////////////////////////////////////////////////////////////
116 PARAM_TEST_CASE(HoughCircles, cv::gpu::DeviceInfo, cv::Size, UseRoi)
118 static void drawCircles(cv::Mat& dst, const std::vector<cv::Vec3f>& circles, bool fill)
120 dst.setTo(cv::Scalar::all(0));
122 for (size_t i = 0; i < circles.size(); ++i)
123 cv::circle(dst, cv::Point2f(circles[i][0], circles[i][1]), (int)circles[i][2], cv::Scalar::all(255), fill ? -1 : 1);
127 TEST_P(HoughCircles, Accuracy)
129 const cv::gpu::DeviceInfo devInfo = GET_PARAM(0);
130 cv::gpu::setDevice(devInfo.deviceID());
131 const cv::Size size = GET_PARAM(1);
132 const bool useRoi = GET_PARAM(2);
134 const float dp = 2.0f;
135 const float minDist = 10.0f;
136 const int minRadius = 10;
137 const int maxRadius = 20;
138 const int cannyThreshold = 100;
139 const int votesThreshold = 20;
141 std::vector<cv::Vec3f> circles_gold(4);
142 circles_gold[0] = cv::Vec3i(20, 20, minRadius);
143 circles_gold[1] = cv::Vec3i(90, 87, minRadius + 3);
144 circles_gold[2] = cv::Vec3i(30, 70, minRadius + 8);
145 circles_gold[3] = cv::Vec3i(80, 10, maxRadius);
147 cv::Mat src(size, CV_8UC1);
148 drawCircles(src, circles_gold, true);
150 cv::gpu::GpuMat d_circles;
151 cv::gpu::HoughCircles(loadMat(src, useRoi), d_circles, CV_HOUGH_GRADIENT, dp, minDist, cannyThreshold, votesThreshold, minRadius, maxRadius);
153 std::vector<cv::Vec3f> circles;
154 cv::gpu::HoughCirclesDownload(d_circles, circles);
156 ASSERT_FALSE(circles.empty());
158 for (size_t i = 0; i < circles.size(); ++i)
160 cv::Vec3f cur = circles[i];
164 for (size_t j = 0; j < circles_gold.size(); ++j)
166 cv::Vec3f gold = circles_gold[j];
168 if (std::fabs(cur[0] - gold[0]) < minDist && std::fabs(cur[1] - gold[1]) < minDist && std::fabs(cur[2] - gold[2]) < minDist)
179 INSTANTIATE_TEST_CASE_P(GPU_ImgProc, HoughCircles, testing::Combine(
184 ///////////////////////////////////////////////////////////////////////////////////////////////////////
187 PARAM_TEST_CASE(GeneralizedHough, cv::gpu::DeviceInfo, UseRoi)
191 TEST_P(GeneralizedHough, POSITION)
193 const cv::gpu::DeviceInfo devInfo = GET_PARAM(0);
194 cv::gpu::setDevice(devInfo.deviceID());
195 const bool useRoi = GET_PARAM(1);
197 cv::Mat templ = readImage("../cv/shared/templ.png", cv::IMREAD_GRAYSCALE);
198 ASSERT_FALSE(templ.empty());
200 cv::Point templCenter(templ.cols / 2, templ.rows / 2);
202 const size_t gold_count = 3;
203 cv::Point pos_gold[gold_count];
204 pos_gold[0] = cv::Point(templCenter.x + 10, templCenter.y + 10);
205 pos_gold[1] = cv::Point(2 * templCenter.x + 40, templCenter.y + 10);
206 pos_gold[2] = cv::Point(2 * templCenter.x + 40, 2 * templCenter.y + 40);
208 cv::Mat image(templ.rows * 3, templ.cols * 3, CV_8UC1, cv::Scalar::all(0));
209 for (size_t i = 0; i < gold_count; ++i)
211 cv::Rect rec(pos_gold[i].x - templCenter.x, pos_gold[i].y - templCenter.y, templ.cols, templ.rows);
212 cv::Mat imageROI = image(rec);
213 templ.copyTo(imageROI);
216 cv::Ptr<cv::gpu::GeneralizedHough_GPU> hough = cv::gpu::GeneralizedHough_GPU::create(cv::GHT_POSITION);
217 hough->set("votesThreshold", 200);
219 hough->setTemplate(loadMat(templ, useRoi));
221 cv::gpu::GpuMat d_pos;
222 hough->detect(loadMat(image, useRoi), d_pos);
224 std::vector<cv::Vec4f> pos;
225 hough->download(d_pos, pos);
227 ASSERT_EQ(gold_count, pos.size());
229 for (size_t i = 0; i < gold_count; ++i)
231 cv::Point gold = pos_gold[i];
235 for (size_t j = 0; j < pos.size(); ++j)
237 cv::Point2f p(pos[j][0], pos[j][1]);
239 if (::fabs(p.x - gold.x) < 2 && ::fabs(p.y - gold.y) < 2)
250 INSTANTIATE_TEST_CASE_P(GPU_ImgProc, GeneralizedHough, testing::Combine(