Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/imgproc/test/test_lsd.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
#include "test_precomp.hpp"
5
6
namespace opencv_test { namespace {
7
8
const Size img_size(640, 480);
9
const int LSD_TEST_SEED = 0x134679;
10
const int EPOCHS = 20;
11
12
class LSDBase : public testing::Test
13
{
14
public:
15
LSDBase() { }
16
17
protected:
18
Mat test_image;
19
vector<Vec4f> lines;
20
RNG rng;
21
int passedtests;
22
23
void GenerateWhiteNoise(Mat& image);
24
void GenerateConstColor(Mat& image);
25
void GenerateLines(Mat& image, const unsigned int numLines);
26
void GenerateRotatedRect(Mat& image);
27
virtual void SetUp();
28
};
29
30
class Imgproc_LSD_ADV: public LSDBase
31
{
32
public:
33
Imgproc_LSD_ADV() { }
34
protected:
35
36
};
37
38
class Imgproc_LSD_STD: public LSDBase
39
{
40
public:
41
Imgproc_LSD_STD() { }
42
protected:
43
44
};
45
46
class Imgproc_LSD_NONE: public LSDBase
47
{
48
public:
49
Imgproc_LSD_NONE() { }
50
protected:
51
52
};
53
54
class Imgproc_LSD_Common : public LSDBase
55
{
56
public:
57
Imgproc_LSD_Common() { }
58
protected:
59
60
};
61
62
void LSDBase::GenerateWhiteNoise(Mat& image)
63
{
64
image = Mat(img_size, CV_8UC1);
65
rng.fill(image, RNG::UNIFORM, 0, 256);
66
}
67
68
void LSDBase::GenerateConstColor(Mat& image)
69
{
70
image = Mat(img_size, CV_8UC1, Scalar::all(rng.uniform(0, 256)));
71
}
72
73
void LSDBase::GenerateLines(Mat& image, const unsigned int numLines)
74
{
75
image = Mat(img_size, CV_8UC1, Scalar::all(rng.uniform(0, 128)));
76
77
for(unsigned int i = 0; i < numLines; ++i)
78
{
79
int y = rng.uniform(10, img_size.width - 10);
80
Point p1(y, 10);
81
Point p2(y, img_size.height - 10);
82
line(image, p1, p2, Scalar(255), 3);
83
}
84
}
85
86
void LSDBase::GenerateRotatedRect(Mat& image)
87
{
88
image = Mat::zeros(img_size, CV_8UC1);
89
90
Point center(rng.uniform(img_size.width/4, img_size.width*3/4),
91
rng.uniform(img_size.height/4, img_size.height*3/4));
92
Size rect_size(rng.uniform(img_size.width/8, img_size.width/6),
93
rng.uniform(img_size.height/8, img_size.height/6));
94
float angle = rng.uniform(0.f, 360.f);
95
96
Point2f vertices[4];
97
98
RotatedRect rRect = RotatedRect(center, rect_size, angle);
99
100
rRect.points(vertices);
101
for (int i = 0; i < 4; i++)
102
{
103
line(image, vertices[i], vertices[(i + 1) % 4], Scalar(255), 3);
104
}
105
}
106
107
void LSDBase::SetUp()
108
{
109
lines.clear();
110
test_image = Mat();
111
rng = RNG(LSD_TEST_SEED);
112
passedtests = 0;
113
}
114
115
116
TEST_F(Imgproc_LSD_ADV, whiteNoise)
117
{
118
for (int i = 0; i < EPOCHS; ++i)
119
{
120
GenerateWhiteNoise(test_image);
121
Ptr<LineSegmentDetector> detector = createLineSegmentDetector(LSD_REFINE_ADV);
122
detector->detect(test_image, lines);
123
124
if(40u >= lines.size()) ++passedtests;
125
}
126
ASSERT_EQ(EPOCHS, passedtests);
127
}
128
129
TEST_F(Imgproc_LSD_ADV, constColor)
130
{
131
for (int i = 0; i < EPOCHS; ++i)
132
{
133
GenerateConstColor(test_image);
134
Ptr<LineSegmentDetector> detector = createLineSegmentDetector(LSD_REFINE_ADV);
135
detector->detect(test_image, lines);
136
137
if(0u == lines.size()) ++passedtests;
138
}
139
ASSERT_EQ(EPOCHS, passedtests);
140
}
141
142
TEST_F(Imgproc_LSD_ADV, lines)
143
{
144
for (int i = 0; i < EPOCHS; ++i)
145
{
146
const unsigned int numOfLines = 1;
147
GenerateLines(test_image, numOfLines);
148
Ptr<LineSegmentDetector> detector = createLineSegmentDetector(LSD_REFINE_ADV);
149
detector->detect(test_image, lines);
150
151
if(numOfLines * 2 == lines.size()) ++passedtests; // * 2 because of Gibbs effect
152
}
153
ASSERT_EQ(EPOCHS, passedtests);
154
}
155
156
TEST_F(Imgproc_LSD_ADV, rotatedRect)
157
{
158
for (int i = 0; i < EPOCHS; ++i)
159
{
160
GenerateRotatedRect(test_image);
161
Ptr<LineSegmentDetector> detector = createLineSegmentDetector(LSD_REFINE_ADV);
162
detector->detect(test_image, lines);
163
164
if(2u <= lines.size()) ++passedtests;
165
}
166
ASSERT_EQ(EPOCHS, passedtests);
167
}
168
169
TEST_F(Imgproc_LSD_STD, whiteNoise)
170
{
171
for (int i = 0; i < EPOCHS; ++i)
172
{
173
GenerateWhiteNoise(test_image);
174
Ptr<LineSegmentDetector> detector = createLineSegmentDetector(LSD_REFINE_STD);
175
detector->detect(test_image, lines);
176
177
if(50u >= lines.size()) ++passedtests;
178
}
179
ASSERT_EQ(EPOCHS, passedtests);
180
}
181
182
TEST_F(Imgproc_LSD_STD, constColor)
183
{
184
for (int i = 0; i < EPOCHS; ++i)
185
{
186
GenerateConstColor(test_image);
187
Ptr<LineSegmentDetector> detector = createLineSegmentDetector(LSD_REFINE_STD);
188
detector->detect(test_image, lines);
189
190
if(0u == lines.size()) ++passedtests;
191
}
192
ASSERT_EQ(EPOCHS, passedtests);
193
}
194
195
TEST_F(Imgproc_LSD_STD, lines)
196
{
197
for (int i = 0; i < EPOCHS; ++i)
198
{
199
const unsigned int numOfLines = 1;
200
GenerateLines(test_image, numOfLines);
201
Ptr<LineSegmentDetector> detector = createLineSegmentDetector(LSD_REFINE_STD);
202
detector->detect(test_image, lines);
203
204
if(numOfLines * 2 == lines.size()) ++passedtests; // * 2 because of Gibbs effect
205
}
206
ASSERT_EQ(EPOCHS, passedtests);
207
}
208
209
TEST_F(Imgproc_LSD_STD, rotatedRect)
210
{
211
for (int i = 0; i < EPOCHS; ++i)
212
{
213
GenerateRotatedRect(test_image);
214
Ptr<LineSegmentDetector> detector = createLineSegmentDetector(LSD_REFINE_STD);
215
detector->detect(test_image, lines);
216
217
if(4u <= lines.size()) ++passedtests;
218
}
219
ASSERT_EQ(EPOCHS, passedtests);
220
}
221
222
TEST_F(Imgproc_LSD_NONE, whiteNoise)
223
{
224
for (int i = 0; i < EPOCHS; ++i)
225
{
226
GenerateWhiteNoise(test_image);
227
Ptr<LineSegmentDetector> detector = createLineSegmentDetector(LSD_REFINE_NONE);
228
detector->detect(test_image, lines);
229
230
if(50u >= lines.size()) ++passedtests;
231
}
232
ASSERT_EQ(EPOCHS, passedtests);
233
}
234
235
TEST_F(Imgproc_LSD_NONE, constColor)
236
{
237
for (int i = 0; i < EPOCHS; ++i)
238
{
239
GenerateConstColor(test_image);
240
Ptr<LineSegmentDetector> detector = createLineSegmentDetector(LSD_REFINE_NONE);
241
detector->detect(test_image, lines);
242
243
if(0u == lines.size()) ++passedtests;
244
}
245
ASSERT_EQ(EPOCHS, passedtests);
246
}
247
248
TEST_F(Imgproc_LSD_NONE, lines)
249
{
250
for (int i = 0; i < EPOCHS; ++i)
251
{
252
const unsigned int numOfLines = 1;
253
GenerateLines(test_image, numOfLines);
254
Ptr<LineSegmentDetector> detector = createLineSegmentDetector(LSD_REFINE_NONE);
255
detector->detect(test_image, lines);
256
257
if(numOfLines * 2 == lines.size()) ++passedtests; // * 2 because of Gibbs effect
258
}
259
ASSERT_EQ(EPOCHS, passedtests);
260
}
261
262
TEST_F(Imgproc_LSD_NONE, rotatedRect)
263
{
264
for (int i = 0; i < EPOCHS; ++i)
265
{
266
GenerateRotatedRect(test_image);
267
Ptr<LineSegmentDetector> detector = createLineSegmentDetector(LSD_REFINE_NONE);
268
detector->detect(test_image, lines);
269
270
if(8u <= lines.size()) ++passedtests;
271
}
272
ASSERT_EQ(EPOCHS, passedtests);
273
}
274
275
TEST_F(Imgproc_LSD_Common, supportsVec4iResult)
276
{
277
for (int i = 0; i < EPOCHS; ++i)
278
{
279
GenerateWhiteNoise(test_image);
280
Ptr<LineSegmentDetector> detector = createLineSegmentDetector(LSD_REFINE_STD);
281
detector->detect(test_image, lines);
282
283
std::vector<Vec4i> linesVec4i;
284
detector->detect(test_image, linesVec4i);
285
286
if (lines.size() == linesVec4i.size())
287
{
288
bool pass = true;
289
for (size_t lineIndex = 0; pass && lineIndex < lines.size(); lineIndex++)
290
{
291
for (int ch = 0; ch < 4; ch++)
292
{
293
if (cv::saturate_cast<int>(lines[lineIndex][ch]) != linesVec4i[lineIndex][ch])
294
{
295
pass = false;
296
break;
297
}
298
}
299
}
300
if (pass)
301
++passedtests;
302
}
303
}
304
ASSERT_EQ(EPOCHS, passedtests);
305
}
306
307
TEST_F(Imgproc_LSD_Common, drawSegmentsVec4f)
308
{
309
GenerateConstColor(test_image);
310
311
std::vector<Vec4f> linesVec4f;
312
RNG cr(0); // constant seed for deterministic test
313
for (int j = 0; j < 10; j++) {
314
linesVec4f.push_back(
315
Vec4f(float(cr) * test_image.cols, float(cr) * test_image.rows, float(cr) * test_image.cols, float(cr) * test_image.rows));
316
}
317
318
Mat actual = Mat::zeros(test_image.size(), CV_8UC3);
319
Mat expected = Mat::zeros(test_image.size(), CV_8UC3);
320
321
Ptr<LineSegmentDetector> detector = createLineSegmentDetector(LSD_REFINE_STD);
322
detector->drawSegments(actual, linesVec4f);
323
324
// something should be drawn
325
ASSERT_EQ(sum(actual == expected) != Scalar::all(0), true);
326
327
for (size_t lineIndex = 0; lineIndex < linesVec4f.size(); lineIndex++)
328
{
329
const Vec4f &v = linesVec4f[lineIndex];
330
const Point2f b(v[0], v[1]);
331
const Point2f e(v[2], v[3]);
332
line(expected, b, e, Scalar(0, 0, 255), 1);
333
}
334
335
ASSERT_EQ(sum(actual != expected) == Scalar::all(0), true);
336
}
337
338
TEST_F(Imgproc_LSD_Common, drawSegmentsVec4i)
339
{
340
GenerateConstColor(test_image);
341
342
std::vector<Vec4i> linesVec4i;
343
RNG cr(0); // constant seed for deterministic test
344
for (int j = 0; j < 10; j++) {
345
linesVec4i.push_back(
346
Vec4i(cr(test_image.cols), cr(test_image.rows), cr(test_image.cols), cr(test_image.rows)));
347
}
348
349
Mat actual = Mat::zeros(test_image.size(), CV_8UC3);
350
Mat expected = Mat::zeros(test_image.size(), CV_8UC3);
351
352
Ptr<LineSegmentDetector> detector = createLineSegmentDetector(LSD_REFINE_STD);
353
detector->drawSegments(actual, linesVec4i);
354
355
// something should be drawn
356
ASSERT_EQ(sum(actual == expected) != Scalar::all(0), true);
357
358
for (size_t lineIndex = 0; lineIndex < linesVec4i.size(); lineIndex++)
359
{
360
const Vec4f &v = linesVec4i[lineIndex];
361
const Point2f b(v[0], v[1]);
362
const Point2f e(v[2], v[3]);
363
line(expected, b, e, Scalar(0, 0, 255), 1);
364
}
365
366
ASSERT_EQ(sum(actual != expected) == Scalar::all(0), true);
367
}
368
369
TEST_F(Imgproc_LSD_Common, compareSegmentsVec4f)
370
{
371
GenerateConstColor(test_image);
372
Ptr<LineSegmentDetector> detector = createLineSegmentDetector(LSD_REFINE_STD);
373
374
std::vector<Vec4f> lines1, lines2;
375
lines1.push_back(Vec4f(0, 0, 100, 200));
376
lines2.push_back(Vec4f(0, 0, 100, 200));
377
int result1 = detector->compareSegments(test_image.size(), lines1, lines2);
378
379
ASSERT_EQ(result1, 0);
380
381
lines2.push_back(Vec4f(100, 100, 110, 100));
382
int result2 = detector->compareSegments(test_image.size(), lines1, lines2);
383
384
ASSERT_EQ(result2, 11);
385
}
386
387
TEST_F(Imgproc_LSD_Common, compareSegmentsVec4i)
388
{
389
GenerateConstColor(test_image);
390
Ptr<LineSegmentDetector> detector = createLineSegmentDetector(LSD_REFINE_STD);
391
392
std::vector<Vec4i> lines1, lines2;
393
lines1.push_back(Vec4i(0, 0, 100, 200));
394
lines2.push_back(Vec4i(0, 0, 100, 200));
395
int result1 = detector->compareSegments(test_image.size(), lines1, lines2);
396
397
ASSERT_EQ(result1, 0);
398
399
lines2.push_back(Vec4i(100, 100, 110, 100));
400
int result2 = detector->compareSegments(test_image.size(), lines1, lines2);
401
402
ASSERT_EQ(result2, 11);
403
}
404
405
}} // namespace
406
407