Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/imgproc/test/test_houghlines.cpp
16339 views
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
// License Agreement
11
// For Open Source Computer Vision Library
12
//
13
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15
// Copyright (C) 2014, Itseez, Inc, all rights reserved.
16
// Third party copyrights are property of their respective owners.
17
//
18
// Redistribution and use in source and binary forms, with or without modification,
19
// are permitted provided that the following conditions are met:
20
//
21
// * Redistribution's of source code must retain the above copyright notice,
22
// this list of conditions and the following disclaimer.
23
//
24
// * Redistribution's in binary form must reproduce the above copyright notice,
25
// this list of conditions and the following disclaimer in the documentation
26
// and/or other materials provided with the distribution.
27
//
28
// * The name of the copyright holders may not be used to endorse or promote products
29
// derived from this software without specific prior written permission.
30
//
31
// This software is provided by the copyright holders and contributors "as is" and
32
// any express or implied warranties, including, but not limited to, the implied
33
// warranties of merchantability and fitness for a particular purpose are disclaimed.
34
// In no event shall the Intel Corporation or contributors be liable for any direct,
35
// indirect, incidental, special, exemplary, or consequential damages
36
// (including, but not limited to, procurement of substitute goods or services;
37
// loss of use, data, or profits; or business interruption) however caused
38
// and on any theory of liability, whether in contract, strict liability,
39
// or tort (including negligence or otherwise) arising in any way out of
40
// the use of this software, even if advised of the possibility of such damage.
41
//
42
//M*/
43
44
#include "test_precomp.hpp"
45
46
//#define GENERATE_DATA // generate data in debug mode via CPU code path (without IPP / OpenCL and other accelerators)
47
48
namespace opencv_test { namespace {
49
50
template<typename T>
51
struct SimilarWith
52
{
53
T value;
54
float theta_eps;
55
float rho_eps;
56
SimilarWith<T>(T val, float e, float r_e): value(val), theta_eps(e), rho_eps(r_e) { };
57
bool operator()(const T& other);
58
};
59
60
template<>
61
bool SimilarWith<Vec2f>::operator()(const Vec2f& other)
62
{
63
return std::abs(other[0] - value[0]) < rho_eps && std::abs(other[1] - value[1]) < theta_eps;
64
}
65
66
template<>
67
bool SimilarWith<Vec3f>::operator()(const Vec3f& other)
68
{
69
return std::abs(other[0] - value[0]) < rho_eps && std::abs(other[1] - value[1]) < theta_eps;
70
}
71
72
template<>
73
bool SimilarWith<Vec4i>::operator()(const Vec4i& other)
74
{
75
return cv::norm(value, other) < theta_eps;
76
}
77
78
template <typename T>
79
int countMatIntersection(const Mat& expect, const Mat& actual, float eps, float rho_eps)
80
{
81
int count = 0;
82
if (!expect.empty() && !actual.empty())
83
{
84
for (MatConstIterator_<T> it=expect.begin<T>(); it!=expect.end<T>(); it++)
85
{
86
MatConstIterator_<T> f = std::find_if(actual.begin<T>(), actual.end<T>(), SimilarWith<T>(*it, eps, rho_eps));
87
if (f != actual.end<T>())
88
count++;
89
}
90
}
91
return count;
92
}
93
94
String getTestCaseName(String filename)
95
{
96
string temp(filename);
97
size_t pos = temp.find_first_of("\\/.");
98
while ( pos != string::npos ) {
99
temp.replace( pos, 1, "_" );
100
pos = temp.find_first_of("\\/.");
101
}
102
return String(temp);
103
}
104
105
class BaseHoughLineTest
106
{
107
public:
108
enum {STANDART = 0, PROBABILISTIC};
109
protected:
110
template<typename LinesType, typename LineType>
111
void run_test(int type, const char* xml_name);
112
113
string picture_name;
114
double rhoStep;
115
double thetaStep;
116
int threshold;
117
int minLineLength;
118
int maxGap;
119
};
120
121
typedef tuple<string, double, double, int> Image_RhoStep_ThetaStep_Threshold_t;
122
class StandartHoughLinesTest : public BaseHoughLineTest, public testing::TestWithParam<Image_RhoStep_ThetaStep_Threshold_t>
123
{
124
public:
125
StandartHoughLinesTest()
126
{
127
picture_name = get<0>(GetParam());
128
rhoStep = get<1>(GetParam());
129
thetaStep = get<2>(GetParam());
130
threshold = get<3>(GetParam());
131
minLineLength = 0;
132
maxGap = 0;
133
}
134
};
135
136
typedef tuple<string, double, double, int, int, int> Image_RhoStep_ThetaStep_Threshold_MinLine_MaxGap_t;
137
class ProbabilisticHoughLinesTest : public BaseHoughLineTest, public testing::TestWithParam<Image_RhoStep_ThetaStep_Threshold_MinLine_MaxGap_t>
138
{
139
public:
140
ProbabilisticHoughLinesTest()
141
{
142
picture_name = get<0>(GetParam());
143
rhoStep = get<1>(GetParam());
144
thetaStep = get<2>(GetParam());
145
threshold = get<3>(GetParam());
146
minLineLength = get<4>(GetParam());
147
maxGap = get<5>(GetParam());
148
}
149
};
150
151
typedef tuple<double, double, double, double> HoughLinesPointSetInput_t;
152
class HoughLinesPointSetTest : public testing::TestWithParam<HoughLinesPointSetInput_t>
153
{
154
protected:
155
void run_test();
156
double Rho;
157
double Theta;
158
double rhoMin, rhoMax, rhoStep;
159
double thetaMin, thetaMax, thetaStep;
160
public:
161
HoughLinesPointSetTest()
162
{
163
rhoMin = get<0>(GetParam());
164
rhoMax = get<1>(GetParam());
165
rhoStep = (rhoMax - rhoMin) / 360.0f;
166
thetaMin = get<2>(GetParam());
167
thetaMax = get<3>(GetParam());
168
thetaStep = CV_PI / 180.0f;
169
Rho = 320.00000;
170
Theta = 1.04719;
171
}
172
};
173
174
template<typename LinesType, typename LineType>
175
void BaseHoughLineTest::run_test(int type, const char* xml_name)
176
{
177
string filename = cvtest::TS::ptr()->get_data_path() + picture_name;
178
Mat src = imread(filename, IMREAD_GRAYSCALE);
179
ASSERT_FALSE(src.empty()) << "Invalid test image: " << filename;
180
181
string xml = string(cvtest::TS::ptr()->get_data_path()) + "imgproc/" + xml_name;
182
183
Mat dst;
184
Canny(src, dst, 100, 150, 3);
185
ASSERT_FALSE(dst.empty()) << "Failed Canny edge detector";
186
187
LinesType lines;
188
if (type == STANDART)
189
HoughLines(dst, lines, rhoStep, thetaStep, threshold, 0, 0);
190
else if (type == PROBABILISTIC)
191
HoughLinesP(dst, lines, rhoStep, thetaStep, threshold, minLineLength, maxGap);
192
193
String test_case_name = format("lines_%s_%.0f_%.2f_%d_%d_%d", picture_name.c_str(), rhoStep, thetaStep,
194
threshold, minLineLength, maxGap);
195
test_case_name = getTestCaseName(test_case_name);
196
197
#ifdef GENERATE_DATA
198
{
199
FileStorage fs(xml, FileStorage::READ);
200
ASSERT_TRUE(!fs.isOpened() || fs[test_case_name].empty());
201
}
202
{
203
FileStorage fs(xml, FileStorage::APPEND);
204
EXPECT_TRUE(fs.isOpened()) << "Cannot open sanity data file: " << xml;
205
fs << test_case_name << Mat(lines);
206
}
207
#else
208
FileStorage fs(xml, FileStorage::READ);
209
FileNode node = fs[test_case_name];
210
ASSERT_FALSE(node.empty()) << "Missing test data: " << test_case_name << std::endl << "XML: " << xml;
211
212
Mat exp_lines_;
213
read(fs[test_case_name], exp_lines_, Mat());
214
fs.release();
215
LinesType exp_lines;
216
exp_lines_.copyTo(exp_lines);
217
218
int count = -1;
219
if (type == STANDART)
220
count = countMatIntersection<LineType>(Mat(exp_lines), Mat(lines), (float) thetaStep + FLT_EPSILON, (float) rhoStep + FLT_EPSILON);
221
else if (type == PROBABILISTIC)
222
count = countMatIntersection<LineType>(Mat(exp_lines), Mat(lines), 1e-4f, 0.f);
223
224
#if defined HAVE_IPP && IPP_VERSION_X100 >= 810 && !IPP_DISABLE_HOUGH
225
EXPECT_LE(std::abs((double)count - Mat(exp_lines).total()), Mat(exp_lines).total() * 0.25)
226
<< "count=" << count << " expected=" << Mat(exp_lines).total();
227
#else
228
EXPECT_EQ(count, (int)Mat(exp_lines).total());
229
#endif
230
#endif // GENERATE_DATA
231
}
232
233
void HoughLinesPointSetTest::run_test(void)
234
{
235
Mat lines_f, lines_i;
236
vector<Point2f> pointf;
237
vector<Point2i> pointi;
238
vector<Vec3d> line_polar_f, line_polar_i;
239
const float Points[20][2] = {
240
{ 0.0f, 369.0f }, { 10.0f, 364.0f }, { 20.0f, 358.0f }, { 30.0f, 352.0f },
241
{ 40.0f, 346.0f }, { 50.0f, 341.0f }, { 60.0f, 335.0f }, { 70.0f, 329.0f },
242
{ 80.0f, 323.0f }, { 90.0f, 318.0f }, { 100.0f, 312.0f }, { 110.0f, 306.0f },
243
{ 120.0f, 300.0f }, { 130.0f, 295.0f }, { 140.0f, 289.0f }, { 150.0f, 284.0f },
244
{ 160.0f, 277.0f }, { 170.0f, 271.0f }, { 180.0f, 266.0f }, { 190.0f, 260.0f }
245
};
246
247
// Float
248
for (int i = 0; i < 20; i++)
249
{
250
pointf.push_back(Point2f(Points[i][0],Points[i][1]));
251
}
252
253
HoughLinesPointSet(pointf, lines_f, 20, 1,
254
rhoMin, rhoMax, rhoStep,
255
thetaMin, thetaMax, thetaStep);
256
257
lines_f.copyTo( line_polar_f );
258
259
// Integer
260
for( int i = 0; i < 20; i++ )
261
{
262
pointi.push_back( Point2i( (int)Points[i][0], (int)Points[i][1] ) );
263
}
264
265
HoughLinesPointSet( pointi, lines_i, 20, 1,
266
rhoMin, rhoMax, rhoStep,
267
thetaMin, thetaMax, thetaStep );
268
269
lines_i.copyTo( line_polar_i );
270
271
EXPECT_EQ((int)(line_polar_f.at(0).val[1] * 100000.0f), (int)(Rho * 100000.0f));
272
EXPECT_EQ((int)(line_polar_f.at(0).val[2] * 100000.0f), (int)(Theta * 100000.0f));
273
EXPECT_EQ((int)(line_polar_i.at(0).val[1] * 100000.0f), (int)(Rho * 100000.0f));
274
EXPECT_EQ((int)(line_polar_i.at(0).val[2] * 100000.0f), (int)(Theta * 100000.0f));
275
}
276
277
TEST_P(StandartHoughLinesTest, regression)
278
{
279
run_test<Mat, Vec2f>(STANDART, "HoughLines.xml");
280
}
281
282
TEST_P(ProbabilisticHoughLinesTest, regression)
283
{
284
run_test<Mat, Vec4i>(PROBABILISTIC, "HoughLinesP.xml");
285
}
286
287
TEST_P(StandartHoughLinesTest, regression_Vec2f)
288
{
289
run_test<std::vector<Vec2f>, Vec2f>(STANDART, "HoughLines2f.xml");
290
}
291
292
TEST_P(StandartHoughLinesTest, regression_Vec3f)
293
{
294
run_test<std::vector<Vec3f>, Vec3f>(STANDART, "HoughLines3f.xml");
295
}
296
297
TEST_P(HoughLinesPointSetTest, regression)
298
{
299
run_test();
300
}
301
302
INSTANTIATE_TEST_CASE_P( ImgProc, StandartHoughLinesTest, testing::Combine(testing::Values( "shared/pic5.png", "../stitching/a1.png" ),
303
testing::Values( 1, 10 ),
304
testing::Values( 0.05, 0.1 ),
305
testing::Values( 80, 150 )
306
));
307
308
INSTANTIATE_TEST_CASE_P( ImgProc, ProbabilisticHoughLinesTest, testing::Combine(testing::Values( "shared/pic5.png", "shared/pic1.png" ),
309
testing::Values( 5, 10 ),
310
testing::Values( 0.05, 0.1 ),
311
testing::Values( 75, 150 ),
312
testing::Values( 0, 10 ),
313
testing::Values( 0, 4 )
314
));
315
316
INSTANTIATE_TEST_CASE_P( Imgproc, HoughLinesPointSetTest, testing::Combine(testing::Values( 0.0f, 120.0f ),
317
testing::Values( 360.0f, 480.0f ),
318
testing::Values( 0.0f, (CV_PI / 18.0f) ),
319
testing::Values( (CV_PI / 2.0f), (CV_PI * 5.0f / 12.0f) )
320
));
321
322
}} // namespace
323
324