Path: blob/master/modules/imgproc/test/test_drawing.cpp
16354 views
/*M///////////////////////////////////////////////////////////////////////////////////////1//2// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.3//4// By downloading, copying, installing or using the software you agree to this license.5// If you do not agree to this license, do not download, install,6// copy or use the software.7//8//9// License Agreement10// For Open Source Computer Vision Library11//12// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.13// Copyright (C) 2009, Willow Garage Inc., all rights reserved.14// Third party copyrights are property of their respective owners.15//16// Redistribution and use in source and binary forms, with or without modification,17// are permitted provided that the following conditions are met:18//19// * Redistribution's of source code must retain the above copyright notice,20// this list of conditions and the following disclaimer.21//22// * Redistribution's in binary form must reproduce the above copyright notice,23// this list of conditions and the following disclaimer in the documentation24// and/or other materials provided with the distribution.25//26// * The name of the copyright holders may not be used to endorse or promote products27// derived from this software without specific prior written permission.28//29// This software is provided by the copyright holders and contributors "as is" and30// any express or implied warranties, including, but not limited to, the implied31// warranties of merchantability and fitness for a particular purpose are disclaimed.32// In no event shall the Intel Corporation or contributors be liable for any direct,33// indirect, incidental, special, exemplary, or consequential damages34// (including, but not limited to, procurement of substitute goods or services;35// loss of use, data, or profits; or business interruption) however caused36// and on any theory of liability, whether in contract, strict liability,37// or tort (including negligence or otherwise) arising in any way out of38// the use of this software, even if advised of the possibility of such damage.39//40//M*/4142#include "test_precomp.hpp"4344namespace opencv_test { namespace {4546//#define DRAW_TEST_IMAGE4748class CV_DrawingTest : public cvtest::BaseTest49{50public:51CV_DrawingTest(){}52protected:53void run( int );54virtual void draw( Mat& img ) = 0;55virtual int checkLineIterator( Mat& img) = 0;56};5758void CV_DrawingTest::run( int )59{60Mat testImg, valImg;61const string fname = "../highgui/drawing/image.png";62string path = ts->get_data_path(), filename;63filename = path + fname;6465draw( testImg );6667valImg = imread( filename );68if( valImg.empty() )69{70//imwrite( filename, testImg );71ts->printf( ts->LOG, "test image can not be read");72#ifdef HAVE_PNG73ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA);74#else75ts->printf( ts->LOG, "PNG image support is not available");76ts->set_failed_test_info(cvtest::TS::OK);77#endif78return;79}80else81{82// image should match exactly83float err = (float)cvtest::norm( testImg, valImg, NORM_L1 );84float Eps = 1;85if( err > Eps)86{87ts->printf( ts->LOG, "NORM_L1 between testImg and valImg is equal %f (larger than %f)\n", err, Eps );88ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);89}90else91{92ts->set_failed_test_info(checkLineIterator( testImg ));93}94}95ts->set_failed_test_info(cvtest::TS::OK);96}9798class CV_DrawingTest_CPP : public CV_DrawingTest99{100public:101CV_DrawingTest_CPP() {}102protected:103virtual void draw( Mat& img );104virtual int checkLineIterator( Mat& img);105};106107void CV_DrawingTest_CPP::draw( Mat& img )108{109Size imgSize( 600, 400 );110img.create( imgSize, CV_8UC3 );111112vector<Point> polyline(4);113polyline[0] = Point(0, 0);114polyline[1] = Point(imgSize.width, 0);115polyline[2] = Point(imgSize.width, imgSize.height);116polyline[3] = Point(0, imgSize.height);117const Point* pts = &polyline[0];118int n = (int)polyline.size();119fillPoly( img, &pts, &n, 1, Scalar::all(255) );120121Point p1(1,1), p2(3,3);122if( clipLine(Rect(0,0,imgSize.width,imgSize.height), p1, p2) && clipLine(imgSize, p1, p2) )123circle( img, Point(300,100), 40, Scalar(0,0,255), 3 ); // draw124125p2 = Point(3,imgSize.height+1000);126if( clipLine(Rect(0,0,imgSize.width,imgSize.height), p1, p2) && clipLine(imgSize, p1, p2) )127circle( img, Point(500,300), 50, cvColorToScalar(255,CV_8UC3), 5, 8, 1 ); // draw128129p1 = Point(imgSize.width,1), p2 = Point(imgSize.width,3);130if( clipLine(Rect(0,0,imgSize.width,imgSize.height), p1, p2) && clipLine(imgSize, p1, p2) )131circle( img, Point(390,100), 10, Scalar(0,0,255), 3 ); // not draw132133p1 = Point(imgSize.width-1,1), p2 = Point(imgSize.width,3);134if( clipLine(Rect(0,0,imgSize.width,imgSize.height), p1, p2) && clipLine(imgSize, p1, p2) )135ellipse( img, Point(390,100), Size(20,30), 60, 0, 220.0, Scalar(0,200,0), 4 ); //draw136137ellipse( img, RotatedRect(Point(100,200),Size(200,100),160), Scalar(200,200,255), 5 );138139polyline.clear();140ellipse2Poly( Point(430,180), Size(100,150), 30, 0, 150, 20, polyline );141pts = &polyline[0];142n = (int)polyline.size();143polylines( img, &pts, &n, 1, false, Scalar(0,0,150), 4, CV_AA );144n = 0;145for( vector<Point>::const_iterator it = polyline.begin(); n < (int)polyline.size()-1; ++it, n++ )146{147line( img, *it, *(it+1), Scalar(50,250,100));148}149150polyline.clear();151ellipse2Poly( Point(500,300), Size(50,80), 0, 0, 180, 10, polyline );152pts = &polyline[0];153n = (int)polyline.size();154polylines( img, &pts, &n, 1, true, Scalar(100,200,100), 20 );155fillConvexPoly( img, pts, n, Scalar(0, 80, 0) );156157polyline.resize(8);158// external rectengular159polyline[0] = Point(0, 0);160polyline[1] = Point(80, 0);161polyline[2] = Point(80, 80);162polyline[3] = Point(0, 80);163// internal rectangular164polyline[4] = Point(20, 20);165polyline[5] = Point(60, 20);166polyline[6] = Point(60, 60);167polyline[7] = Point(20, 60);168const Point* ppts[] = {&polyline[0], &polyline[0]+4};169int pn[] = {4, 4};170fillPoly( img, ppts, pn, 2, Scalar(100, 100, 0), 8, 0, Point(500, 20) );171172rectangle( img, Point(0, 300), Point(50, 398), Scalar(0,0,255) );173174string text1 = "OpenCV";175int baseline = 0, thickness = 3, fontFace = FONT_HERSHEY_SCRIPT_SIMPLEX;176float fontScale = 2;177Size textSize = getTextSize( text1, fontFace, fontScale, thickness, &baseline);178baseline += thickness;179Point textOrg((img.cols - textSize.width)/2, (img.rows + textSize.height)/2);180rectangle(img, textOrg + Point(0, baseline), textOrg + Point(textSize.width, -textSize.height), Scalar(0,0,255));181line(img, textOrg + Point(0, thickness), textOrg + Point(textSize.width, thickness), Scalar(0, 0, 255));182putText(img, text1, textOrg, fontFace, fontScale, Scalar(150,0,150), thickness, 8);183184string text2 = "abcdefghijklmnopqrstuvwxyz1234567890";185Scalar color(200,0,0);186fontScale = 0.5, thickness = 1;187int dist = 5;188189textSize = getTextSize( text2, FONT_HERSHEY_SIMPLEX, fontScale, thickness, &baseline);190textOrg = Point(5,5)+Point(0,textSize.height+dist);191putText(img, text2, textOrg, FONT_HERSHEY_SIMPLEX, fontScale, color, thickness, CV_AA);192193fontScale = 1;194textSize = getTextSize( text2, FONT_HERSHEY_PLAIN, fontScale, thickness, &baseline);195textOrg += Point(0,textSize.height+dist);196putText(img, text2, textOrg, FONT_HERSHEY_PLAIN, fontScale, color, thickness, CV_AA);197198fontScale = 0.5;199textSize = getTextSize( text2, FONT_HERSHEY_DUPLEX, fontScale, thickness, &baseline);200textOrg += Point(0,textSize.height+dist);201putText(img, text2, textOrg, FONT_HERSHEY_DUPLEX, fontScale, color, thickness, CV_AA);202203textSize = getTextSize( text2, FONT_HERSHEY_COMPLEX, fontScale, thickness, &baseline);204textOrg += Point(0,textSize.height+dist);205putText(img, text2, textOrg, FONT_HERSHEY_COMPLEX, fontScale, color, thickness, CV_AA);206207textSize = getTextSize( text2, FONT_HERSHEY_TRIPLEX, fontScale, thickness, &baseline);208textOrg += Point(0,textSize.height+dist);209putText(img, text2, textOrg, FONT_HERSHEY_TRIPLEX, fontScale, color, thickness, CV_AA);210211fontScale = 1;212textSize = getTextSize( text2, FONT_HERSHEY_COMPLEX_SMALL, fontScale, thickness, &baseline);213textOrg += Point(0,180) + Point(0,textSize.height+dist);214putText(img, text2, textOrg, FONT_HERSHEY_COMPLEX_SMALL, fontScale, color, thickness, CV_AA);215216textSize = getTextSize( text2, FONT_HERSHEY_SCRIPT_SIMPLEX, fontScale, thickness, &baseline);217textOrg += Point(0,textSize.height+dist);218putText(img, text2, textOrg, FONT_HERSHEY_SCRIPT_SIMPLEX, fontScale, color, thickness, CV_AA);219220textSize = getTextSize( text2, FONT_HERSHEY_SCRIPT_COMPLEX, fontScale, thickness, &baseline);221textOrg += Point(0,textSize.height+dist);222putText(img, text2, textOrg, FONT_HERSHEY_SCRIPT_COMPLEX, fontScale, color, thickness, CV_AA);223224dist = 15, fontScale = 0.5;225textSize = getTextSize( text2, FONT_ITALIC, fontScale, thickness, &baseline);226textOrg += Point(0,textSize.height+dist);227putText(img, text2, textOrg, FONT_ITALIC, fontScale, color, thickness, CV_AA);228}229230int CV_DrawingTest_CPP::checkLineIterator( Mat& img )231{232LineIterator it( img, Point(0,300), Point(1000, 300) );233for(int i = 0; i < it.count; ++it, i++ )234{235Vec3b v = (Vec3b)(*(*it)) - img.at<Vec3b>(300,i);236float err = (float)cvtest::norm( v, NORM_L2 );237if( err != 0 )238{239ts->printf( ts->LOG, "LineIterator works incorrect" );240ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT);241}242}243ts->set_failed_test_info(cvtest::TS::OK);244return 0;245}246247class CV_DrawingTest_C : public CV_DrawingTest248{249public:250CV_DrawingTest_C() {}251protected:252virtual void draw( Mat& img );253virtual int checkLineIterator( Mat& img);254};255256void CV_DrawingTest_C::draw( Mat& _img )257{258CvSize imgSize = cvSize(600, 400);259_img.create( imgSize, CV_8UC3 );260CvMat img = cvMat(_img);261262vector<CvPoint> polyline(4);263polyline[0] = cvPoint(0, 0);264polyline[1] = cvPoint(imgSize.width, 0);265polyline[2] = cvPoint(imgSize.width, imgSize.height);266polyline[3] = cvPoint(0, imgSize.height);267CvPoint* pts = &polyline[0];268int n = (int)polyline.size();269int actualSize = 0;270cvFillPoly( &img, &pts, &n, 1, cvScalar(255,255,255) );271272CvPoint p1 = cvPoint(1,1), p2 = cvPoint(3,3);273if( cvClipLine(imgSize, &p1, &p2) )274cvCircle( &img, cvPoint(300,100), 40, cvScalar(0,0,255), 3 ); // draw275276p1 = cvPoint(1,1), p2 = cvPoint(3,imgSize.height+1000);277if( cvClipLine(imgSize, &p1, &p2) )278cvCircle( &img, cvPoint(500,300), 50, cvScalar(255,0,0), 5, 8, 1 ); // draw279280p1 = cvPoint(imgSize.width,1), p2 = cvPoint(imgSize.width,3);281if( cvClipLine(imgSize, &p1, &p2) )282cvCircle( &img, cvPoint(390,100), 10, cvScalar(0,0,255), 3 ); // not draw283284p1 = cvPoint(imgSize.width-1,1), p2 = cvPoint(imgSize.width,3);285if( cvClipLine(imgSize, &p1, &p2) )286cvEllipse( &img, cvPoint(390,100), cvSize(20,30), 60, 0, 220.0, cvScalar(0,200,0), 4 ); //draw287288CvBox2D box;289box.center.x = 100;290box.center.y = 200;291box.size.width = 200;292box.size.height = 100;293box.angle = 160;294cvEllipseBox( &img, box, cvScalar(200,200,255), 5 );295296polyline.resize(9);297pts = &polyline[0];298n = (int)polyline.size();299actualSize = cvEllipse2Poly( cvPoint(430,180), cvSize(100,150), 30, 0, 150, &polyline[0], 20 );300CV_Assert(actualSize == n);301cvPolyLine( &img, &pts, &n, 1, false, cvScalar(0,0,150), 4, CV_AA );302n = 0;303for( vector<CvPoint>::const_iterator it = polyline.begin(); n < (int)polyline.size()-1; ++it, n++ )304{305cvLine( &img, *it, *(it+1), cvScalar(50,250,100) );306}307308polyline.resize(19);309pts = &polyline[0];310n = (int)polyline.size();311actualSize = cvEllipse2Poly( cvPoint(500,300), cvSize(50,80), 0, 0, 180, &polyline[0], 10 );312CV_Assert(actualSize == n);313cvPolyLine( &img, &pts, &n, 1, true, cvScalar(100,200,100), 20 );314cvFillConvexPoly( &img, pts, n, cvScalar(0, 80, 0) );315316polyline.resize(8);317// external rectengular318polyline[0] = cvPoint(500, 20);319polyline[1] = cvPoint(580, 20);320polyline[2] = cvPoint(580, 100);321polyline[3] = cvPoint(500, 100);322// internal rectangular323polyline[4] = cvPoint(520, 40);324polyline[5] = cvPoint(560, 40);325polyline[6] = cvPoint(560, 80);326polyline[7] = cvPoint(520, 80);327CvPoint* ppts[] = {&polyline[0], &polyline[0]+4};328int pn[] = {4, 4};329cvFillPoly( &img, ppts, pn, 2, cvScalar(100, 100, 0), 8, 0 );330331cvRectangle( &img, cvPoint(0, 300), cvPoint(50, 398), cvScalar(0,0,255) );332333string text1 = "OpenCV";334CvFont font;335cvInitFont( &font, FONT_HERSHEY_SCRIPT_SIMPLEX, 2, 2, 0, 3 );336int baseline = 0;337CvSize textSize = {0, 0};338cvGetTextSize( text1.c_str(), &font, &textSize, &baseline );339baseline += font.thickness;340CvPoint textOrg = cvPoint((imgSize.width - textSize.width)/2, (imgSize.height + textSize.height)/2);341cvRectangle( &img, cvPoint( textOrg.x, textOrg.y + baseline),342cvPoint(textOrg.x + textSize.width, textOrg.y - textSize.height), cvScalar(0,0,255));343cvLine( &img, cvPoint(textOrg.x, textOrg.y + font.thickness),344cvPoint(textOrg.x + textSize.width, textOrg.y + font.thickness), cvScalar(0, 0, 255));345cvPutText( &img, text1.c_str(), textOrg, &font, cvScalar(150,0,150) );346347int dist = 5;348string text2 = "abcdefghijklmnopqrstuvwxyz1234567890";349CvScalar color = cvScalar(200,0,0);350cvInitFont( &font, FONT_HERSHEY_SIMPLEX, 0.5, 0.5, 0, 1, CV_AA );351cvGetTextSize( text2.c_str(), &font, &textSize, &baseline );352textOrg = cvPoint(5, 5+textSize.height+dist);353cvPutText(&img, text2.c_str(), textOrg, &font, color );354355cvInitFont( &font, FONT_HERSHEY_PLAIN, 1, 1, 0, 1, CV_AA );356cvGetTextSize( text2.c_str(), &font, &textSize, &baseline );357textOrg = cvPoint(textOrg.x,textOrg.y+textSize.height+dist);358cvPutText(&img, text2.c_str(), textOrg, &font, color );359360cvInitFont( &font, FONT_HERSHEY_DUPLEX, 0.5, 0.5, 0, 1, CV_AA );361cvGetTextSize( text2.c_str(), &font, &textSize, &baseline );362textOrg = cvPoint(textOrg.x,textOrg.y+textSize.height+dist);363cvPutText(&img, text2.c_str(), textOrg, &font, color );364365cvInitFont( &font, FONT_HERSHEY_COMPLEX, 0.5, 0.5, 0, 1, CV_AA );366cvGetTextSize( text2.c_str(), &font, &textSize, &baseline );367textOrg = cvPoint(textOrg.x,textOrg.y+textSize.height+dist);368cvPutText(&img, text2.c_str(), textOrg, &font, color );369370cvInitFont( &font, FONT_HERSHEY_TRIPLEX, 0.5, 0.5, 0, 1, CV_AA );371cvGetTextSize( text2.c_str(), &font, &textSize, &baseline );372textOrg = cvPoint(textOrg.x,textOrg.y+textSize.height+dist);373cvPutText(&img, text2.c_str(), textOrg, &font, color );374375cvInitFont( &font, FONT_HERSHEY_COMPLEX_SMALL, 1, 1, 0, 1, CV_AA );376cvGetTextSize( text2.c_str(), &font, &textSize, &baseline );377textOrg = cvPoint(textOrg.x,textOrg.y+textSize.height+dist + 180);378cvPutText(&img, text2.c_str(), textOrg, &font, color );379380cvInitFont( &font, FONT_HERSHEY_SCRIPT_SIMPLEX, 1, 1, 0, 1, CV_AA );381cvGetTextSize( text2.c_str(), &font, &textSize, &baseline );382textOrg = cvPoint(textOrg.x,textOrg.y+textSize.height+dist);383cvPutText(&img, text2.c_str(), textOrg, &font, color );384385cvInitFont( &font, FONT_HERSHEY_SCRIPT_COMPLEX, 1, 1, 0, 1, CV_AA );386cvGetTextSize( text2.c_str(), &font, &textSize, &baseline );387textOrg = cvPoint(textOrg.x,textOrg.y+textSize.height+dist);388cvPutText(&img, text2.c_str(), textOrg, &font, color );389390dist = 15;391cvInitFont( &font, FONT_ITALIC, 0.5, 0.5, 0, 1, CV_AA );392cvGetTextSize( text2.c_str(), &font, &textSize, &baseline );393textOrg = cvPoint(textOrg.x,textOrg.y+textSize.height+dist);394cvPutText(&img, text2.c_str(), textOrg, &font, color );395}396397int CV_DrawingTest_C::checkLineIterator( Mat& _img )398{399CvLineIterator it;400CvMat img = cvMat(_img);401int count = cvInitLineIterator( &img, cvPoint(0,300), cvPoint(1000, 300), &it );402for(int i = 0; i < count; i++ )403{404Vec3b v = (Vec3b)(*(it.ptr)) - _img.at<Vec3b>(300,i);405float err = (float)cvtest::norm( v, NORM_L2 );406if( err != 0 )407{408ts->printf( ts->LOG, "CvLineIterator works incorrect" );409ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT);410}411CV_NEXT_LINE_POINT(it);412}413ts->set_failed_test_info(cvtest::TS::OK);414return 0;415}416417class CV_DrawingTest_Far : public CV_DrawingTest_CPP418{419public:420CV_DrawingTest_Far() {}421protected:422virtual void draw(Mat& img);423};424425void CV_DrawingTest_Far::draw(Mat& img)426{427Size imgSize(32768 + 600, 400);428img.create(imgSize, CV_8UC3);429430vector<Point> polyline(4);431polyline[0] = Point(32768 + 0, 0);432polyline[1] = Point(imgSize.width, 0);433polyline[2] = Point(imgSize.width, imgSize.height);434polyline[3] = Point(32768 + 0, imgSize.height);435const Point* pts = &polyline[0];436int n = (int)polyline.size();437fillPoly(img, &pts, &n, 1, Scalar::all(255));438439Point p1(32768 + 1, 1), p2(32768 + 3, 3);440if (clipLine(Rect(32768 + 0, 0, imgSize.width, imgSize.height), p1, p2) && clipLine(imgSize, p1, p2))441circle(img, Point(32768 + 300, 100), 40, Scalar(0, 0, 255), 3); // draw442443p2 = Point(32768 + 3, imgSize.height + 1000);444if (clipLine(Rect(32768 + 0, 0, imgSize.width, imgSize.height), p1, p2) && clipLine(imgSize, p1, p2))445circle(img, Point(65536 + 500, 300), 50, cvColorToScalar(255, CV_8UC3), 5, 8, 1); // draw446447p1 = Point(imgSize.width, 1), p2 = Point(imgSize.width, 3);448if (clipLine(Rect(32768 + 0, 0, imgSize.width, imgSize.height), p1, p2) && clipLine(imgSize, p1, p2))449circle(img, Point(32768 + 390, 100), 10, Scalar(0, 0, 255), 3); // not draw450451p1 = Point(imgSize.width - 1, 1), p2 = Point(imgSize.width, 3);452if (clipLine(Rect(32768 + 0, 0, imgSize.width, imgSize.height), p1, p2) && clipLine(imgSize, p1, p2))453ellipse(img, Point(32768 + 390, 100), Size(20, 30), 60, 0, 220.0, Scalar(0, 200, 0), 4); //draw454455ellipse(img, RotatedRect(Point(32768 + 100, 200), Size(200, 100), 160), Scalar(200, 200, 255), 5);456457polyline.clear();458ellipse2Poly(Point(32768 + 430, 180), Size(100, 150), 30, 0, 150, 20, polyline);459pts = &polyline[0];460n = (int)polyline.size();461polylines(img, &pts, &n, 1, false, Scalar(0, 0, 150), 4, CV_AA);462n = 0;463for (vector<Point>::const_iterator it = polyline.begin(); n < (int)polyline.size() - 1; ++it, n++)464{465line(img, *it, *(it + 1), Scalar(50, 250, 100));466}467468polyline.clear();469ellipse2Poly(Point(32768 + 500, 300), Size(50, 80), 0, 0, 180, 10, polyline);470pts = &polyline[0];471n = (int)polyline.size();472polylines(img, &pts, &n, 1, true, Scalar(100, 200, 100), 20);473fillConvexPoly(img, pts, n, Scalar(0, 80, 0));474475polyline.resize(8);476// external rectengular477polyline[0] = Point(32768 + 0, 0);478polyline[1] = Point(32768 + 80, 0);479polyline[2] = Point(32768 + 80, 80);480polyline[3] = Point(32768 + 0, 80);481// internal rectangular482polyline[4] = Point(32768 + 20, 20);483polyline[5] = Point(32768 + 60, 20);484polyline[6] = Point(32768 + 60, 60);485polyline[7] = Point(32768 + 20, 60);486const Point* ppts[] = { &polyline[0], &polyline[0] + 4 };487int pn[] = { 4, 4 };488fillPoly(img, ppts, pn, 2, Scalar(100, 100, 0), 8, 0, Point(500, 20));489490rectangle(img, Point(32768 + 0, 300), Point(32768 + 50, 398), Scalar(0, 0, 255));491492string text1 = "OpenCV";493int baseline = 0, thickness = 3, fontFace = FONT_HERSHEY_SCRIPT_SIMPLEX;494float fontScale = 2;495Size textSize = getTextSize(text1, fontFace, fontScale, thickness, &baseline);496baseline += thickness;497Point textOrg((32768 + img.cols - textSize.width) / 2, (img.rows + textSize.height) / 2);498rectangle(img, textOrg + Point(0, baseline), textOrg + Point(textSize.width, -textSize.height), Scalar(0, 0, 255));499line(img, textOrg + Point(0, thickness), textOrg + Point(textSize.width, thickness), Scalar(0, 0, 255));500putText(img, text1, textOrg, fontFace, fontScale, Scalar(150, 0, 150), thickness, 8);501502string text2 = "abcdefghijklmnopqrstuvwxyz1234567890";503Scalar color(200, 0, 0);504fontScale = 0.5, thickness = 1;505int dist = 5;506507textSize = getTextSize(text2, FONT_HERSHEY_SIMPLEX, fontScale, thickness, &baseline);508textOrg = Point(32768 + 5, 5) + Point(0, textSize.height + dist);509putText(img, text2, textOrg, FONT_HERSHEY_SIMPLEX, fontScale, color, thickness, CV_AA);510511fontScale = 1;512textSize = getTextSize(text2, FONT_HERSHEY_PLAIN, fontScale, thickness, &baseline);513textOrg += Point(0, textSize.height + dist);514putText(img, text2, textOrg, FONT_HERSHEY_PLAIN, fontScale, color, thickness, CV_AA);515516fontScale = 0.5;517textSize = getTextSize(text2, FONT_HERSHEY_DUPLEX, fontScale, thickness, &baseline);518textOrg += Point(0, textSize.height + dist);519putText(img, text2, textOrg, FONT_HERSHEY_DUPLEX, fontScale, color, thickness, CV_AA);520521textSize = getTextSize(text2, FONT_HERSHEY_COMPLEX, fontScale, thickness, &baseline);522textOrg += Point(0, textSize.height + dist);523putText(img, text2, textOrg, FONT_HERSHEY_COMPLEX, fontScale, color, thickness, CV_AA);524525textSize = getTextSize(text2, FONT_HERSHEY_TRIPLEX, fontScale, thickness, &baseline);526textOrg += Point(0, textSize.height + dist);527putText(img, text2, textOrg, FONT_HERSHEY_TRIPLEX, fontScale, color, thickness, CV_AA);528529fontScale = 1;530textSize = getTextSize(text2, FONT_HERSHEY_COMPLEX_SMALL, fontScale, thickness, &baseline);531textOrg += Point(0, 180) + Point(0, textSize.height + dist);532putText(img, text2, textOrg, FONT_HERSHEY_COMPLEX_SMALL, fontScale, color, thickness, CV_AA);533534textSize = getTextSize(text2, FONT_HERSHEY_SCRIPT_SIMPLEX, fontScale, thickness, &baseline);535textOrg += Point(0, textSize.height + dist);536putText(img, text2, textOrg, FONT_HERSHEY_SCRIPT_SIMPLEX, fontScale, color, thickness, CV_AA);537538textSize = getTextSize(text2, FONT_HERSHEY_SCRIPT_COMPLEX, fontScale, thickness, &baseline);539textOrg += Point(0, textSize.height + dist);540putText(img, text2, textOrg, FONT_HERSHEY_SCRIPT_COMPLEX, fontScale, color, thickness, CV_AA);541542dist = 15, fontScale = 0.5;543textSize = getTextSize(text2, FONT_ITALIC, fontScale, thickness, &baseline);544textOrg += Point(0, textSize.height + dist);545putText(img, text2, textOrg, FONT_ITALIC, fontScale, color, thickness, CV_AA);546547img = img(Rect(32768, 0, 600, 400)).clone();548}549550TEST(Drawing, cpp_regression) { CV_DrawingTest_CPP test; test.safe_run(); }551TEST(Drawing, c_regression) { CV_DrawingTest_C test; test.safe_run(); }552TEST(Drawing, far_regression) { CV_DrawingTest_Far test; test.safe_run(); }553554class CV_FillConvexPolyTest : public cvtest::BaseTest555{556public:557CV_FillConvexPolyTest() {}558~CV_FillConvexPolyTest() {}559protected:560void run(int)561{562vector<Point> line1;563vector<Point> line2;564565line1.push_back(Point(1, 1));566line1.push_back(Point(5, 1));567line1.push_back(Point(5, 8));568line1.push_back(Point(1, 8));569570line2.push_back(Point(2, 2));571line2.push_back(Point(10, 2));572line2.push_back(Point(10, 16));573line2.push_back(Point(2, 16));574575Mat gray0(10,10,CV_8U, Scalar(0));576fillConvexPoly(gray0, line1, Scalar(255), 8, 0);577int nz1 = countNonZero(gray0);578579fillConvexPoly(gray0, line2, Scalar(0), 8, 1);580int nz2 = countNonZero(gray0)/255;581582CV_Assert( nz1 == 40 && nz2 == 0 );583}584};585586TEST(Drawing, fillconvexpoly_clipping) { CV_FillConvexPolyTest test; test.safe_run(); }587588class CV_DrawingTest_UTF8 : public cvtest::BaseTest589{590public:591CV_DrawingTest_UTF8() {}592~CV_DrawingTest_UTF8() {}593protected:594void run(int)595{596vector<string> lines;597lines.push_back("abcdefghijklmnopqrstuvwxyz1234567890");598// cyrillic letters small599lines.push_back("\xD0\xB0\xD0\xB1\xD0\xB2\xD0\xB3\xD0\xB4\xD0\xB5\xD1\x91\xD0\xB6\xD0\xB7"600"\xD0\xB8\xD0\xB9\xD0\xBA\xD0\xBB\xD0\xBC\xD0\xBD\xD0\xBE\xD0\xBF\xD1\x80"601"\xD1\x81\xD1\x82\xD1\x83\xD1\x84\xD1\x85\xD1\x86\xD1\x87\xD1\x88\xD1\x89"602"\xD1\x8A\xD1\x8B\xD1\x8C\xD1\x8D\xD1\x8E\xD1\x8F");603// cyrillic letters capital604lines.push_back("\xD0\x90\xD0\x91\xD0\x92\xD0\x93\xD0\x94\xD0\x95\xD0\x81\xD0\x96\xD0\x97"605"\xD0\x98\xD0\x99\xD0\x9A\xD0\x9B\xD0\x9C\xD0\x9D\xD0\x9E\xD0\x9F\xD0\xA0"606"\xD0\xA1\xD0\xA2\xD0\xA3\xD0\xA4\xD0\xA5\xD0\xA6\xD0\xA7\xD0\xA8\xD0\xA9"607"\xD0\xAA\xD0\xAB\xD0\xAC\xD0\xAD\xD0\xAE\xD0\xAF");608// bounds609lines.push_back("-\xD0\x80-\xD0\x8E-\xD0\x8F-");610lines.push_back("-\xD1\x90-\xD1\x91-\xD1\xBF-");611// bad utf8612lines.push_back("-\x81-\x82-\x83-");613lines.push_back("--\xF0--");614lines.push_back("-\xF0");615616vector<int> fonts;617fonts.push_back(FONT_HERSHEY_SIMPLEX);618fonts.push_back(FONT_HERSHEY_PLAIN);619fonts.push_back(FONT_HERSHEY_DUPLEX);620fonts.push_back(FONT_HERSHEY_COMPLEX);621fonts.push_back(FONT_HERSHEY_TRIPLEX);622fonts.push_back(FONT_HERSHEY_COMPLEX_SMALL);623fonts.push_back(FONT_HERSHEY_SCRIPT_SIMPLEX);624fonts.push_back(FONT_HERSHEY_SCRIPT_COMPLEX);625626vector<Mat> results;627Size bigSize(0, 0);628for (vector<int>::const_iterator font = fonts.begin(); font != fonts.end(); ++font)629{630for (int italic = 0; italic <= FONT_ITALIC; italic += FONT_ITALIC)631{632for (vector<string>::const_iterator line = lines.begin(); line != lines.end(); ++line)633{634const float fontScale = 1;635const int thickness = 1;636const Scalar color(20,20,20);637int baseline = 0;638639Size textSize = getTextSize(*line, *font | italic, fontScale, thickness, &baseline);640Point textOrg(0, textSize.height + 2);641Mat img(textSize + Size(0, baseline), CV_8UC3, Scalar(255, 255, 255));642putText(img, *line, textOrg, *font | italic, fontScale, color, thickness, CV_AA);643644results.push_back(img);645bigSize.width = max(bigSize.width, img.size().width);646bigSize.height += img.size().height + 1;647}648}649}650651int shift = 0;652Mat result(bigSize, CV_8UC3, Scalar(100, 100, 100));653for (vector<Mat>::const_iterator img = results.begin(); img != results.end(); ++img)654{655Rect roi(Point(0, shift), img->size());656Mat sub(result, roi);657img->copyTo(sub);658shift += img->size().height + 1;659}660//imwrite("/tmp/all_fonts.png", result);661}662};663664TEST(Drawing, utf8_support) { CV_DrawingTest_UTF8 test; test.safe_run(); }665666667TEST(Drawing, _914)668{669const int rows = 256;670const int cols = 256;671672Mat img(rows, cols, CV_8UC1, Scalar(255));673674line(img, Point(0, 10), Point(255, 10), Scalar(0), 2, 4);675line(img, Point(-5, 20), Point(260, 20), Scalar(0), 2, 4);676line(img, Point(10, 0), Point(10, 255), Scalar(0), 2, 4);677678double x0 = 0.0/pow(2.0, -2.0);679double x1 = 255.0/pow(2.0, -2.0);680double y = 30.5/pow(2.0, -2.0);681682line(img, Point(int(x0), int(y)), Point(int(x1), int(y)), Scalar(0), 2, 4, 2);683684int pixelsDrawn = rows*cols - countNonZero(img);685ASSERT_EQ( (3*rows + cols)*3 - 3*9, pixelsDrawn);686}687688TEST(Drawing, polylines_empty)689{690Mat img(100, 100, CV_8UC1, Scalar(0));691vector<Point> pts; // empty692polylines(img, pts, false, Scalar(255));693int cnt = countNonZero(img);694ASSERT_EQ(cnt, 0);695}696697TEST(Drawing, polylines)698{699Mat img(100, 100, CV_8UC1, Scalar(0));700vector<Point> pts;701pts.push_back(Point(0, 0));702pts.push_back(Point(20, 0));703polylines(img, pts, false, Scalar(255));704int cnt = countNonZero(img);705ASSERT_EQ(cnt, 21);706}707708TEST(Drawing, longline)709{710Mat mat = Mat::zeros(256, 256, CV_8UC1);711712line(mat, cv::Point(34, 204), cv::Point(46400, 47400), cv::Scalar(255), 3);713EXPECT_EQ(310, cv::countNonZero(mat));714715Point pt[6];716pt[0].x = 32;717pt[0].y = 204;718pt[1].x = 34;719pt[1].y = 202;720pt[2].x = 87;721pt[2].y = 255;722pt[3].x = 82;723pt[3].y = 255;724pt[4].x = 37;725pt[4].y = 210;726pt[5].x = 37;727pt[5].y = 209;728fillConvexPoly(mat, pt, 6, cv::Scalar(0));729730EXPECT_EQ(0, cv::countNonZero(mat));731}732733734TEST(Drawing, putText_no_garbage)735{736Size sz(640, 480);737Mat mat = Mat::zeros(sz, CV_8UC1);738739mat = Scalar::all(0);740putText(mat, "029", Point(10, 350), 0, 10, Scalar(128), 15);741742EXPECT_EQ(0, cv::countNonZero(mat(Rect(0, 0, 10, sz.height))));743EXPECT_EQ(0, cv::countNonZero(mat(Rect(sz.width-10, 0, 10, sz.height))));744EXPECT_EQ(0, cv::countNonZero(mat(Rect(205, 0, 10, sz.height))));745EXPECT_EQ(0, cv::countNonZero(mat(Rect(405, 0, 10, sz.height))));746}747748749TEST(Drawing, line)750{751Mat mat = Mat::zeros(Size(100,100), CV_8UC1);752753ASSERT_THROW(line(mat, Point(1,1),Point(99,99),Scalar(255),0), cv::Exception);754}755756}} // namespace757758759