added Generalized Hough implementation
[profile/ivi/opencv.git] / modules / gpu / test / test_hough.cpp
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 //                        Intel License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
15 //
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
18 //
19 //   * Redistribution's of source code must retain the above copyright notice,
20 //     this list of conditions and the following disclaimer.
21 //
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.
25 //
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.
28 //
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.
39 //
40 //M*/
41
42 #include "test_precomp.hpp"
43
44 #ifdef HAVE_CUDA
45
46 namespace {
47
48 ///////////////////////////////////////////////////////////////////////////////////////////////////////
49 // HoughLines
50
51 PARAM_TEST_CASE(HoughLines, cv::gpu::DeviceInfo, cv::Size, UseRoi)
52 {
53     static void generateLines(cv::Mat& img)
54     {
55         img.setTo(cv::Scalar::all(0));
56
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));
61     }
62
63     static void drawLines(cv::Mat& dst, const std::vector<cv::Vec2f>& lines)
64     {
65         dst.setTo(cv::Scalar::all(0));
66
67         for (size_t i = 0; i < lines.size(); ++i)
68         {
69             float rho = lines[i][0], theta = lines[i][1];
70             cv::Point pt1, pt2;
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));
78         }
79     }
80 };
81
82 TEST_P(HoughLines, Accuracy)
83 {
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);
88
89     const float rho = 1.0f;
90     const float theta = 1.5f * CV_PI / 180.0f;
91     const int threshold = 100;
92
93     cv::Mat src(size, CV_8UC1);
94     generateLines(src);
95
96     cv::gpu::GpuMat d_lines;
97     cv::gpu::HoughLines(loadMat(src, useRoi), d_lines, rho, theta, threshold);
98
99     std::vector<cv::Vec2f> lines;
100     cv::gpu::HoughLinesDownload(d_lines, lines);
101
102     cv::Mat dst(size, CV_8UC1);
103     drawLines(dst, lines);
104
105     ASSERT_MAT_NEAR(src, dst, 0.0);
106 }
107
108 INSTANTIATE_TEST_CASE_P(GPU_ImgProc, HoughLines, testing::Combine(
109     ALL_DEVICES,
110     DIFFERENT_SIZES,
111     WHOLE_SUBMAT));
112
113 ///////////////////////////////////////////////////////////////////////////////////////////////////////
114 // HoughCircles
115
116 PARAM_TEST_CASE(HoughCircles, cv::gpu::DeviceInfo, cv::Size, UseRoi)
117 {
118     static void drawCircles(cv::Mat& dst, const std::vector<cv::Vec3f>& circles, bool fill)
119     {
120         dst.setTo(cv::Scalar::all(0));
121
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);
124     }
125 };
126
127 TEST_P(HoughCircles, Accuracy)
128 {
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);
133
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;
140
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);
146
147     cv::Mat src(size, CV_8UC1);
148     drawCircles(src, circles_gold, true);
149
150     cv::gpu::GpuMat d_circles;
151     cv::gpu::HoughCircles(loadMat(src, useRoi), d_circles, CV_HOUGH_GRADIENT, dp, minDist, cannyThreshold, votesThreshold, minRadius, maxRadius);
152
153     std::vector<cv::Vec3f> circles;
154     cv::gpu::HoughCirclesDownload(d_circles, circles);
155
156     ASSERT_FALSE(circles.empty());
157
158     for (size_t i = 0; i < circles.size(); ++i)
159     {
160         cv::Vec3f cur = circles[i];
161
162         bool found = false;
163
164         for (size_t j = 0; j < circles_gold.size(); ++j)
165         {
166             cv::Vec3f gold = circles_gold[j];
167
168             if (std::fabs(cur[0] - gold[0]) < minDist && std::fabs(cur[1] - gold[1]) < minDist && std::fabs(cur[2] - gold[2]) < minDist)
169             {
170                 found = true;
171                 break;
172             }
173         }
174
175         ASSERT_TRUE(found);
176     }
177 }
178
179 INSTANTIATE_TEST_CASE_P(GPU_ImgProc, HoughCircles, testing::Combine(
180     ALL_DEVICES,
181     DIFFERENT_SIZES,
182     WHOLE_SUBMAT));
183
184 ///////////////////////////////////////////////////////////////////////////////////////////////////////
185 // GeneralizedHough
186
187 PARAM_TEST_CASE(GeneralizedHough, cv::gpu::DeviceInfo, UseRoi)
188 {
189 };
190
191 TEST_P(GeneralizedHough, POSITION)
192 {
193     const cv::gpu::DeviceInfo devInfo = GET_PARAM(0);
194     cv::gpu::setDevice(devInfo.deviceID());
195     const bool useRoi = GET_PARAM(1);
196
197     cv::Mat templ = readImage("../cv/shared/templ.png", cv::IMREAD_GRAYSCALE);
198     ASSERT_FALSE(templ.empty());
199
200     cv::Point templCenter(templ.cols / 2, templ.rows / 2);
201
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);
207
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)
210     {
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);
214     }
215
216     cv::Ptr<cv::gpu::GeneralizedHough_GPU> hough = cv::gpu::GeneralizedHough_GPU::create(cv::GHT_POSITION);
217     hough->set("votesThreshold", 200);
218
219     hough->setTemplate(loadMat(templ, useRoi));
220
221     cv::gpu::GpuMat d_pos;
222     hough->detect(loadMat(image, useRoi), d_pos);
223
224     std::vector<cv::Vec4f> pos;
225     hough->download(d_pos, pos);
226
227     ASSERT_EQ(gold_count, pos.size());
228
229     for (size_t i = 0; i < gold_count; ++i)
230     {
231         cv::Point gold = pos_gold[i];
232
233         bool found = false;
234
235         for (size_t j = 0; j < pos.size(); ++j)
236         {
237             cv::Point2f p(pos[j][0], pos[j][1]);
238
239             if (::fabs(p.x - gold.x) < 2 && ::fabs(p.y - gold.y) < 2)
240             {
241                 found = true;
242                 break;
243             }
244         }
245
246         ASSERT_TRUE(found);
247     }
248 }
249
250 INSTANTIATE_TEST_CASE_P(GPU_ImgProc, GeneralizedHough, testing::Combine(
251     ALL_DEVICES,
252     WHOLE_SUBMAT));
253
254 } // namespace
255
256 #endif // HAVE_CUDA