]> rtime.felk.cvut.cz Git - opencv.git/commitdiff
added test for drawing functions
authormdim <mdim@73c94f0f-984f-4a5f-82bc-2d8db8d8ee08>
Fri, 18 Dec 2009 14:33:56 +0000 (14:33 +0000)
committermdim <mdim@73c94f0f-984f-4a5f-82bc-2d8db8d8ee08>
Fri, 18 Dec 2009 14:33:56 +0000 (14:33 +0000)
git-svn-id: https://code.ros.org/svn/opencv/trunk@2460 73c94f0f-984f-4a5f-82bc-2d8db8d8ee08

opencv/tests/cv/src/adrawing.cpp [new file with mode: 0644]
opencv_extra/testdata/cv/drawing/image.jpg [new file with mode: 0644]

diff --git a/opencv/tests/cv/src/adrawing.cpp b/opencv/tests/cv/src/adrawing.cpp
new file mode 100644 (file)
index 0000000..5b134af
--- /dev/null
@@ -0,0 +1,411 @@
+/*M///////////////////////////////////////////////////////////////////////////////////////\r
+//\r
+//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.\r
+//\r
+//  By downloading, copying, installing or using the software you agree to this license.\r
+//  If you do not agree to this license, do not download, install,\r
+//  copy or use the software.\r
+//\r
+//\r
+//                        Intel License Agreement\r
+//                For Open Source Computer Vision Library\r
+//\r
+// Copyright (C) 2000, Intel Corporation, all rights reserved.\r
+// Third party copyrights are property of their respective owners.\r
+//\r
+// Redistribution and use in source and binary forms, with or without modification,\r
+// are permitted provided that the following conditions are met:\r
+//\r
+//   * Redistribution's of source code must retain the above copyright notice,\r
+//     this list of conditions and the following disclaimer.\r
+//\r
+//   * Redistribution's in binary form must reproduce the above copyright notice,\r
+//     this list of conditions and the following disclaimer in the documentation\r
+//     and/or other materials provided with the distribution.\r
+//\r
+//   * The name of Intel Corporation may not be used to endorse or promote products\r
+//     derived from this software without specific prior written permission.\r
+//\r
+// This software is provided by the copyright holders and contributors "as is" and\r
+// any express or implied warranties, including, but not limited to, the implied\r
+// warranties of merchantability and fitness for a particular purpose are disclaimed.\r
+// In no event shall the Intel Corporation or contributors be liable for any direct,\r
+// indirect, incidental, special, exemplary, or consequential damages\r
+// (including, but not limited to, procurement of substitute goods or services;\r
+// loss of use, data, or profits; or business interruption) however caused\r
+// and on any theory of liability, whether in contract, strict liability,\r
+// or tort (including negligence or otherwise) arising in any way out of\r
+// the use of this software, even if advised of the possibility of such damage.\r
+//\r
+//M*/\r
+\r
+#include "cvtest.h"\r
+#include "highgui.h"\r
+\r
+using namespace cv;\r
+\r
+//#define DRAW_TEST_IMAGE\r
+\r
+class CV_DrawingTest : public CvTest\r
+{\r
+public:\r
+       CV_DrawingTest( const char* testName ) : CvTest( testName, "drawing funcs" ) {}\r
+protected:\r
+    void run( int );\r
+       virtual void draw( Mat& img ) = 0;\r
+       virtual int checkLineIterator( Mat& img) = 0;\r
+};\r
+\r
+void CV_DrawingTest::run( int )\r
+{\r
+    int code = CvTS::OK;\r
+       Mat testImg, valImg;
+       const string name = "drawing/image.jpg";
+       string path = ts->get_data_path(), filename;
+       filename = path + name;
+       
+       draw( testImg );
+
+#ifdef DRAW_TEST_IMAGE
+       imwrite( filename, testImg );
+#else
+       valImg = imread( filename );
+       if( valImg.empty() )
+       {
+               ts->printf( CvTS::LOG, "test image can not be read");
+               code = CvTS::FAIL_INVALID_TEST_DATA;
+       }
+       else
+       {
+               float err = (float)norm( testImg, valImg, CV_RELATIVE_L1 );
+               float Eps = 0.9f;
+               if( err > Eps)
+               {
+                       ts->printf( CvTS::LOG, "CV_RELATIVE_L1 between testImg and valImg is equal %f (larger than %f)\n", err, Eps );\r
+                       code = CvTS::FAIL_BAD_ACCURACY;
+               }
+               else
+               {
+                       code = checkLineIterator( testImg );
+               }\r
+       }\r
+#endif
+    ts->set_failed_test_info( code );\r
+}\r
+\r
+class CV_DrawingTest_CPP : public CV_DrawingTest\r
+{\r
+public:\r
+    CV_DrawingTest_CPP() : CV_DrawingTest( "drawing_cpp" ) {}\r
+protected:\r
+       virtual void draw( Mat& img );\r
+       virtual int checkLineIterator( Mat& img);\r
+};\r
+\r
+void CV_DrawingTest_CPP::draw( Mat& img )\r
+{\r
+       Size imgSize( 600, 400 );\r
+       img.create( imgSize, CV_8UC3 );\r
+\r
+       vector<Point> polyline(4);\r
+       polyline[0] = Point(0, 0);\r
+       polyline[1] = Point(imgSize.width, 0);\r
+       polyline[2] = Point(imgSize.width, imgSize.height);\r
+       polyline[3] = Point(0, imgSize.height);\r
+       const Point* pts = &polyline[0];\r
+       int n = polyline.size();\r
+       fillPoly( img, &pts, &n, 1, Scalar::all(255) );\r
+\r
+       Point p1(1,1), p2(3,3);\r
+       if( clipLine(Rect(0,0,imgSize.width,imgSize.height), p1, p2) && clipLine(imgSize, p1, p2) )\r
+               circle( img, Point(300,100), 40, Scalar(0,0,255), 3 ); // draw\r
+\r
+       p2 = Point(3,imgSize.height+1000);\r
+       if( clipLine(Rect(0,0,imgSize.width,imgSize.height), p1, p2) && clipLine(imgSize, p1, p2) )\r
+               circle( img, Point(500,300), 50, Scalar(255,0,0), 5, 8, 1 ); // draw\r
+\r
+       p1 = Point(imgSize.width,1), p2 = Point(imgSize.width,3);\r
+       if( clipLine(Rect(0,0,imgSize.width,imgSize.height), p1, p2) && clipLine(imgSize, p1, p2) )\r
+               circle( img, Point(390,100), 10, Scalar(0,0,255), 3 ); // not draw\r
+\r
+       p1 = Point(imgSize.width-1,1), p2 = Point(imgSize.width,3);\r
+       if( clipLine(Rect(0,0,imgSize.width,imgSize.height), p1, p2) && clipLine(imgSize, p1, p2) )\r
+               ellipse( img, Point(390,100), Size(20,30), 60, 0, 220.0, Scalar(0,200,0), 4 ); //draw\r
+\r
+       ellipse( img, RotatedRect(Point(100,200),Size(200,100),160), Scalar(200,200,255), 5 );\r
+\r
+       polyline.clear();\r
+       ellipse2Poly( Point(430,180), Size(100,150), 30, 0, 150, 20, polyline );\r
+       pts = &polyline[0];\r
+       n = polyline.size();\r
+       polylines( img, &pts, &n, 1, false, Scalar(0,0,150), 4, CV_AA );\r
+       n = 0;\r
+       for( vector<Point>::const_iterator it = polyline.begin(); n < (int)polyline.size()-1; ++it, n++ )\r
+       {\r
+               line( img, *it, *(it+1), Scalar(50,250,100));\r
+       }\r
+\r
+       polyline.clear();\r
+       ellipse2Poly( Point(500,300), Size(50,80), 0, 0, 180, 10, polyline );\r
+       pts = &polyline[0];\r
+       n = polyline.size();\r
+       polylines( img, &pts, &n, 1, true, Scalar(100,200,100), 20 );\r
+       fillConvexPoly( img, pts, n, Scalar(0, 80, 0) );\r
+\r
+       polyline.resize(8);\r
+       // external rectengular\r
+       polyline[0] = Point(0, 0);\r
+       polyline[1] = Point(80, 0);\r
+       polyline[2] = Point(80, 80);\r
+       polyline[3] = Point(0, 80);\r
+       // internal rectangular\r
+       polyline[4] = Point(20, 20);\r
+       polyline[5] = Point(60, 20);\r
+       polyline[6] = Point(60, 60);\r
+       polyline[7] = Point(20, 60);\r
+       const Point* ppts[] = {&polyline[0], &polyline[0]+4};\r
+       int pn[] = {4, 4};\r
+       fillPoly( img, ppts, pn, 2, Scalar(100, 100, 0), 8, 0, Point(500, 20) );\r
+\r
+       rectangle( img, Point(0, 300), Point(50, 398), Scalar(0,0,255) );\r
+\r
+       string text1 = "OpenCV";\r
+       int baseline = 0, thickness = 3, fontFace = FONT_HERSHEY_SCRIPT_SIMPLEX;\r
+       float fontScale = 2;\r
+       Size textSize = getTextSize( text1, fontFace, fontScale, thickness, &baseline);\r
+       baseline += thickness;\r
+       Point textOrg((img.cols - textSize.width)/2, (img.rows + textSize.height)/2);\r
+       rectangle(img, textOrg + Point(0, baseline), textOrg + Point(textSize.width, -textSize.height), Scalar(0,0,255));\r
+       line(img, textOrg + Point(0, thickness), textOrg + Point(textSize.width, thickness), Scalar(0, 0, 255));\r
+       putText(img, text1, textOrg, fontFace, fontScale, Scalar(150,0,150), thickness, 8);\r
+\r
+       string text2 = "abcdefghijklmnopqrstuvwxyz1234567890";\r
+       Scalar color(200,0,0);\r
+       fontScale = 0.5, thickness = 1;\r
+       int dist = 5;\r
+\r
+       textSize = getTextSize( text2, FONT_HERSHEY_SIMPLEX, fontScale, thickness, &baseline);\r
+       textOrg = Point(5,5)+Point(0,textSize.height+dist);\r
+       putText(img, text2, textOrg, FONT_HERSHEY_SIMPLEX, fontScale, color, thickness, CV_AA);\r
+       \r
+       fontScale = 1;\r
+       textSize = getTextSize( text2, FONT_HERSHEY_PLAIN, fontScale, thickness, &baseline);\r
+       textOrg += Point(0,textSize.height+dist);\r
+       putText(img, text2, textOrg, FONT_HERSHEY_PLAIN, fontScale, color, thickness, CV_AA);\r
+\r
+       fontScale = 0.5;\r
+       textSize = getTextSize( text2, FONT_HERSHEY_DUPLEX, fontScale, thickness, &baseline);\r
+       textOrg += Point(0,textSize.height+dist);\r
+       putText(img, text2, textOrg, FONT_HERSHEY_DUPLEX, fontScale, color, thickness, CV_AA);\r
+\r
+       textSize = getTextSize( text2, FONT_HERSHEY_COMPLEX, fontScale, thickness, &baseline);\r
+       textOrg += Point(0,textSize.height+dist);\r
+       putText(img, text2, textOrg, FONT_HERSHEY_COMPLEX, fontScale, color, thickness, CV_AA);\r
+\r
+       textSize = getTextSize( text2, FONT_HERSHEY_TRIPLEX, fontScale, thickness, &baseline);\r
+       textOrg += Point(0,textSize.height+dist);\r
+       putText(img, text2, textOrg, FONT_HERSHEY_TRIPLEX, fontScale, color, thickness, CV_AA);\r
+\r
+       fontScale = 1;\r
+       textSize = getTextSize( text2, FONT_HERSHEY_COMPLEX_SMALL, fontScale, thickness, &baseline);\r
+       textOrg += Point(0,180) + Point(0,textSize.height+dist);\r
+       putText(img, text2, textOrg, FONT_HERSHEY_COMPLEX_SMALL, fontScale, color, thickness, CV_AA);\r
+\r
+       textSize = getTextSize( text2, FONT_HERSHEY_SCRIPT_SIMPLEX, fontScale, thickness, &baseline);\r
+       textOrg += Point(0,textSize.height+dist);\r
+       putText(img, text2, textOrg, FONT_HERSHEY_SCRIPT_SIMPLEX, fontScale, color, thickness, CV_AA);\r
+\r
+       textSize = getTextSize( text2, FONT_HERSHEY_SCRIPT_COMPLEX, fontScale, thickness, &baseline);\r
+       textOrg += Point(0,textSize.height+dist);\r
+       putText(img, text2, textOrg, FONT_HERSHEY_SCRIPT_COMPLEX, fontScale, color, thickness, CV_AA);\r
+\r
+       dist = 15, fontScale = 0.5;\r
+       textSize = getTextSize( text2, FONT_ITALIC, fontScale, thickness, &baseline);\r
+       textOrg += Point(0,textSize.height+dist);\r
+       putText(img, text2, textOrg, FONT_ITALIC, fontScale, color, thickness, CV_AA);\r
+}\r
+\r
+int CV_DrawingTest_CPP::checkLineIterator( Mat& img )\r
+{\r
+       LineIterator it( img, Point(0,300), Point(1000, 300) );\r
+       for(int i = 0; i < it.count; ++it, i++ )\r
+       {\r
+               Vec3b v = (Vec3b)(*(*it)) - img.at<Vec3b>(300,i);\r
+               float err = (float)norm( v );\r
+               if( err != 0 )\r
+               {\r
+                       ts->printf( CvTS::LOG, "LineIterator works incorrect" );\r
+                       return CvTS::FAIL_INVALID_OUTPUT;\r
+               }\r
+       }\r
+       return CvTS::OK;\r
+}\r
+\r
+class CV_DrawingTest_C : public CV_DrawingTest\r
+{\r
+public:\r
+    CV_DrawingTest_C() : CV_DrawingTest( "drawing_c" ) {}\r
+protected:\r
+       virtual void draw( Mat& img );\r
+       virtual int checkLineIterator( Mat& img);\r
+};\r
+\r
+void CV_DrawingTest_C::draw( Mat& _img )\r
+{\r
+       CvSize imgSize = cvSize(600, 400);\r
+       _img.create( imgSize, CV_8UC3 );\r
+       CvMat img = _img;\r
+       \r
+       vector<CvPoint> polyline(4);\r
+       polyline[0] = cvPoint(0, 0);\r
+       polyline[1] = cvPoint(imgSize.width, 0);\r
+       polyline[2] = cvPoint(imgSize.width, imgSize.height);\r
+       polyline[3] = cvPoint(0, imgSize.height);\r
+       CvPoint* pts = &polyline[0];\r
+       int n = polyline.size();\r
+       cvFillPoly( &img, &pts, &n, 1, cvScalar(255,255,255) );\r
+\r
+       CvPoint p1 = cvPoint(1,1), p2 = cvPoint(3,3);\r
+       if( cvClipLine(imgSize, &p1, &p2) )\r
+               cvCircle( &img, cvPoint(300,100), 40, cvScalar(0,0,255), 3 ); // draw\r
+\r
+       p1 = cvPoint(1,1), p2 = cvPoint(3,imgSize.height+1000);\r
+       if( cvClipLine(imgSize, &p1, &p2) )\r
+               cvCircle( &img, cvPoint(500,300), 50, cvScalar(255,0,0), 5, 8, 1 ); // draw\r
+\r
+       p1 = cvPoint(imgSize.width,1), p2 = cvPoint(imgSize.width,3);\r
+       if( cvClipLine(imgSize, &p1, &p2) )\r
+               cvCircle( &img, cvPoint(390,100), 10, cvScalar(0,0,255), 3 ); // not draw\r
+\r
+       p1 = Point(imgSize.width-1,1), p2 = Point(imgSize.width,3);\r
+       if( cvClipLine(imgSize, &p1, &p2) )\r
+               cvEllipse( &img, cvPoint(390,100), cvSize(20,30), 60, 0, 220.0, cvScalar(0,200,0), 4 ); //draw\r
+\r
+       CvBox2D box;\r
+       box.center.x = 100;\r
+       box.center.y = 200;\r
+       box.size.width = 200;
+       box.size.height = 100;
+       box.angle = 160;
+       cvEllipseBox( &img, box, Scalar(200,200,255), 5 );\r
+\r
+       polyline.resize(9);\r
+       pts = &polyline[0];\r
+       n = polyline.size();\r
+       assert( cvEllipse2Poly( cvPoint(430,180), cvSize(100,150), 30, 0, 150, &polyline[0], 20 ) == n );\r
+       cvPolyLine( &img, &pts, &n, 1, false, cvScalar(0,0,150), 4, CV_AA );\r
+       n = 0;\r
+       for( vector<CvPoint>::const_iterator it = polyline.begin(); n < (int)polyline.size()-1; ++it, n++ )\r
+       {\r
+               cvLine( &img, *it, *(it+1), cvScalar(50,250,100) );\r
+       }\r
+\r
+       polyline.resize(19);\r
+       pts = &polyline[0];\r
+       n = polyline.size();\r
+       assert( cvEllipse2Poly( cvPoint(500,300), cvSize(50,80), 0, 0, 180, &polyline[0], 10 ) == n );\r
+       cvPolyLine( &img, &pts, &n, 1, true, Scalar(100,200,100), 20 );\r
+       cvFillConvexPoly( &img, pts, n, cvScalar(0, 80, 0) );\r
+\r
+       polyline.resize(8);\r
+       // external rectengular\r
+       polyline[0] = cvPoint(500, 20);\r
+       polyline[1] = cvPoint(580, 20);\r
+       polyline[2] = cvPoint(580, 100);\r
+       polyline[3] = cvPoint(500, 100);\r
+       // internal rectangular\r
+       polyline[4] = cvPoint(520, 40);\r
+       polyline[5] = cvPoint(560, 40);\r
+       polyline[6] = cvPoint(560, 80);\r
+       polyline[7] = cvPoint(520, 80);\r
+       CvPoint* ppts[] = {&polyline[0], &polyline[0]+4};\r
+       int pn[] = {4, 4};\r
+       cvFillPoly( &img, ppts, pn, 2, cvScalar(100, 100, 0), 8, 0 );\r
+\r
+       cvRectangle( &img, cvPoint(0, 300), cvPoint(50, 398), cvScalar(0,0,255) );\r
+\r
+       string text1 = "OpenCV";\r
+       CvFont font;\r
+       cvInitFont( &font, FONT_HERSHEY_SCRIPT_SIMPLEX, 2, 2, 0, 3 );\r
+       int baseline = 0;\r
+       CvSize textSize;\r
+       cvGetTextSize( text1.c_str(), &font, &textSize, &baseline );\r
+       baseline += font.thickness;\r
+       CvPoint textOrg = cvPoint((imgSize.width - textSize.width)/2, (imgSize.height + textSize.height)/2);\r
+       cvRectangle( &img, cvPoint( textOrg.x, textOrg.y + baseline),\r
+               cvPoint(textOrg.x + textSize.width, textOrg.y - textSize.height), cvScalar(0,0,255));\r
+       cvLine( &img, cvPoint(textOrg.x, textOrg.y + font.thickness), \r
+               cvPoint(textOrg.x + textSize.width, textOrg.y + font.thickness), cvScalar(0, 0, 255));\r
+       cvPutText( &img, text1.c_str(), textOrg, &font, cvScalar(150,0,150) );\r
+\r
+    int dist = 5;\r
+       string text2 = "abcdefghijklmnopqrstuvwxyz1234567890";\r
+       CvScalar color = cvScalar(200,0,0);\r
+       cvInitFont( &font, FONT_HERSHEY_SIMPLEX, 0.5, 0.5, 0, 1, CV_AA );\r
+       cvGetTextSize( text2.c_str(), &font, &textSize, &baseline );\r
+       textOrg = cvPoint(5, 5+textSize.height+dist);\r
+       cvPutText(&img, text2.c_str(), textOrg, &font, color );\r
+       \r
+       cvInitFont( &font, FONT_HERSHEY_PLAIN, 1, 1, 0, 1, CV_AA );\r
+       cvGetTextSize( text2.c_str(), &font, &textSize, &baseline );\r
+       textOrg = cvPoint(textOrg.x,textOrg.y+textSize.height+dist);\r
+       cvPutText(&img, text2.c_str(), textOrg, &font, color );\r
+\r
+       cvInitFont( &font, FONT_HERSHEY_DUPLEX, 0.5, 0.5, 0, 1, CV_AA );\r
+       cvGetTextSize( text2.c_str(), &font, &textSize, &baseline );\r
+       textOrg = cvPoint(textOrg.x,textOrg.y+textSize.height+dist);\r
+       cvPutText(&img, text2.c_str(), textOrg, &font, color );\r
+\r
+       cvInitFont( &font, FONT_HERSHEY_COMPLEX, 0.5, 0.5, 0, 1, CV_AA );\r
+       cvGetTextSize( text2.c_str(), &font, &textSize, &baseline );\r
+       textOrg = cvPoint(textOrg.x,textOrg.y+textSize.height+dist);\r
+       cvPutText(&img, text2.c_str(), textOrg, &font, color );\r
+\r
+       cvInitFont( &font, FONT_HERSHEY_TRIPLEX, 0.5, 0.5, 0, 1, CV_AA );\r
+       cvGetTextSize( text2.c_str(), &font, &textSize, &baseline );\r
+       textOrg = cvPoint(textOrg.x,textOrg.y+textSize.height+dist);\r
+       cvPutText(&img, text2.c_str(), textOrg, &font, color );\r
+\r
+       cvInitFont( &font, FONT_HERSHEY_COMPLEX_SMALL, 1, 1, 0, 1, CV_AA );\r
+       cvGetTextSize( text2.c_str(), &font, &textSize, &baseline );\r
+       textOrg = cvPoint(textOrg.x,textOrg.y+textSize.height+dist + 180);\r
+       cvPutText(&img, text2.c_str(), textOrg, &font, color );\r
+\r
+       cvInitFont( &font, FONT_HERSHEY_SCRIPT_SIMPLEX, 1, 1, 0, 1, CV_AA );\r
+       cvGetTextSize( text2.c_str(), &font, &textSize, &baseline );\r
+       textOrg = cvPoint(textOrg.x,textOrg.y+textSize.height+dist);\r
+       cvPutText(&img, text2.c_str(), textOrg, &font, color );\r
+\r
+       cvInitFont( &font, FONT_HERSHEY_SCRIPT_COMPLEX, 1, 1, 0, 1, CV_AA );\r
+       cvGetTextSize( text2.c_str(), &font, &textSize, &baseline );\r
+       textOrg = cvPoint(textOrg.x,textOrg.y+textSize.height+dist);\r
+       cvPutText(&img, text2.c_str(), textOrg, &font, color );\r
+\r
+       dist = 15;\r
+       cvInitFont( &font, FONT_ITALIC, 0.5, 0.5, 0, 1, CV_AA );\r
+       cvGetTextSize( text2.c_str(), &font, &textSize, &baseline );\r
+       textOrg = cvPoint(textOrg.x,textOrg.y+textSize.height+dist);\r
+       cvPutText(&img, text2.c_str(), textOrg, &font, color );\r
+}\r
+\r
+int CV_DrawingTest_C::checkLineIterator( Mat& _img )\r
+{\r
+       CvLineIterator it;\r
+       CvMat img = _img;\r
+       int count = cvInitLineIterator( &img, cvPoint(0,300), cvPoint(1000, 300), &it );\r
+       for(int i = 0; i < count; i++ )\r
+       {\r
+               Vec3b v = (Vec3b)(*(it.ptr)) - _img.at<Vec3b>(300,i);\r
+               float err = (float)norm( v );\r
+               if( err != 0 )\r
+               {\r
+                       ts->printf( CvTS::LOG, "CvLineIterator works incorrect" );\r
+                       return CvTS::FAIL_INVALID_OUTPUT;\r
+               }\r
+               CV_NEXT_LINE_POINT(it);\r
+       }\r
+       return CvTS::OK;\r
+}\r
+\r
+CV_DrawingTest_CPP drawing_test_cpp;\r
+CV_DrawingTest_C drawing_test_c;
\ No newline at end of file
diff --git a/opencv_extra/testdata/cv/drawing/image.jpg b/opencv_extra/testdata/cv/drawing/image.jpg
new file mode 100644 (file)
index 0000000..4c23475
Binary files /dev/null and b/opencv_extra/testdata/cv/drawing/image.jpg differ