]> rtime.felk.cvut.cz Git - opencv.git/blob - opencv/tests/cv/src/adrawing.cpp
added test for drawing functions
[opencv.git] / opencv / tests / cv / src / adrawing.cpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////\r
2 //\r
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.\r
4 //\r
5 //  By downloading, copying, installing or using the software you agree to this license.\r
6 //  If you do not agree to this license, do not download, install,\r
7 //  copy or use the software.\r
8 //\r
9 //\r
10 //                        Intel License Agreement\r
11 //                For Open Source Computer Vision Library\r
12 //\r
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.\r
14 // Third party copyrights are property of their respective owners.\r
15 //\r
16 // Redistribution and use in source and binary forms, with or without modification,\r
17 // are permitted provided that the following conditions are met:\r
18 //\r
19 //   * Redistribution's of source code must retain the above copyright notice,\r
20 //     this list of conditions and the following disclaimer.\r
21 //\r
22 //   * Redistribution's in binary form must reproduce the above copyright notice,\r
23 //     this list of conditions and the following disclaimer in the documentation\r
24 //     and/or other materials provided with the distribution.\r
25 //\r
26 //   * The name of Intel Corporation may not be used to endorse or promote products\r
27 //     derived from this software without specific prior written permission.\r
28 //\r
29 // This software is provided by the copyright holders and contributors "as is" and\r
30 // any express or implied warranties, including, but not limited to, the implied\r
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.\r
32 // In no event shall the Intel Corporation or contributors be liable for any direct,\r
33 // indirect, incidental, special, exemplary, or consequential damages\r
34 // (including, but not limited to, procurement of substitute goods or services;\r
35 // loss of use, data, or profits; or business interruption) however caused\r
36 // and on any theory of liability, whether in contract, strict liability,\r
37 // or tort (including negligence or otherwise) arising in any way out of\r
38 // the use of this software, even if advised of the possibility of such damage.\r
39 //\r
40 //M*/\r
41 \r
42 #include "cvtest.h"\r
43 #include "highgui.h"\r
44 \r
45 using namespace cv;\r
46 \r
47 //#define DRAW_TEST_IMAGE\r
48 \r
49 class CV_DrawingTest : public CvTest\r
50 {\r
51 public:\r
52         CV_DrawingTest( const char* testName ) : CvTest( testName, "drawing funcs" ) {}\r
53 protected:\r
54     void run( int );\r
55         virtual void draw( Mat& img ) = 0;\r
56         virtual int checkLineIterator( Mat& img) = 0;\r
57 };\r
58 \r
59 void CV_DrawingTest::run( int )\r
60 {\r
61     int code = CvTS::OK;\r
62         Mat testImg, valImg;
63         const string name = "drawing/image.jpg";
64         string path = ts->get_data_path(), filename;
65         filename = path + name;
66         
67         draw( testImg );
68
69 #ifdef DRAW_TEST_IMAGE
70         imwrite( filename, testImg );
71 #else
72         valImg = imread( filename );
73         if( valImg.empty() )
74         {
75                 ts->printf( CvTS::LOG, "test image can not be read");
76                 code = CvTS::FAIL_INVALID_TEST_DATA;
77         }
78         else
79         {
80                 float err = (float)norm( testImg, valImg, CV_RELATIVE_L1 );
81                 float Eps = 0.9f;
82                 if( err > Eps)
83                 {
84                         ts->printf( CvTS::LOG, "CV_RELATIVE_L1 between testImg and valImg is equal %f (larger than %f)\n", err, Eps );\r
85                         code = CvTS::FAIL_BAD_ACCURACY;
86                 }
87                 else
88                 {
89                         code = checkLineIterator( testImg );
90                 }\r
91         }\r
92 #endif
93     ts->set_failed_test_info( code );\r
94 }\r
95 \r
96 class CV_DrawingTest_CPP : public CV_DrawingTest\r
97 {\r
98 public:\r
99     CV_DrawingTest_CPP() : CV_DrawingTest( "drawing_cpp" ) {}\r
100 protected:\r
101         virtual void draw( Mat& img );\r
102         virtual int checkLineIterator( Mat& img);\r
103 };\r
104 \r
105 void CV_DrawingTest_CPP::draw( Mat& img )\r
106 {\r
107         Size imgSize( 600, 400 );\r
108         img.create( imgSize, CV_8UC3 );\r
109 \r
110         vector<Point> polyline(4);\r
111         polyline[0] = Point(0, 0);\r
112         polyline[1] = Point(imgSize.width, 0);\r
113         polyline[2] = Point(imgSize.width, imgSize.height);\r
114         polyline[3] = Point(0, imgSize.height);\r
115         const Point* pts = &polyline[0];\r
116         int n = polyline.size();\r
117         fillPoly( img, &pts, &n, 1, Scalar::all(255) );\r
118 \r
119         Point p1(1,1), p2(3,3);\r
120         if( clipLine(Rect(0,0,imgSize.width,imgSize.height), p1, p2) && clipLine(imgSize, p1, p2) )\r
121                 circle( img, Point(300,100), 40, Scalar(0,0,255), 3 ); // draw\r
122 \r
123         p2 = Point(3,imgSize.height+1000);\r
124         if( clipLine(Rect(0,0,imgSize.width,imgSize.height), p1, p2) && clipLine(imgSize, p1, p2) )\r
125                 circle( img, Point(500,300), 50, Scalar(255,0,0), 5, 8, 1 ); // draw\r
126 \r
127         p1 = Point(imgSize.width,1), p2 = Point(imgSize.width,3);\r
128         if( clipLine(Rect(0,0,imgSize.width,imgSize.height), p1, p2) && clipLine(imgSize, p1, p2) )\r
129                 circle( img, Point(390,100), 10, Scalar(0,0,255), 3 ); // not draw\r
130 \r
131         p1 = Point(imgSize.width-1,1), p2 = Point(imgSize.width,3);\r
132         if( clipLine(Rect(0,0,imgSize.width,imgSize.height), p1, p2) && clipLine(imgSize, p1, p2) )\r
133                 ellipse( img, Point(390,100), Size(20,30), 60, 0, 220.0, Scalar(0,200,0), 4 ); //draw\r
134 \r
135         ellipse( img, RotatedRect(Point(100,200),Size(200,100),160), Scalar(200,200,255), 5 );\r
136 \r
137         polyline.clear();\r
138         ellipse2Poly( Point(430,180), Size(100,150), 30, 0, 150, 20, polyline );\r
139         pts = &polyline[0];\r
140         n = polyline.size();\r
141         polylines( img, &pts, &n, 1, false, Scalar(0,0,150), 4, CV_AA );\r
142         n = 0;\r
143         for( vector<Point>::const_iterator it = polyline.begin(); n < (int)polyline.size()-1; ++it, n++ )\r
144         {\r
145                 line( img, *it, *(it+1), Scalar(50,250,100));\r
146         }\r
147 \r
148         polyline.clear();\r
149         ellipse2Poly( Point(500,300), Size(50,80), 0, 0, 180, 10, polyline );\r
150         pts = &polyline[0];\r
151         n = polyline.size();\r
152         polylines( img, &pts, &n, 1, true, Scalar(100,200,100), 20 );\r
153         fillConvexPoly( img, pts, n, Scalar(0, 80, 0) );\r
154 \r
155         polyline.resize(8);\r
156         // external rectengular\r
157         polyline[0] = Point(0, 0);\r
158         polyline[1] = Point(80, 0);\r
159         polyline[2] = Point(80, 80);\r
160         polyline[3] = Point(0, 80);\r
161         // internal rectangular\r
162         polyline[4] = Point(20, 20);\r
163         polyline[5] = Point(60, 20);\r
164         polyline[6] = Point(60, 60);\r
165         polyline[7] = Point(20, 60);\r
166         const Point* ppts[] = {&polyline[0], &polyline[0]+4};\r
167         int pn[] = {4, 4};\r
168         fillPoly( img, ppts, pn, 2, Scalar(100, 100, 0), 8, 0, Point(500, 20) );\r
169 \r
170         rectangle( img, Point(0, 300), Point(50, 398), Scalar(0,0,255) );\r
171 \r
172         string text1 = "OpenCV";\r
173         int baseline = 0, thickness = 3, fontFace = FONT_HERSHEY_SCRIPT_SIMPLEX;\r
174         float fontScale = 2;\r
175         Size textSize = getTextSize( text1, fontFace, fontScale, thickness, &baseline);\r
176         baseline += thickness;\r
177         Point textOrg((img.cols - textSize.width)/2, (img.rows + textSize.height)/2);\r
178         rectangle(img, textOrg + Point(0, baseline), textOrg + Point(textSize.width, -textSize.height), Scalar(0,0,255));\r
179         line(img, textOrg + Point(0, thickness), textOrg + Point(textSize.width, thickness), Scalar(0, 0, 255));\r
180         putText(img, text1, textOrg, fontFace, fontScale, Scalar(150,0,150), thickness, 8);\r
181 \r
182         string text2 = "abcdefghijklmnopqrstuvwxyz1234567890";\r
183         Scalar color(200,0,0);\r
184         fontScale = 0.5, thickness = 1;\r
185         int dist = 5;\r
186 \r
187         textSize = getTextSize( text2, FONT_HERSHEY_SIMPLEX, fontScale, thickness, &baseline);\r
188         textOrg = Point(5,5)+Point(0,textSize.height+dist);\r
189         putText(img, text2, textOrg, FONT_HERSHEY_SIMPLEX, fontScale, color, thickness, CV_AA);\r
190         \r
191         fontScale = 1;\r
192         textSize = getTextSize( text2, FONT_HERSHEY_PLAIN, fontScale, thickness, &baseline);\r
193         textOrg += Point(0,textSize.height+dist);\r
194         putText(img, text2, textOrg, FONT_HERSHEY_PLAIN, fontScale, color, thickness, CV_AA);\r
195 \r
196         fontScale = 0.5;\r
197         textSize = getTextSize( text2, FONT_HERSHEY_DUPLEX, fontScale, thickness, &baseline);\r
198         textOrg += Point(0,textSize.height+dist);\r
199         putText(img, text2, textOrg, FONT_HERSHEY_DUPLEX, fontScale, color, thickness, CV_AA);\r
200 \r
201         textSize = getTextSize( text2, FONT_HERSHEY_COMPLEX, fontScale, thickness, &baseline);\r
202         textOrg += Point(0,textSize.height+dist);\r
203         putText(img, text2, textOrg, FONT_HERSHEY_COMPLEX, fontScale, color, thickness, CV_AA);\r
204 \r
205         textSize = getTextSize( text2, FONT_HERSHEY_TRIPLEX, fontScale, thickness, &baseline);\r
206         textOrg += Point(0,textSize.height+dist);\r
207         putText(img, text2, textOrg, FONT_HERSHEY_TRIPLEX, fontScale, color, thickness, CV_AA);\r
208 \r
209         fontScale = 1;\r
210         textSize = getTextSize( text2, FONT_HERSHEY_COMPLEX_SMALL, fontScale, thickness, &baseline);\r
211         textOrg += Point(0,180) + Point(0,textSize.height+dist);\r
212         putText(img, text2, textOrg, FONT_HERSHEY_COMPLEX_SMALL, fontScale, color, thickness, CV_AA);\r
213 \r
214         textSize = getTextSize( text2, FONT_HERSHEY_SCRIPT_SIMPLEX, fontScale, thickness, &baseline);\r
215         textOrg += Point(0,textSize.height+dist);\r
216         putText(img, text2, textOrg, FONT_HERSHEY_SCRIPT_SIMPLEX, fontScale, color, thickness, CV_AA);\r
217 \r
218         textSize = getTextSize( text2, FONT_HERSHEY_SCRIPT_COMPLEX, fontScale, thickness, &baseline);\r
219         textOrg += Point(0,textSize.height+dist);\r
220         putText(img, text2, textOrg, FONT_HERSHEY_SCRIPT_COMPLEX, fontScale, color, thickness, CV_AA);\r
221 \r
222         dist = 15, fontScale = 0.5;\r
223         textSize = getTextSize( text2, FONT_ITALIC, fontScale, thickness, &baseline);\r
224         textOrg += Point(0,textSize.height+dist);\r
225         putText(img, text2, textOrg, FONT_ITALIC, fontScale, color, thickness, CV_AA);\r
226 }\r
227 \r
228 int CV_DrawingTest_CPP::checkLineIterator( Mat& img )\r
229 {\r
230         LineIterator it( img, Point(0,300), Point(1000, 300) );\r
231         for(int i = 0; i < it.count; ++it, i++ )\r
232         {\r
233                 Vec3b v = (Vec3b)(*(*it)) - img.at<Vec3b>(300,i);\r
234                 float err = (float)norm( v );\r
235                 if( err != 0 )\r
236                 {\r
237                         ts->printf( CvTS::LOG, "LineIterator works incorrect" );\r
238                         return CvTS::FAIL_INVALID_OUTPUT;\r
239                 }\r
240         }\r
241         return CvTS::OK;\r
242 }\r
243 \r
244 class CV_DrawingTest_C : public CV_DrawingTest\r
245 {\r
246 public:\r
247     CV_DrawingTest_C() : CV_DrawingTest( "drawing_c" ) {}\r
248 protected:\r
249         virtual void draw( Mat& img );\r
250         virtual int checkLineIterator( Mat& img);\r
251 };\r
252 \r
253 void CV_DrawingTest_C::draw( Mat& _img )\r
254 {\r
255         CvSize imgSize = cvSize(600, 400);\r
256         _img.create( imgSize, CV_8UC3 );\r
257         CvMat img = _img;\r
258         \r
259         vector<CvPoint> polyline(4);\r
260         polyline[0] = cvPoint(0, 0);\r
261         polyline[1] = cvPoint(imgSize.width, 0);\r
262         polyline[2] = cvPoint(imgSize.width, imgSize.height);\r
263         polyline[3] = cvPoint(0, imgSize.height);\r
264         CvPoint* pts = &polyline[0];\r
265         int n = polyline.size();\r
266         cvFillPoly( &img, &pts, &n, 1, cvScalar(255,255,255) );\r
267 \r
268         CvPoint p1 = cvPoint(1,1), p2 = cvPoint(3,3);\r
269         if( cvClipLine(imgSize, &p1, &p2) )\r
270                 cvCircle( &img, cvPoint(300,100), 40, cvScalar(0,0,255), 3 ); // draw\r
271 \r
272         p1 = cvPoint(1,1), p2 = cvPoint(3,imgSize.height+1000);\r
273         if( cvClipLine(imgSize, &p1, &p2) )\r
274                 cvCircle( &img, cvPoint(500,300), 50, cvScalar(255,0,0), 5, 8, 1 ); // draw\r
275 \r
276         p1 = cvPoint(imgSize.width,1), p2 = cvPoint(imgSize.width,3);\r
277         if( cvClipLine(imgSize, &p1, &p2) )\r
278                 cvCircle( &img, cvPoint(390,100), 10, cvScalar(0,0,255), 3 ); // not draw\r
279 \r
280         p1 = Point(imgSize.width-1,1), p2 = Point(imgSize.width,3);\r
281         if( cvClipLine(imgSize, &p1, &p2) )\r
282                 cvEllipse( &img, cvPoint(390,100), cvSize(20,30), 60, 0, 220.0, cvScalar(0,200,0), 4 ); //draw\r
283 \r
284         CvBox2D box;\r
285         box.center.x = 100;\r
286         box.center.y = 200;\r
287         box.size.width = 200;
288         box.size.height = 100;
289         box.angle = 160;
290         cvEllipseBox( &img, box, Scalar(200,200,255), 5 );\r
291 \r
292         polyline.resize(9);\r
293         pts = &polyline[0];\r
294         n = polyline.size();\r
295         assert( cvEllipse2Poly( cvPoint(430,180), cvSize(100,150), 30, 0, 150, &polyline[0], 20 ) == n );\r
296         cvPolyLine( &img, &pts, &n, 1, false, cvScalar(0,0,150), 4, CV_AA );\r
297         n = 0;\r
298         for( vector<CvPoint>::const_iterator it = polyline.begin(); n < (int)polyline.size()-1; ++it, n++ )\r
299         {\r
300                 cvLine( &img, *it, *(it+1), cvScalar(50,250,100) );\r
301         }\r
302 \r
303         polyline.resize(19);\r
304         pts = &polyline[0];\r
305         n = polyline.size();\r
306         assert( cvEllipse2Poly( cvPoint(500,300), cvSize(50,80), 0, 0, 180, &polyline[0], 10 ) == n );\r
307         cvPolyLine( &img, &pts, &n, 1, true, Scalar(100,200,100), 20 );\r
308         cvFillConvexPoly( &img, pts, n, cvScalar(0, 80, 0) );\r
309 \r
310         polyline.resize(8);\r
311         // external rectengular\r
312         polyline[0] = cvPoint(500, 20);\r
313         polyline[1] = cvPoint(580, 20);\r
314         polyline[2] = cvPoint(580, 100);\r
315         polyline[3] = cvPoint(500, 100);\r
316         // internal rectangular\r
317         polyline[4] = cvPoint(520, 40);\r
318         polyline[5] = cvPoint(560, 40);\r
319         polyline[6] = cvPoint(560, 80);\r
320         polyline[7] = cvPoint(520, 80);\r
321         CvPoint* ppts[] = {&polyline[0], &polyline[0]+4};\r
322         int pn[] = {4, 4};\r
323         cvFillPoly( &img, ppts, pn, 2, cvScalar(100, 100, 0), 8, 0 );\r
324 \r
325         cvRectangle( &img, cvPoint(0, 300), cvPoint(50, 398), cvScalar(0,0,255) );\r
326 \r
327         string text1 = "OpenCV";\r
328         CvFont font;\r
329         cvInitFont( &font, FONT_HERSHEY_SCRIPT_SIMPLEX, 2, 2, 0, 3 );\r
330         int baseline = 0;\r
331         CvSize textSize;\r
332         cvGetTextSize( text1.c_str(), &font, &textSize, &baseline );\r
333         baseline += font.thickness;\r
334         CvPoint textOrg = cvPoint((imgSize.width - textSize.width)/2, (imgSize.height + textSize.height)/2);\r
335         cvRectangle( &img, cvPoint( textOrg.x, textOrg.y + baseline),\r
336                 cvPoint(textOrg.x + textSize.width, textOrg.y - textSize.height), cvScalar(0,0,255));\r
337         cvLine( &img, cvPoint(textOrg.x, textOrg.y + font.thickness), \r
338                 cvPoint(textOrg.x + textSize.width, textOrg.y + font.thickness), cvScalar(0, 0, 255));\r
339         cvPutText( &img, text1.c_str(), textOrg, &font, cvScalar(150,0,150) );\r
340 \r
341     int dist = 5;\r
342         string text2 = "abcdefghijklmnopqrstuvwxyz1234567890";\r
343         CvScalar color = cvScalar(200,0,0);\r
344         cvInitFont( &font, FONT_HERSHEY_SIMPLEX, 0.5, 0.5, 0, 1, CV_AA );\r
345         cvGetTextSize( text2.c_str(), &font, &textSize, &baseline );\r
346         textOrg = cvPoint(5, 5+textSize.height+dist);\r
347         cvPutText(&img, text2.c_str(), textOrg, &font, color );\r
348         \r
349         cvInitFont( &font, FONT_HERSHEY_PLAIN, 1, 1, 0, 1, CV_AA );\r
350         cvGetTextSize( text2.c_str(), &font, &textSize, &baseline );\r
351         textOrg = cvPoint(textOrg.x,textOrg.y+textSize.height+dist);\r
352         cvPutText(&img, text2.c_str(), textOrg, &font, color );\r
353 \r
354         cvInitFont( &font, FONT_HERSHEY_DUPLEX, 0.5, 0.5, 0, 1, CV_AA );\r
355         cvGetTextSize( text2.c_str(), &font, &textSize, &baseline );\r
356         textOrg = cvPoint(textOrg.x,textOrg.y+textSize.height+dist);\r
357         cvPutText(&img, text2.c_str(), textOrg, &font, color );\r
358 \r
359         cvInitFont( &font, FONT_HERSHEY_COMPLEX, 0.5, 0.5, 0, 1, CV_AA );\r
360         cvGetTextSize( text2.c_str(), &font, &textSize, &baseline );\r
361         textOrg = cvPoint(textOrg.x,textOrg.y+textSize.height+dist);\r
362         cvPutText(&img, text2.c_str(), textOrg, &font, color );\r
363 \r
364         cvInitFont( &font, FONT_HERSHEY_TRIPLEX, 0.5, 0.5, 0, 1, CV_AA );\r
365         cvGetTextSize( text2.c_str(), &font, &textSize, &baseline );\r
366         textOrg = cvPoint(textOrg.x,textOrg.y+textSize.height+dist);\r
367         cvPutText(&img, text2.c_str(), textOrg, &font, color );\r
368 \r
369         cvInitFont( &font, FONT_HERSHEY_COMPLEX_SMALL, 1, 1, 0, 1, CV_AA );\r
370         cvGetTextSize( text2.c_str(), &font, &textSize, &baseline );\r
371         textOrg = cvPoint(textOrg.x,textOrg.y+textSize.height+dist + 180);\r
372         cvPutText(&img, text2.c_str(), textOrg, &font, color );\r
373 \r
374         cvInitFont( &font, FONT_HERSHEY_SCRIPT_SIMPLEX, 1, 1, 0, 1, CV_AA );\r
375         cvGetTextSize( text2.c_str(), &font, &textSize, &baseline );\r
376         textOrg = cvPoint(textOrg.x,textOrg.y+textSize.height+dist);\r
377         cvPutText(&img, text2.c_str(), textOrg, &font, color );\r
378 \r
379         cvInitFont( &font, FONT_HERSHEY_SCRIPT_COMPLEX, 1, 1, 0, 1, CV_AA );\r
380         cvGetTextSize( text2.c_str(), &font, &textSize, &baseline );\r
381         textOrg = cvPoint(textOrg.x,textOrg.y+textSize.height+dist);\r
382         cvPutText(&img, text2.c_str(), textOrg, &font, color );\r
383 \r
384         dist = 15;\r
385         cvInitFont( &font, FONT_ITALIC, 0.5, 0.5, 0, 1, CV_AA );\r
386         cvGetTextSize( text2.c_str(), &font, &textSize, &baseline );\r
387         textOrg = cvPoint(textOrg.x,textOrg.y+textSize.height+dist);\r
388         cvPutText(&img, text2.c_str(), textOrg, &font, color );\r
389 }\r
390 \r
391 int CV_DrawingTest_C::checkLineIterator( Mat& _img )\r
392 {\r
393         CvLineIterator it;\r
394         CvMat img = _img;\r
395         int count = cvInitLineIterator( &img, cvPoint(0,300), cvPoint(1000, 300), &it );\r
396         for(int i = 0; i < count; i++ )\r
397         {\r
398                 Vec3b v = (Vec3b)(*(it.ptr)) - _img.at<Vec3b>(300,i);\r
399                 float err = (float)norm( v );\r
400                 if( err != 0 )\r
401                 {\r
402                         ts->printf( CvTS::LOG, "CvLineIterator works incorrect" );\r
403                         return CvTS::FAIL_INVALID_OUTPUT;\r
404                 }\r
405                 CV_NEXT_LINE_POINT(it);\r
406         }\r
407         return CvTS::OK;\r
408 }\r
409 \r
410 CV_DrawingTest_CPP drawing_test_cpp;\r
411 CV_DrawingTest_C drawing_test_c;