Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/imgproc/test/test_drawing.cpp
16354 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
// Third party copyrights are property of their respective owners.
16
//
17
// Redistribution and use in source and binary forms, with or without modification,
18
// are permitted provided that the following conditions are met:
19
//
20
// * Redistribution's of source code must retain the above copyright notice,
21
// this list of conditions and the following disclaimer.
22
//
23
// * Redistribution's in binary form must reproduce the above copyright notice,
24
// this list of conditions and the following disclaimer in the documentation
25
// and/or other materials provided with the distribution.
26
//
27
// * The name of the copyright holders may not be used to endorse or promote products
28
// derived from this software without specific prior written permission.
29
//
30
// This software is provided by the copyright holders and contributors "as is" and
31
// any express or implied warranties, including, but not limited to, the implied
32
// warranties of merchantability and fitness for a particular purpose are disclaimed.
33
// In no event shall the Intel Corporation or contributors be liable for any direct,
34
// indirect, incidental, special, exemplary, or consequential damages
35
// (including, but not limited to, procurement of substitute goods or services;
36
// loss of use, data, or profits; or business interruption) however caused
37
// and on any theory of liability, whether in contract, strict liability,
38
// or tort (including negligence or otherwise) arising in any way out of
39
// the use of this software, even if advised of the possibility of such damage.
40
//
41
//M*/
42
43
#include "test_precomp.hpp"
44
45
namespace opencv_test { namespace {
46
47
//#define DRAW_TEST_IMAGE
48
49
class CV_DrawingTest : public cvtest::BaseTest
50
{
51
public:
52
CV_DrawingTest(){}
53
protected:
54
void run( int );
55
virtual void draw( Mat& img ) = 0;
56
virtual int checkLineIterator( Mat& img) = 0;
57
};
58
59
void CV_DrawingTest::run( int )
60
{
61
Mat testImg, valImg;
62
const string fname = "../highgui/drawing/image.png";
63
string path = ts->get_data_path(), filename;
64
filename = path + fname;
65
66
draw( testImg );
67
68
valImg = imread( filename );
69
if( valImg.empty() )
70
{
71
//imwrite( filename, testImg );
72
ts->printf( ts->LOG, "test image can not be read");
73
#ifdef HAVE_PNG
74
ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA);
75
#else
76
ts->printf( ts->LOG, "PNG image support is not available");
77
ts->set_failed_test_info(cvtest::TS::OK);
78
#endif
79
return;
80
}
81
else
82
{
83
// image should match exactly
84
float err = (float)cvtest::norm( testImg, valImg, NORM_L1 );
85
float Eps = 1;
86
if( err > Eps)
87
{
88
ts->printf( ts->LOG, "NORM_L1 between testImg and valImg is equal %f (larger than %f)\n", err, Eps );
89
ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);
90
}
91
else
92
{
93
ts->set_failed_test_info(checkLineIterator( testImg ));
94
}
95
}
96
ts->set_failed_test_info(cvtest::TS::OK);
97
}
98
99
class CV_DrawingTest_CPP : public CV_DrawingTest
100
{
101
public:
102
CV_DrawingTest_CPP() {}
103
protected:
104
virtual void draw( Mat& img );
105
virtual int checkLineIterator( Mat& img);
106
};
107
108
void CV_DrawingTest_CPP::draw( Mat& img )
109
{
110
Size imgSize( 600, 400 );
111
img.create( imgSize, CV_8UC3 );
112
113
vector<Point> polyline(4);
114
polyline[0] = Point(0, 0);
115
polyline[1] = Point(imgSize.width, 0);
116
polyline[2] = Point(imgSize.width, imgSize.height);
117
polyline[3] = Point(0, imgSize.height);
118
const Point* pts = &polyline[0];
119
int n = (int)polyline.size();
120
fillPoly( img, &pts, &n, 1, Scalar::all(255) );
121
122
Point p1(1,1), p2(3,3);
123
if( clipLine(Rect(0,0,imgSize.width,imgSize.height), p1, p2) && clipLine(imgSize, p1, p2) )
124
circle( img, Point(300,100), 40, Scalar(0,0,255), 3 ); // draw
125
126
p2 = Point(3,imgSize.height+1000);
127
if( clipLine(Rect(0,0,imgSize.width,imgSize.height), p1, p2) && clipLine(imgSize, p1, p2) )
128
circle( img, Point(500,300), 50, cvColorToScalar(255,CV_8UC3), 5, 8, 1 ); // draw
129
130
p1 = Point(imgSize.width,1), p2 = Point(imgSize.width,3);
131
if( clipLine(Rect(0,0,imgSize.width,imgSize.height), p1, p2) && clipLine(imgSize, p1, p2) )
132
circle( img, Point(390,100), 10, Scalar(0,0,255), 3 ); // not draw
133
134
p1 = Point(imgSize.width-1,1), p2 = Point(imgSize.width,3);
135
if( clipLine(Rect(0,0,imgSize.width,imgSize.height), p1, p2) && clipLine(imgSize, p1, p2) )
136
ellipse( img, Point(390,100), Size(20,30), 60, 0, 220.0, Scalar(0,200,0), 4 ); //draw
137
138
ellipse( img, RotatedRect(Point(100,200),Size(200,100),160), Scalar(200,200,255), 5 );
139
140
polyline.clear();
141
ellipse2Poly( Point(430,180), Size(100,150), 30, 0, 150, 20, polyline );
142
pts = &polyline[0];
143
n = (int)polyline.size();
144
polylines( img, &pts, &n, 1, false, Scalar(0,0,150), 4, CV_AA );
145
n = 0;
146
for( vector<Point>::const_iterator it = polyline.begin(); n < (int)polyline.size()-1; ++it, n++ )
147
{
148
line( img, *it, *(it+1), Scalar(50,250,100));
149
}
150
151
polyline.clear();
152
ellipse2Poly( Point(500,300), Size(50,80), 0, 0, 180, 10, polyline );
153
pts = &polyline[0];
154
n = (int)polyline.size();
155
polylines( img, &pts, &n, 1, true, Scalar(100,200,100), 20 );
156
fillConvexPoly( img, pts, n, Scalar(0, 80, 0) );
157
158
polyline.resize(8);
159
// external rectengular
160
polyline[0] = Point(0, 0);
161
polyline[1] = Point(80, 0);
162
polyline[2] = Point(80, 80);
163
polyline[3] = Point(0, 80);
164
// internal rectangular
165
polyline[4] = Point(20, 20);
166
polyline[5] = Point(60, 20);
167
polyline[6] = Point(60, 60);
168
polyline[7] = Point(20, 60);
169
const Point* ppts[] = {&polyline[0], &polyline[0]+4};
170
int pn[] = {4, 4};
171
fillPoly( img, ppts, pn, 2, Scalar(100, 100, 0), 8, 0, Point(500, 20) );
172
173
rectangle( img, Point(0, 300), Point(50, 398), Scalar(0,0,255) );
174
175
string text1 = "OpenCV";
176
int baseline = 0, thickness = 3, fontFace = FONT_HERSHEY_SCRIPT_SIMPLEX;
177
float fontScale = 2;
178
Size textSize = getTextSize( text1, fontFace, fontScale, thickness, &baseline);
179
baseline += thickness;
180
Point textOrg((img.cols - textSize.width)/2, (img.rows + textSize.height)/2);
181
rectangle(img, textOrg + Point(0, baseline), textOrg + Point(textSize.width, -textSize.height), Scalar(0,0,255));
182
line(img, textOrg + Point(0, thickness), textOrg + Point(textSize.width, thickness), Scalar(0, 0, 255));
183
putText(img, text1, textOrg, fontFace, fontScale, Scalar(150,0,150), thickness, 8);
184
185
string text2 = "abcdefghijklmnopqrstuvwxyz1234567890";
186
Scalar color(200,0,0);
187
fontScale = 0.5, thickness = 1;
188
int dist = 5;
189
190
textSize = getTextSize( text2, FONT_HERSHEY_SIMPLEX, fontScale, thickness, &baseline);
191
textOrg = Point(5,5)+Point(0,textSize.height+dist);
192
putText(img, text2, textOrg, FONT_HERSHEY_SIMPLEX, fontScale, color, thickness, CV_AA);
193
194
fontScale = 1;
195
textSize = getTextSize( text2, FONT_HERSHEY_PLAIN, fontScale, thickness, &baseline);
196
textOrg += Point(0,textSize.height+dist);
197
putText(img, text2, textOrg, FONT_HERSHEY_PLAIN, fontScale, color, thickness, CV_AA);
198
199
fontScale = 0.5;
200
textSize = getTextSize( text2, FONT_HERSHEY_DUPLEX, fontScale, thickness, &baseline);
201
textOrg += Point(0,textSize.height+dist);
202
putText(img, text2, textOrg, FONT_HERSHEY_DUPLEX, fontScale, color, thickness, CV_AA);
203
204
textSize = getTextSize( text2, FONT_HERSHEY_COMPLEX, fontScale, thickness, &baseline);
205
textOrg += Point(0,textSize.height+dist);
206
putText(img, text2, textOrg, FONT_HERSHEY_COMPLEX, fontScale, color, thickness, CV_AA);
207
208
textSize = getTextSize( text2, FONT_HERSHEY_TRIPLEX, fontScale, thickness, &baseline);
209
textOrg += Point(0,textSize.height+dist);
210
putText(img, text2, textOrg, FONT_HERSHEY_TRIPLEX, fontScale, color, thickness, CV_AA);
211
212
fontScale = 1;
213
textSize = getTextSize( text2, FONT_HERSHEY_COMPLEX_SMALL, fontScale, thickness, &baseline);
214
textOrg += Point(0,180) + Point(0,textSize.height+dist);
215
putText(img, text2, textOrg, FONT_HERSHEY_COMPLEX_SMALL, fontScale, color, thickness, CV_AA);
216
217
textSize = getTextSize( text2, FONT_HERSHEY_SCRIPT_SIMPLEX, fontScale, thickness, &baseline);
218
textOrg += Point(0,textSize.height+dist);
219
putText(img, text2, textOrg, FONT_HERSHEY_SCRIPT_SIMPLEX, fontScale, color, thickness, CV_AA);
220
221
textSize = getTextSize( text2, FONT_HERSHEY_SCRIPT_COMPLEX, fontScale, thickness, &baseline);
222
textOrg += Point(0,textSize.height+dist);
223
putText(img, text2, textOrg, FONT_HERSHEY_SCRIPT_COMPLEX, fontScale, color, thickness, CV_AA);
224
225
dist = 15, fontScale = 0.5;
226
textSize = getTextSize( text2, FONT_ITALIC, fontScale, thickness, &baseline);
227
textOrg += Point(0,textSize.height+dist);
228
putText(img, text2, textOrg, FONT_ITALIC, fontScale, color, thickness, CV_AA);
229
}
230
231
int CV_DrawingTest_CPP::checkLineIterator( Mat& img )
232
{
233
LineIterator it( img, Point(0,300), Point(1000, 300) );
234
for(int i = 0; i < it.count; ++it, i++ )
235
{
236
Vec3b v = (Vec3b)(*(*it)) - img.at<Vec3b>(300,i);
237
float err = (float)cvtest::norm( v, NORM_L2 );
238
if( err != 0 )
239
{
240
ts->printf( ts->LOG, "LineIterator works incorrect" );
241
ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT);
242
}
243
}
244
ts->set_failed_test_info(cvtest::TS::OK);
245
return 0;
246
}
247
248
class CV_DrawingTest_C : public CV_DrawingTest
249
{
250
public:
251
CV_DrawingTest_C() {}
252
protected:
253
virtual void draw( Mat& img );
254
virtual int checkLineIterator( Mat& img);
255
};
256
257
void CV_DrawingTest_C::draw( Mat& _img )
258
{
259
CvSize imgSize = cvSize(600, 400);
260
_img.create( imgSize, CV_8UC3 );
261
CvMat img = cvMat(_img);
262
263
vector<CvPoint> polyline(4);
264
polyline[0] = cvPoint(0, 0);
265
polyline[1] = cvPoint(imgSize.width, 0);
266
polyline[2] = cvPoint(imgSize.width, imgSize.height);
267
polyline[3] = cvPoint(0, imgSize.height);
268
CvPoint* pts = &polyline[0];
269
int n = (int)polyline.size();
270
int actualSize = 0;
271
cvFillPoly( &img, &pts, &n, 1, cvScalar(255,255,255) );
272
273
CvPoint p1 = cvPoint(1,1), p2 = cvPoint(3,3);
274
if( cvClipLine(imgSize, &p1, &p2) )
275
cvCircle( &img, cvPoint(300,100), 40, cvScalar(0,0,255), 3 ); // draw
276
277
p1 = cvPoint(1,1), p2 = cvPoint(3,imgSize.height+1000);
278
if( cvClipLine(imgSize, &p1, &p2) )
279
cvCircle( &img, cvPoint(500,300), 50, cvScalar(255,0,0), 5, 8, 1 ); // draw
280
281
p1 = cvPoint(imgSize.width,1), p2 = cvPoint(imgSize.width,3);
282
if( cvClipLine(imgSize, &p1, &p2) )
283
cvCircle( &img, cvPoint(390,100), 10, cvScalar(0,0,255), 3 ); // not draw
284
285
p1 = cvPoint(imgSize.width-1,1), p2 = cvPoint(imgSize.width,3);
286
if( cvClipLine(imgSize, &p1, &p2) )
287
cvEllipse( &img, cvPoint(390,100), cvSize(20,30), 60, 0, 220.0, cvScalar(0,200,0), 4 ); //draw
288
289
CvBox2D box;
290
box.center.x = 100;
291
box.center.y = 200;
292
box.size.width = 200;
293
box.size.height = 100;
294
box.angle = 160;
295
cvEllipseBox( &img, box, cvScalar(200,200,255), 5 );
296
297
polyline.resize(9);
298
pts = &polyline[0];
299
n = (int)polyline.size();
300
actualSize = cvEllipse2Poly( cvPoint(430,180), cvSize(100,150), 30, 0, 150, &polyline[0], 20 );
301
CV_Assert(actualSize == n);
302
cvPolyLine( &img, &pts, &n, 1, false, cvScalar(0,0,150), 4, CV_AA );
303
n = 0;
304
for( vector<CvPoint>::const_iterator it = polyline.begin(); n < (int)polyline.size()-1; ++it, n++ )
305
{
306
cvLine( &img, *it, *(it+1), cvScalar(50,250,100) );
307
}
308
309
polyline.resize(19);
310
pts = &polyline[0];
311
n = (int)polyline.size();
312
actualSize = cvEllipse2Poly( cvPoint(500,300), cvSize(50,80), 0, 0, 180, &polyline[0], 10 );
313
CV_Assert(actualSize == n);
314
cvPolyLine( &img, &pts, &n, 1, true, cvScalar(100,200,100), 20 );
315
cvFillConvexPoly( &img, pts, n, cvScalar(0, 80, 0) );
316
317
polyline.resize(8);
318
// external rectengular
319
polyline[0] = cvPoint(500, 20);
320
polyline[1] = cvPoint(580, 20);
321
polyline[2] = cvPoint(580, 100);
322
polyline[3] = cvPoint(500, 100);
323
// internal rectangular
324
polyline[4] = cvPoint(520, 40);
325
polyline[5] = cvPoint(560, 40);
326
polyline[6] = cvPoint(560, 80);
327
polyline[7] = cvPoint(520, 80);
328
CvPoint* ppts[] = {&polyline[0], &polyline[0]+4};
329
int pn[] = {4, 4};
330
cvFillPoly( &img, ppts, pn, 2, cvScalar(100, 100, 0), 8, 0 );
331
332
cvRectangle( &img, cvPoint(0, 300), cvPoint(50, 398), cvScalar(0,0,255) );
333
334
string text1 = "OpenCV";
335
CvFont font;
336
cvInitFont( &font, FONT_HERSHEY_SCRIPT_SIMPLEX, 2, 2, 0, 3 );
337
int baseline = 0;
338
CvSize textSize = {0, 0};
339
cvGetTextSize( text1.c_str(), &font, &textSize, &baseline );
340
baseline += font.thickness;
341
CvPoint textOrg = cvPoint((imgSize.width - textSize.width)/2, (imgSize.height + textSize.height)/2);
342
cvRectangle( &img, cvPoint( textOrg.x, textOrg.y + baseline),
343
cvPoint(textOrg.x + textSize.width, textOrg.y - textSize.height), cvScalar(0,0,255));
344
cvLine( &img, cvPoint(textOrg.x, textOrg.y + font.thickness),
345
cvPoint(textOrg.x + textSize.width, textOrg.y + font.thickness), cvScalar(0, 0, 255));
346
cvPutText( &img, text1.c_str(), textOrg, &font, cvScalar(150,0,150) );
347
348
int dist = 5;
349
string text2 = "abcdefghijklmnopqrstuvwxyz1234567890";
350
CvScalar color = cvScalar(200,0,0);
351
cvInitFont( &font, FONT_HERSHEY_SIMPLEX, 0.5, 0.5, 0, 1, CV_AA );
352
cvGetTextSize( text2.c_str(), &font, &textSize, &baseline );
353
textOrg = cvPoint(5, 5+textSize.height+dist);
354
cvPutText(&img, text2.c_str(), textOrg, &font, color );
355
356
cvInitFont( &font, FONT_HERSHEY_PLAIN, 1, 1, 0, 1, CV_AA );
357
cvGetTextSize( text2.c_str(), &font, &textSize, &baseline );
358
textOrg = cvPoint(textOrg.x,textOrg.y+textSize.height+dist);
359
cvPutText(&img, text2.c_str(), textOrg, &font, color );
360
361
cvInitFont( &font, FONT_HERSHEY_DUPLEX, 0.5, 0.5, 0, 1, CV_AA );
362
cvGetTextSize( text2.c_str(), &font, &textSize, &baseline );
363
textOrg = cvPoint(textOrg.x,textOrg.y+textSize.height+dist);
364
cvPutText(&img, text2.c_str(), textOrg, &font, color );
365
366
cvInitFont( &font, FONT_HERSHEY_COMPLEX, 0.5, 0.5, 0, 1, CV_AA );
367
cvGetTextSize( text2.c_str(), &font, &textSize, &baseline );
368
textOrg = cvPoint(textOrg.x,textOrg.y+textSize.height+dist);
369
cvPutText(&img, text2.c_str(), textOrg, &font, color );
370
371
cvInitFont( &font, FONT_HERSHEY_TRIPLEX, 0.5, 0.5, 0, 1, CV_AA );
372
cvGetTextSize( text2.c_str(), &font, &textSize, &baseline );
373
textOrg = cvPoint(textOrg.x,textOrg.y+textSize.height+dist);
374
cvPutText(&img, text2.c_str(), textOrg, &font, color );
375
376
cvInitFont( &font, FONT_HERSHEY_COMPLEX_SMALL, 1, 1, 0, 1, CV_AA );
377
cvGetTextSize( text2.c_str(), &font, &textSize, &baseline );
378
textOrg = cvPoint(textOrg.x,textOrg.y+textSize.height+dist + 180);
379
cvPutText(&img, text2.c_str(), textOrg, &font, color );
380
381
cvInitFont( &font, FONT_HERSHEY_SCRIPT_SIMPLEX, 1, 1, 0, 1, CV_AA );
382
cvGetTextSize( text2.c_str(), &font, &textSize, &baseline );
383
textOrg = cvPoint(textOrg.x,textOrg.y+textSize.height+dist);
384
cvPutText(&img, text2.c_str(), textOrg, &font, color );
385
386
cvInitFont( &font, FONT_HERSHEY_SCRIPT_COMPLEX, 1, 1, 0, 1, CV_AA );
387
cvGetTextSize( text2.c_str(), &font, &textSize, &baseline );
388
textOrg = cvPoint(textOrg.x,textOrg.y+textSize.height+dist);
389
cvPutText(&img, text2.c_str(), textOrg, &font, color );
390
391
dist = 15;
392
cvInitFont( &font, FONT_ITALIC, 0.5, 0.5, 0, 1, CV_AA );
393
cvGetTextSize( text2.c_str(), &font, &textSize, &baseline );
394
textOrg = cvPoint(textOrg.x,textOrg.y+textSize.height+dist);
395
cvPutText(&img, text2.c_str(), textOrg, &font, color );
396
}
397
398
int CV_DrawingTest_C::checkLineIterator( Mat& _img )
399
{
400
CvLineIterator it;
401
CvMat img = cvMat(_img);
402
int count = cvInitLineIterator( &img, cvPoint(0,300), cvPoint(1000, 300), &it );
403
for(int i = 0; i < count; i++ )
404
{
405
Vec3b v = (Vec3b)(*(it.ptr)) - _img.at<Vec3b>(300,i);
406
float err = (float)cvtest::norm( v, NORM_L2 );
407
if( err != 0 )
408
{
409
ts->printf( ts->LOG, "CvLineIterator works incorrect" );
410
ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT);
411
}
412
CV_NEXT_LINE_POINT(it);
413
}
414
ts->set_failed_test_info(cvtest::TS::OK);
415
return 0;
416
}
417
418
class CV_DrawingTest_Far : public CV_DrawingTest_CPP
419
{
420
public:
421
CV_DrawingTest_Far() {}
422
protected:
423
virtual void draw(Mat& img);
424
};
425
426
void CV_DrawingTest_Far::draw(Mat& img)
427
{
428
Size imgSize(32768 + 600, 400);
429
img.create(imgSize, CV_8UC3);
430
431
vector<Point> polyline(4);
432
polyline[0] = Point(32768 + 0, 0);
433
polyline[1] = Point(imgSize.width, 0);
434
polyline[2] = Point(imgSize.width, imgSize.height);
435
polyline[3] = Point(32768 + 0, imgSize.height);
436
const Point* pts = &polyline[0];
437
int n = (int)polyline.size();
438
fillPoly(img, &pts, &n, 1, Scalar::all(255));
439
440
Point p1(32768 + 1, 1), p2(32768 + 3, 3);
441
if (clipLine(Rect(32768 + 0, 0, imgSize.width, imgSize.height), p1, p2) && clipLine(imgSize, p1, p2))
442
circle(img, Point(32768 + 300, 100), 40, Scalar(0, 0, 255), 3); // draw
443
444
p2 = Point(32768 + 3, imgSize.height + 1000);
445
if (clipLine(Rect(32768 + 0, 0, imgSize.width, imgSize.height), p1, p2) && clipLine(imgSize, p1, p2))
446
circle(img, Point(65536 + 500, 300), 50, cvColorToScalar(255, CV_8UC3), 5, 8, 1); // draw
447
448
p1 = Point(imgSize.width, 1), p2 = Point(imgSize.width, 3);
449
if (clipLine(Rect(32768 + 0, 0, imgSize.width, imgSize.height), p1, p2) && clipLine(imgSize, p1, p2))
450
circle(img, Point(32768 + 390, 100), 10, Scalar(0, 0, 255), 3); // not draw
451
452
p1 = Point(imgSize.width - 1, 1), p2 = Point(imgSize.width, 3);
453
if (clipLine(Rect(32768 + 0, 0, imgSize.width, imgSize.height), p1, p2) && clipLine(imgSize, p1, p2))
454
ellipse(img, Point(32768 + 390, 100), Size(20, 30), 60, 0, 220.0, Scalar(0, 200, 0), 4); //draw
455
456
ellipse(img, RotatedRect(Point(32768 + 100, 200), Size(200, 100), 160), Scalar(200, 200, 255), 5);
457
458
polyline.clear();
459
ellipse2Poly(Point(32768 + 430, 180), Size(100, 150), 30, 0, 150, 20, polyline);
460
pts = &polyline[0];
461
n = (int)polyline.size();
462
polylines(img, &pts, &n, 1, false, Scalar(0, 0, 150), 4, CV_AA);
463
n = 0;
464
for (vector<Point>::const_iterator it = polyline.begin(); n < (int)polyline.size() - 1; ++it, n++)
465
{
466
line(img, *it, *(it + 1), Scalar(50, 250, 100));
467
}
468
469
polyline.clear();
470
ellipse2Poly(Point(32768 + 500, 300), Size(50, 80), 0, 0, 180, 10, polyline);
471
pts = &polyline[0];
472
n = (int)polyline.size();
473
polylines(img, &pts, &n, 1, true, Scalar(100, 200, 100), 20);
474
fillConvexPoly(img, pts, n, Scalar(0, 80, 0));
475
476
polyline.resize(8);
477
// external rectengular
478
polyline[0] = Point(32768 + 0, 0);
479
polyline[1] = Point(32768 + 80, 0);
480
polyline[2] = Point(32768 + 80, 80);
481
polyline[3] = Point(32768 + 0, 80);
482
// internal rectangular
483
polyline[4] = Point(32768 + 20, 20);
484
polyline[5] = Point(32768 + 60, 20);
485
polyline[6] = Point(32768 + 60, 60);
486
polyline[7] = Point(32768 + 20, 60);
487
const Point* ppts[] = { &polyline[0], &polyline[0] + 4 };
488
int pn[] = { 4, 4 };
489
fillPoly(img, ppts, pn, 2, Scalar(100, 100, 0), 8, 0, Point(500, 20));
490
491
rectangle(img, Point(32768 + 0, 300), Point(32768 + 50, 398), Scalar(0, 0, 255));
492
493
string text1 = "OpenCV";
494
int baseline = 0, thickness = 3, fontFace = FONT_HERSHEY_SCRIPT_SIMPLEX;
495
float fontScale = 2;
496
Size textSize = getTextSize(text1, fontFace, fontScale, thickness, &baseline);
497
baseline += thickness;
498
Point textOrg((32768 + img.cols - textSize.width) / 2, (img.rows + textSize.height) / 2);
499
rectangle(img, textOrg + Point(0, baseline), textOrg + Point(textSize.width, -textSize.height), Scalar(0, 0, 255));
500
line(img, textOrg + Point(0, thickness), textOrg + Point(textSize.width, thickness), Scalar(0, 0, 255));
501
putText(img, text1, textOrg, fontFace, fontScale, Scalar(150, 0, 150), thickness, 8);
502
503
string text2 = "abcdefghijklmnopqrstuvwxyz1234567890";
504
Scalar color(200, 0, 0);
505
fontScale = 0.5, thickness = 1;
506
int dist = 5;
507
508
textSize = getTextSize(text2, FONT_HERSHEY_SIMPLEX, fontScale, thickness, &baseline);
509
textOrg = Point(32768 + 5, 5) + Point(0, textSize.height + dist);
510
putText(img, text2, textOrg, FONT_HERSHEY_SIMPLEX, fontScale, color, thickness, CV_AA);
511
512
fontScale = 1;
513
textSize = getTextSize(text2, FONT_HERSHEY_PLAIN, fontScale, thickness, &baseline);
514
textOrg += Point(0, textSize.height + dist);
515
putText(img, text2, textOrg, FONT_HERSHEY_PLAIN, fontScale, color, thickness, CV_AA);
516
517
fontScale = 0.5;
518
textSize = getTextSize(text2, FONT_HERSHEY_DUPLEX, fontScale, thickness, &baseline);
519
textOrg += Point(0, textSize.height + dist);
520
putText(img, text2, textOrg, FONT_HERSHEY_DUPLEX, fontScale, color, thickness, CV_AA);
521
522
textSize = getTextSize(text2, FONT_HERSHEY_COMPLEX, fontScale, thickness, &baseline);
523
textOrg += Point(0, textSize.height + dist);
524
putText(img, text2, textOrg, FONT_HERSHEY_COMPLEX, fontScale, color, thickness, CV_AA);
525
526
textSize = getTextSize(text2, FONT_HERSHEY_TRIPLEX, fontScale, thickness, &baseline);
527
textOrg += Point(0, textSize.height + dist);
528
putText(img, text2, textOrg, FONT_HERSHEY_TRIPLEX, fontScale, color, thickness, CV_AA);
529
530
fontScale = 1;
531
textSize = getTextSize(text2, FONT_HERSHEY_COMPLEX_SMALL, fontScale, thickness, &baseline);
532
textOrg += Point(0, 180) + Point(0, textSize.height + dist);
533
putText(img, text2, textOrg, FONT_HERSHEY_COMPLEX_SMALL, fontScale, color, thickness, CV_AA);
534
535
textSize = getTextSize(text2, FONT_HERSHEY_SCRIPT_SIMPLEX, fontScale, thickness, &baseline);
536
textOrg += Point(0, textSize.height + dist);
537
putText(img, text2, textOrg, FONT_HERSHEY_SCRIPT_SIMPLEX, fontScale, color, thickness, CV_AA);
538
539
textSize = getTextSize(text2, FONT_HERSHEY_SCRIPT_COMPLEX, fontScale, thickness, &baseline);
540
textOrg += Point(0, textSize.height + dist);
541
putText(img, text2, textOrg, FONT_HERSHEY_SCRIPT_COMPLEX, fontScale, color, thickness, CV_AA);
542
543
dist = 15, fontScale = 0.5;
544
textSize = getTextSize(text2, FONT_ITALIC, fontScale, thickness, &baseline);
545
textOrg += Point(0, textSize.height + dist);
546
putText(img, text2, textOrg, FONT_ITALIC, fontScale, color, thickness, CV_AA);
547
548
img = img(Rect(32768, 0, 600, 400)).clone();
549
}
550
551
TEST(Drawing, cpp_regression) { CV_DrawingTest_CPP test; test.safe_run(); }
552
TEST(Drawing, c_regression) { CV_DrawingTest_C test; test.safe_run(); }
553
TEST(Drawing, far_regression) { CV_DrawingTest_Far test; test.safe_run(); }
554
555
class CV_FillConvexPolyTest : public cvtest::BaseTest
556
{
557
public:
558
CV_FillConvexPolyTest() {}
559
~CV_FillConvexPolyTest() {}
560
protected:
561
void run(int)
562
{
563
vector<Point> line1;
564
vector<Point> line2;
565
566
line1.push_back(Point(1, 1));
567
line1.push_back(Point(5, 1));
568
line1.push_back(Point(5, 8));
569
line1.push_back(Point(1, 8));
570
571
line2.push_back(Point(2, 2));
572
line2.push_back(Point(10, 2));
573
line2.push_back(Point(10, 16));
574
line2.push_back(Point(2, 16));
575
576
Mat gray0(10,10,CV_8U, Scalar(0));
577
fillConvexPoly(gray0, line1, Scalar(255), 8, 0);
578
int nz1 = countNonZero(gray0);
579
580
fillConvexPoly(gray0, line2, Scalar(0), 8, 1);
581
int nz2 = countNonZero(gray0)/255;
582
583
CV_Assert( nz1 == 40 && nz2 == 0 );
584
}
585
};
586
587
TEST(Drawing, fillconvexpoly_clipping) { CV_FillConvexPolyTest test; test.safe_run(); }
588
589
class CV_DrawingTest_UTF8 : public cvtest::BaseTest
590
{
591
public:
592
CV_DrawingTest_UTF8() {}
593
~CV_DrawingTest_UTF8() {}
594
protected:
595
void run(int)
596
{
597
vector<string> lines;
598
lines.push_back("abcdefghijklmnopqrstuvwxyz1234567890");
599
// cyrillic letters small
600
lines.push_back("\xD0\xB0\xD0\xB1\xD0\xB2\xD0\xB3\xD0\xB4\xD0\xB5\xD1\x91\xD0\xB6\xD0\xB7"
601
"\xD0\xB8\xD0\xB9\xD0\xBA\xD0\xBB\xD0\xBC\xD0\xBD\xD0\xBE\xD0\xBF\xD1\x80"
602
"\xD1\x81\xD1\x82\xD1\x83\xD1\x84\xD1\x85\xD1\x86\xD1\x87\xD1\x88\xD1\x89"
603
"\xD1\x8A\xD1\x8B\xD1\x8C\xD1\x8D\xD1\x8E\xD1\x8F");
604
// cyrillic letters capital
605
lines.push_back("\xD0\x90\xD0\x91\xD0\x92\xD0\x93\xD0\x94\xD0\x95\xD0\x81\xD0\x96\xD0\x97"
606
"\xD0\x98\xD0\x99\xD0\x9A\xD0\x9B\xD0\x9C\xD0\x9D\xD0\x9E\xD0\x9F\xD0\xA0"
607
"\xD0\xA1\xD0\xA2\xD0\xA3\xD0\xA4\xD0\xA5\xD0\xA6\xD0\xA7\xD0\xA8\xD0\xA9"
608
"\xD0\xAA\xD0\xAB\xD0\xAC\xD0\xAD\xD0\xAE\xD0\xAF");
609
// bounds
610
lines.push_back("-\xD0\x80-\xD0\x8E-\xD0\x8F-");
611
lines.push_back("-\xD1\x90-\xD1\x91-\xD1\xBF-");
612
// bad utf8
613
lines.push_back("-\x81-\x82-\x83-");
614
lines.push_back("--\xF0--");
615
lines.push_back("-\xF0");
616
617
vector<int> fonts;
618
fonts.push_back(FONT_HERSHEY_SIMPLEX);
619
fonts.push_back(FONT_HERSHEY_PLAIN);
620
fonts.push_back(FONT_HERSHEY_DUPLEX);
621
fonts.push_back(FONT_HERSHEY_COMPLEX);
622
fonts.push_back(FONT_HERSHEY_TRIPLEX);
623
fonts.push_back(FONT_HERSHEY_COMPLEX_SMALL);
624
fonts.push_back(FONT_HERSHEY_SCRIPT_SIMPLEX);
625
fonts.push_back(FONT_HERSHEY_SCRIPT_COMPLEX);
626
627
vector<Mat> results;
628
Size bigSize(0, 0);
629
for (vector<int>::const_iterator font = fonts.begin(); font != fonts.end(); ++font)
630
{
631
for (int italic = 0; italic <= FONT_ITALIC; italic += FONT_ITALIC)
632
{
633
for (vector<string>::const_iterator line = lines.begin(); line != lines.end(); ++line)
634
{
635
const float fontScale = 1;
636
const int thickness = 1;
637
const Scalar color(20,20,20);
638
int baseline = 0;
639
640
Size textSize = getTextSize(*line, *font | italic, fontScale, thickness, &baseline);
641
Point textOrg(0, textSize.height + 2);
642
Mat img(textSize + Size(0, baseline), CV_8UC3, Scalar(255, 255, 255));
643
putText(img, *line, textOrg, *font | italic, fontScale, color, thickness, CV_AA);
644
645
results.push_back(img);
646
bigSize.width = max(bigSize.width, img.size().width);
647
bigSize.height += img.size().height + 1;
648
}
649
}
650
}
651
652
int shift = 0;
653
Mat result(bigSize, CV_8UC3, Scalar(100, 100, 100));
654
for (vector<Mat>::const_iterator img = results.begin(); img != results.end(); ++img)
655
{
656
Rect roi(Point(0, shift), img->size());
657
Mat sub(result, roi);
658
img->copyTo(sub);
659
shift += img->size().height + 1;
660
}
661
//imwrite("/tmp/all_fonts.png", result);
662
}
663
};
664
665
TEST(Drawing, utf8_support) { CV_DrawingTest_UTF8 test; test.safe_run(); }
666
667
668
TEST(Drawing, _914)
669
{
670
const int rows = 256;
671
const int cols = 256;
672
673
Mat img(rows, cols, CV_8UC1, Scalar(255));
674
675
line(img, Point(0, 10), Point(255, 10), Scalar(0), 2, 4);
676
line(img, Point(-5, 20), Point(260, 20), Scalar(0), 2, 4);
677
line(img, Point(10, 0), Point(10, 255), Scalar(0), 2, 4);
678
679
double x0 = 0.0/pow(2.0, -2.0);
680
double x1 = 255.0/pow(2.0, -2.0);
681
double y = 30.5/pow(2.0, -2.0);
682
683
line(img, Point(int(x0), int(y)), Point(int(x1), int(y)), Scalar(0), 2, 4, 2);
684
685
int pixelsDrawn = rows*cols - countNonZero(img);
686
ASSERT_EQ( (3*rows + cols)*3 - 3*9, pixelsDrawn);
687
}
688
689
TEST(Drawing, polylines_empty)
690
{
691
Mat img(100, 100, CV_8UC1, Scalar(0));
692
vector<Point> pts; // empty
693
polylines(img, pts, false, Scalar(255));
694
int cnt = countNonZero(img);
695
ASSERT_EQ(cnt, 0);
696
}
697
698
TEST(Drawing, polylines)
699
{
700
Mat img(100, 100, CV_8UC1, Scalar(0));
701
vector<Point> pts;
702
pts.push_back(Point(0, 0));
703
pts.push_back(Point(20, 0));
704
polylines(img, pts, false, Scalar(255));
705
int cnt = countNonZero(img);
706
ASSERT_EQ(cnt, 21);
707
}
708
709
TEST(Drawing, longline)
710
{
711
Mat mat = Mat::zeros(256, 256, CV_8UC1);
712
713
line(mat, cv::Point(34, 204), cv::Point(46400, 47400), cv::Scalar(255), 3);
714
EXPECT_EQ(310, cv::countNonZero(mat));
715
716
Point pt[6];
717
pt[0].x = 32;
718
pt[0].y = 204;
719
pt[1].x = 34;
720
pt[1].y = 202;
721
pt[2].x = 87;
722
pt[2].y = 255;
723
pt[3].x = 82;
724
pt[3].y = 255;
725
pt[4].x = 37;
726
pt[4].y = 210;
727
pt[5].x = 37;
728
pt[5].y = 209;
729
fillConvexPoly(mat, pt, 6, cv::Scalar(0));
730
731
EXPECT_EQ(0, cv::countNonZero(mat));
732
}
733
734
735
TEST(Drawing, putText_no_garbage)
736
{
737
Size sz(640, 480);
738
Mat mat = Mat::zeros(sz, CV_8UC1);
739
740
mat = Scalar::all(0);
741
putText(mat, "029", Point(10, 350), 0, 10, Scalar(128), 15);
742
743
EXPECT_EQ(0, cv::countNonZero(mat(Rect(0, 0, 10, sz.height))));
744
EXPECT_EQ(0, cv::countNonZero(mat(Rect(sz.width-10, 0, 10, sz.height))));
745
EXPECT_EQ(0, cv::countNonZero(mat(Rect(205, 0, 10, sz.height))));
746
EXPECT_EQ(0, cv::countNonZero(mat(Rect(405, 0, 10, sz.height))));
747
}
748
749
750
TEST(Drawing, line)
751
{
752
Mat mat = Mat::zeros(Size(100,100), CV_8UC1);
753
754
ASSERT_THROW(line(mat, Point(1,1),Point(99,99),Scalar(255),0), cv::Exception);
755
}
756
757
}} // namespace
758
759