1 /*M///////////////////////////////////////////////////////////////////////////////////////
\r
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
\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
10 // Intel License Agreement
\r
11 // For Open Source Computer Vision Library
\r
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
\r
14 // Third party copyrights are property of their respective owners.
\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
19 // * Redistribution's of source code must retain the above copyright notice,
\r
20 // this list of conditions and the following disclaimer.
\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
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
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
43 #include "highgui.h"
\r
47 //#define DRAW_TEST_IMAGE
\r
49 class CV_DrawingTest : public CvTest
\r
52 CV_DrawingTest( const char* testName ) : CvTest( testName, "drawing funcs" ) {}
\r
55 virtual void draw( Mat& img ) = 0;
\r
56 virtual int checkLineIterator( Mat& img) = 0;
\r
59 void CV_DrawingTest::run( int )
\r
61 int code = CvTS::OK;
\r
63 const string name = "drawing/image.jpg";
64 string path = ts->get_data_path(), filename;
65 filename = path + name;
69 #ifdef DRAW_TEST_IMAGE
70 imwrite( filename, testImg );
72 valImg = imread( filename );
75 ts->printf( CvTS::LOG, "test image can not be read");
76 code = CvTS::FAIL_INVALID_TEST_DATA;
80 float err = (float)norm( testImg, valImg, CV_RELATIVE_L1 );
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;
89 code = checkLineIterator( testImg );
93 ts->set_failed_test_info( code );
\r
96 class CV_DrawingTest_CPP : public CV_DrawingTest
\r
99 CV_DrawingTest_CPP() : CV_DrawingTest( "drawing_cpp" ) {}
\r
101 virtual void draw( Mat& img );
\r
102 virtual int checkLineIterator( Mat& img);
\r
105 void CV_DrawingTest_CPP::draw( Mat& img )
\r
107 Size imgSize( 600, 400 );
\r
108 img.create( imgSize, CV_8UC3 );
\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
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
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
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
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
135 ellipse( img, RotatedRect(Point(100,200),Size(200,100),160), Scalar(200,200,255), 5 );
\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
143 for( vector<Point>::const_iterator it = polyline.begin(); n < (int)polyline.size()-1; ++it, n++ )
\r
145 line( img, *it, *(it+1), Scalar(50,250,100));
\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
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
168 fillPoly( img, ppts, pn, 2, Scalar(100, 100, 0), 8, 0, Point(500, 20) );
\r
170 rectangle( img, Point(0, 300), Point(50, 398), Scalar(0,0,255) );
\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
182 string text2 = "abcdefghijklmnopqrstuvwxyz1234567890";
\r
183 Scalar color(200,0,0);
\r
184 fontScale = 0.5, thickness = 1;
\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
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
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
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
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
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
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
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
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
228 int CV_DrawingTest_CPP::checkLineIterator( Mat& img )
\r
230 LineIterator it( img, Point(0,300), Point(1000, 300) );
\r
231 for(int i = 0; i < it.count; ++it, i++ )
\r
233 Vec3b v = (Vec3b)(*(*it)) - img.at<Vec3b>(300,i);
\r
234 float err = (float)norm( v );
\r
237 ts->printf( CvTS::LOG, "LineIterator works incorrect" );
\r
238 return CvTS::FAIL_INVALID_OUTPUT;
\r
244 class CV_DrawingTest_C : public CV_DrawingTest
\r
247 CV_DrawingTest_C() : CV_DrawingTest( "drawing_c" ) {}
\r
249 virtual void draw( Mat& img );
\r
250 virtual int checkLineIterator( Mat& img);
\r
253 void CV_DrawingTest_C::draw( Mat& _img )
\r
255 CvSize imgSize = cvSize(600, 400);
\r
256 _img.create( imgSize, CV_8UC3 );
\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
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
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
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
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
285 box.center.x = 100;
\r
286 box.center.y = 200;
\r
287 box.size.width = 200;
288 box.size.height = 100;
290 cvEllipseBox( &img, box, Scalar(200,200,255), 5 );
\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
298 for( vector<CvPoint>::const_iterator it = polyline.begin(); n < (int)polyline.size()-1; ++it, n++ )
\r
300 cvLine( &img, *it, *(it+1), cvScalar(50,250,100) );
\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
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
323 cvFillPoly( &img, ppts, pn, 2, cvScalar(100, 100, 0), 8, 0 );
\r
325 cvRectangle( &img, cvPoint(0, 300), cvPoint(50, 398), cvScalar(0,0,255) );
\r
327 string text1 = "OpenCV";
\r
329 cvInitFont( &font, FONT_HERSHEY_SCRIPT_SIMPLEX, 2, 2, 0, 3 );
\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
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
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
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
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
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
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
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
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
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
391 int CV_DrawingTest_C::checkLineIterator( Mat& _img )
\r
395 int count = cvInitLineIterator( &img, cvPoint(0,300), cvPoint(1000, 300), &it );
\r
396 for(int i = 0; i < count; i++ )
\r
398 Vec3b v = (Vec3b)(*(it.ptr)) - _img.at<Vec3b>(300,i);
\r
399 float err = (float)norm( v );
\r
402 ts->printf( CvTS::LOG, "CvLineIterator works incorrect" );
\r
403 return CvTS::FAIL_INVALID_OUTPUT;
\r
405 CV_NEXT_LINE_POINT(it);
\r
410 CV_DrawingTest_CPP drawing_test_cpp;
\r
411 CV_DrawingTest_C drawing_test_c;