Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/imgproc/test/ocl/test_houghlines.cpp
16354 views
1
// This file is part of OpenCV project.
2
// It is subject to the license terms in the LICENSE file found in the top-level directory
3
// of this distribution and at http://opencv.org/license.html.
4
5
// Copyright (C) 2014, Itseez, Inc., all rights reserved.
6
// Third party copyrights are property of their respective owners.
7
8
#include "../test_precomp.hpp"
9
#include "opencv2/ts/ocl_test.hpp"
10
11
#ifdef HAVE_OPENCL
12
13
namespace opencv_test {
14
namespace ocl {
15
16
struct Vec2fComparator
17
{
18
bool operator()(const Vec2f& a, const Vec2f b) const
19
{
20
if(a[0] != b[0]) return a[0] < b[0];
21
else return a[1] < b[1];
22
}
23
};
24
25
/////////////////////////////// HoughLines ////////////////////////////////////
26
27
PARAM_TEST_CASE(HoughLines, double, double, int)
28
{
29
double rhoStep, thetaStep;
30
int threshold;
31
32
Size src_size;
33
Mat src, dst;
34
UMat usrc, udst;
35
36
virtual void SetUp()
37
{
38
rhoStep = GET_PARAM(0);
39
thetaStep = GET_PARAM(1);
40
threshold = GET_PARAM(2);
41
}
42
43
void generateTestData()
44
{
45
src_size = randomSize(500, 1920);
46
src.create(src_size, CV_8UC1);
47
src.setTo(Scalar::all(0));
48
line(src, Point(0, 100), Point(100, 100), Scalar::all(255), 1);
49
line(src, Point(0, 200), Point(100, 200), Scalar::all(255), 1);
50
line(src, Point(0, 400), Point(100, 400), Scalar::all(255), 1);
51
line(src, Point(100, 0), Point(100, 200), Scalar::all(255), 1);
52
line(src, Point(200, 0), Point(200, 200), Scalar::all(255), 1);
53
line(src, Point(400, 0), Point(400, 200), Scalar::all(255), 1);
54
55
src.copyTo(usrc);
56
}
57
58
void readRealTestData()
59
{
60
Mat img = readImage("shared/pic5.png", IMREAD_GRAYSCALE);
61
Canny(img, src, 100, 150, 3);
62
63
src.copyTo(usrc);
64
}
65
66
void Near(double eps = 0.)
67
{
68
EXPECT_EQ(dst.size(), udst.size());
69
70
if (dst.total() > 0)
71
{
72
Mat lines_cpu, lines_gpu;
73
dst.copyTo(lines_cpu);
74
udst.copyTo(lines_gpu);
75
76
std::sort(lines_cpu.begin<Vec2f>(), lines_cpu.end<Vec2f>(), Vec2fComparator());
77
std::sort(lines_gpu.begin<Vec2f>(), lines_gpu.end<Vec2f>(), Vec2fComparator());
78
79
EXPECT_LE(TestUtils::checkNorm2(lines_cpu, lines_gpu), eps);
80
}
81
}
82
};
83
84
OCL_TEST_P(HoughLines, RealImage)
85
{
86
readRealTestData();
87
88
OCL_OFF(cv::HoughLines(src, dst, rhoStep, thetaStep, threshold));
89
OCL_ON(cv::HoughLines(usrc, udst, rhoStep, thetaStep, threshold));
90
91
Near(1e-5);
92
}
93
94
OCL_TEST_P(HoughLines, GeneratedImage)
95
{
96
for (int j = 0; j < test_loop_times; j++)
97
{
98
generateTestData();
99
100
OCL_OFF(cv::HoughLines(src, dst, rhoStep, thetaStep, threshold));
101
OCL_ON(cv::HoughLines(usrc, udst, rhoStep, thetaStep, threshold));
102
103
Near(1e-5);
104
}
105
}
106
107
/////////////////////////////// HoughLinesP ///////////////////////////////////
108
109
PARAM_TEST_CASE(HoughLinesP, int, double, double)
110
{
111
double rhoStep, thetaStep, minLineLength, maxGap;
112
int threshold;
113
114
Size src_size;
115
Mat src, dst;
116
UMat usrc, udst;
117
118
virtual void SetUp()
119
{
120
rhoStep = 1.0;
121
thetaStep = CV_PI / 180;
122
threshold = GET_PARAM(0);
123
minLineLength = GET_PARAM(1);
124
maxGap = GET_PARAM(2);
125
}
126
127
void readRealTestData()
128
{
129
Mat img = readImage("shared/pic5.png", IMREAD_GRAYSCALE);
130
Canny(img, src, 50, 200, 3);
131
132
src.copyTo(usrc);
133
}
134
135
void Near(double eps = 0.)
136
{
137
Mat lines_gpu = udst.getMat(ACCESS_READ);
138
139
if (dst.total() > 0 && lines_gpu.total() > 0)
140
{
141
Mat result_cpu(src.size(), CV_8UC1, Scalar::all(0));
142
Mat result_gpu(src.size(), CV_8UC1, Scalar::all(0));
143
144
MatConstIterator_<Vec4i> it = dst.begin<Vec4i>(), end = dst.end<Vec4i>();
145
for ( ; it != end; it++)
146
{
147
Vec4i p = *it;
148
line(result_cpu, Point(p[0], p[1]), Point(p[2], p[3]), Scalar(255));
149
}
150
151
it = lines_gpu.begin<Vec4i>(), end = lines_gpu.end<Vec4i>();
152
for ( ; it != end; it++)
153
{
154
Vec4i p = *it;
155
line(result_gpu, Point(p[0], p[1]), Point(p[2], p[3]), Scalar(255));
156
}
157
158
EXPECT_MAT_SIMILAR(result_cpu, result_gpu, eps);
159
}
160
}
161
};
162
163
164
OCL_TEST_P(HoughLinesP, RealImage)
165
{
166
readRealTestData();
167
168
OCL_OFF(cv::HoughLinesP(src, dst, rhoStep, thetaStep, threshold, minLineLength, maxGap));
169
OCL_ON(cv::HoughLinesP(usrc, udst, rhoStep, thetaStep, threshold, minLineLength, maxGap));
170
171
Near(0.25);
172
}
173
174
OCL_INSTANTIATE_TEST_CASE_P(Imgproc, HoughLines, Combine(Values(1, 0.5), // rhoStep
175
Values(CV_PI / 180.0, CV_PI / 360.0), // thetaStep
176
Values(80, 150))); // threshold
177
178
OCL_INSTANTIATE_TEST_CASE_P(Imgproc, HoughLinesP, Combine(Values(100, 150), // threshold
179
Values(50, 100), // minLineLength
180
Values(5, 10))); // maxLineGap
181
182
} } // namespace opencv_test::ocl
183
184
#endif // HAVE_OPENCL
185