1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > 学习OpenCV2——绘制基本图形及文字

学习OpenCV2——绘制基本图形及文字

时间:2021-12-12 06:54:09

相关推荐

学习OpenCV2——绘制基本图形及文字

OpenCV中可以绘制的图形有直线、矩形、多边形、圆、椭圆。以及一个写文本的函数puttext

1. 基本函数

Line

C++: void line(Mat& img, Point pt1,Point pt2, const Scalar& color, int thickness=1, int lineType=8,int shift=0)

Parameters:

img – 图像.

pt1 – 线条起点.

pt2 – 线条终点.

color – 线条颜色.

thickness – 线条宽度.

lineType – 线型

Type of the line: 8 (or omitted) - 8-连接线.

4 - 4-连接线.

CV_AA - 反走样线条.

shift – 坐标点小数点位数.

Rectangle

C++: void rectangle(Mat& img,Point pt1, Pointpt2, const Scalar&color, intthickness=1,intlineType=8, intshift=0)

C++: void rectangle(Mat& img,Rect rec, const Scalar&color, intthickness=1, intlineType=8,intshift=0 )

Parameters:

img – 画矩形的对象

pt1 – 矩形的一个顶点,左上角的.

pt2 – 另一个顶点,右下角的.

rec – 确定矩形的另一种方式,给左上角坐标和长宽

color – 指定矩形的颜色或亮度(灰度图像),scalar(255,0,255)既可指定.

thickness – 矩形边框的粗细. 负值(like CV_FILLED)表示要画一个填充的矩形

lineType – 边框线型. (8 (or 0) - 8-connected line(8邻接)连接线。

4 - 4-connected line(4邻接)连接线。

CV_AA - antialiased 线条。)

shift –坐标点的小数点位数

PolyLine

C++: void polylines(Mat& img, const Point** pts, const int* npts, int ncontours, bool isClosed, const Scalar& color, int thickness=1, int lineType=8, int shift=0 )

C++: void polylines(InputOutputArray img, InputArrayOfArrays pts, bool isClosed, const Scalar& color, int thickness=1, int lineType=8, int shift=0 )

Parameters:

img – 折线所在图像.

pts – 折线中拐点坐标指针.

npts – 折线拐点个数指针.

ncontours – 折线线段数量.

isClosed – 折线是否闭合.

color – 折线颜色.

thickness – 折线宽度.

lineType – 线型.

shift – 顶点坐标小数点位数.

Circle

C++: void circle(Mat&img, Point center, intradius, const Scalar&color,intthickness=1, intlineType=8, intshift=0)

Parameters:

img – 要画圆的那个矩形.

center – 圆心坐标.

radius – 半径.

color – 圆边框颜色,scalar类型的

thickness – 正值表示圆边框宽度. 负值表示画一个填充圆形

lineType – 圆边框线型

shift – 圆心坐标和半径的小数点位数

Ellipse

C++: void ellipse(Mat& img, Point center,Size axes, double angle, double startAngle, double endAngle, const Scalar& color,int thickness=1, int lineType=8, int shift=0)

C++: void ellipse(Mat& img, constRotatedRect& box, const Scalar& color, int thickness=1, int lineType=8)

Parameters:

img – 椭圆所在图像.

center – 椭圆中心.

axes – 椭圆主轴一半的长度

angle – 椭圆旋转角度

startAngle – 椭圆弧起始角度

endAngle –椭圆弧终止角度

box – 指定椭圆中心和旋转角度的信息,通过 RotatedRect 或 CvBox2D. 这表示椭圆画在旋转矩形上(矩形是不可见的,只是指定了一个框而已)

color – 椭圆边框颜色.

thickness – 正值代表椭圆边框宽度,负值代表填充的椭圆

lineType – 线型

shift – 椭圆中心坐标和坐标轴的小数点位数

PutText

C++: void putText(Mat& img, const string& text, Point org, int fontFace, double fontScale, Scalar color, int thickness=1, int lineType=8, bool bottomLeftOrigin=false )

Parameters:

img – 显示文字所在图像.

text – 待显示的文字.

org – 文字在图像中的左下角 坐标.

font – 字体结构体.

fontFace – 字体类型, 可选择字体:

FONT_HERSHEY_SIMPLEX,FONT_HERSHEY_PLAIN,

FONT_HERSHEY_DUPLEX, FONT_HERSHEY_COMPLEX,

FONT_HERSHEY_TRIPLEX, FONT_HERSHEY_COMPLEX_SMALL,

FONT_HERSHEY_SCRIPT_SIMPLEX, or FONT_HERSHEY_SCRIPT_COMPLEX,

以上所有类型都可以配合 FONT_HERSHEY_ITALIC使用,产生斜体效果。

fontScale – 字体大小,该值和字体内置大小相乘得到字体大小

color – 文本颜色

thickness – 写字的线的粗细

lineType – 线型.

bottomLeftOrigin – true, 图像数据原点在左下角. Otherwise, 图像数据原点在左上角.

2. 一些参数取值情况

lineType: 8或0 8-连接线

4 4-连接线

CV_AA 反走样线条

thickness:>0,线条粗细 <0,填充

fontFace :

CV_FONT_HERSHEY_SIMPLEX 正常尺寸sanserif字体

CV_FONT_HERSHEY_PLAIN 小尺寸sanserif字体

CV_FONT_HERSHEY_DUPLEX 正常尺寸sanserif, 比CV_FONT_HERSHEY_SIMPLEX更复杂

CV_FONT_HERSHEY_COMPLEX 正常尺寸serif, 比CV_FONT_HERSHEY_DUPLEX更复杂

CV_FONT_HERSHEY_TRIPLEX 正常尺寸serif, 比CV_FONT_HERSHEY_COMPLEX更复杂

CV_FONT_HERSHEY_COMPLEX_SMALL 小尺寸的CV_FONT_HERSHEY_COMPLEX

CV_FONT_HERSHEY_SCRIPT_SIMPLEX 手写风格

CV_FONT_HERSHEY_SCRIPT_COMPLEX 比CV_FONT_HERSHEY_SCRIPT_SIMPLEX更复杂的风格

常见颜色RGB值对照表

更全的RGB颜色对照表参见 :/commons?type=3

3. 示例代码

例1.

[cpp]view plaincopy#include<iostream> #include"cv.h" #include"highgui.h" usingnamespacestd; usingnamespacecv; staticvoidhelp() { cout<<"ThisprogramdemonstratesOpenCVdrawingandtextoutputfunctions"<<endl <<"Usage:"<<endl <<"./drawing"<<endl; } staticScalarrandomColor(RNG&rng) { intiColor=unsigned(rng); //255=0xFF returnScalar(iColor&255,(iColor>>8)&255,(iColor>>16)&255);//产生一种颜色 } intmain(intargc,char**argv) { help(); charwndName[]="DrawingDemo"; constintrandomNumber=100; constintDELAY=10; intlineType=CV_AA; intheight=700; intwidth=1000; intx1=-width/2;//-500 intx2=3*width/2;//1500 inty1=-height/2;//-350 inty2=3*height/2;//1050 RNGrng(0xFFFFFFFF); Matimage=Mat::zeros(height,width,CV_8UC3); imshow(wndName,image); waitKey(DELAY); //drawline for(inti=0;i<randomNumber;i++) { Pointpt1,pt2; pt1.x=rng.uniform(x1,x2); pt1.y=rng.uniform(y1,y2); pt2.x=rng.uniform(x1,x2); pt2.y=rng.uniform(y1,y2); line(image,pt1,pt2,randomColor(rng),rng.uniform(1,10),lineType); imshow(wndName,image); if(waitKey(DELAY)>=0) return0; } //drawrectangle for(inti=0;i<randomNumber;i++) { Pointpt1,pt2; pt1.x=rng.uniform(x1,x2); pt1.y=rng.uniform(y1,y2); pt2.x=rng.uniform(x1,x2); pt2.y=rng.uniform(y1,y2); intthickness=rng.uniform(-3,10); /*----------------------drawsasimple,thick,orfilledup-rightrectangle----------- *C++:voidrectangle(Mat&img,Pointpt1,Pointpt2,constScalar&color, intthickness=1,intlineType=8,intshift=0) *C++:voidrectangle(Mat&img,Rectrec,constScalar&color,intthickness=1, intlineType=8,intshift=0) *img–image *pt1–Vertexoftherectangle矩形的一个顶点p1 *pt2–Vertexoftherectangleoppositetopt1矩形的另一个顶点,与p1相对 *rec–Alternativespecificationofthedrawnrectangle *color–Rectanglecolororbrightness(grayscaleimage)线条颜色,对于灰度图是亮度 *thickness–Thicknessoflinesthatmakeuptherectangle.Negativevalues,线条粗细 likeCV_FILLED,meanthatthefunctionhastodrawafilledrectangle *lineType–Typeoftheline.Seetheline()description线条类型 *shift–Numberoffractionalbitsinthepointcoordinates -----------------------------------------------------------------------------*/ rectangle(image,pt1,pt2,randomColor(rng),MAX(thickness,-1),lineType); imshow(wndName,image); if(waitKey(DELAY)>=0) return0; }//drawellipse for(inti=0;i<randomNumber;i++) { Pointcenter; center.x=rng.uniform(x1,x2); center.y=rng.uniform(y1,y2); Sizeaxes; axes.width=rng.uniform(0,200); axes.height=rng.uniform(0,200); doubleangle=rng.uniform(0,180); /*---------drawsasimpleorthickellipticarcorfillsanellipsesector--------- *C++:voidellipse(Mat&img,Pointcenter,Sizeaxes,doubleangle, doublestartAngle,doubleendAngle,constScalar&color, intthickness=1,intlineType=8,intshift=0) *C++:voidellipse(Mat&img,constRotatedRect&box,constScalar&color, intthickness=1,intlineType=8) *img–image *center–Centeroftheellipse椭圆中心 *axes–Halfofthesizeoftheellipsemainaxes椭圆长轴的一半 *angle–Ellipserotationangleindegrees椭圆旋转的角度 *startAngle–Startingangleoftheellipticarcindegrees弧度开始的角度 *endAngle–Endingangleoftheellipticarcindegrees弧度结束的角度 *box–AlternativeellipserepresentationviaRotatedRectorCvBox2D Thismeansthatthefunctiondrawsanellipseinscribedintherotatedrectangle *color–Ellipsecolor *thickness–Thicknessoftheellipsearcoutline,ifpositive.Otherwise,thisindicatesthata filledellipsesectoristobedrawn *lineType–Typeoftheellipseboundary.Seetheline()description *shift–Numberoffractionalbitsinthecoordinatesofthecenterandvaluesofaxes -----------------------------------------------------------------------------*/ ellipse(image,center,axes,angle,angle-100,angle+200,randomColor(rng),rng.uniform(1,8),lineType); imshow(wndName,image); if(waitKey(DELAY)>=0) return0; } //drawpolylines for(inti=0;i<randomNumber;i++) { Pointpt[2][3]; pt[0][0].x=rng.uniform(x1,x2); pt[0][0].y=rng.uniform(y1,y2); pt[0][1].x=rng.uniform(x1,x2); pt[0][1].y=rng.uniform(y1,y2); pt[0][2].x=rng.uniform(x1,x2); pt[0][2].y=rng.uniform(y1,y2); pt[1][0].x=rng.uniform(x1,x2); pt[1][0].y=rng.uniform(y1,y2); pt[1][1].x=rng.uniform(x1,x2); pt[1][1].y=rng.uniform(y1,y2); pt[1][2].x=rng.uniform(x1,x2); pt[1][2].y=rng.uniform(y1,y2); constPoint*ppt[2]={pt[0],pt[1]}; intnpt[]={3,3}; /*-------------------drawsseveralpolygonalcurves---------------------------- *C++:voidpolylines(Mat&img,constPoint**pts,constint*npts,intncontours, boolisClosed,constScalar&color,intthickness=1, intlineType=8,intshift=0) *C++:voidpolylines(InputOutputArrayimg,InputArrayOfArrayspts,boolisClosed, constScalar&color,intthickness=1,intlineType=8,intshift=0) *img–image *pts–Arrayofpolygonalcurves多边形曲线数组 *npts–Arrayofpolygonvertexcounters顶点数组 *ncontours–Numberofcurves曲线数量 *isClosed–Flagindicatingwhetherthedrawnpolylinesareclosedornot Iftheyareclosed,thefunctiondrawsalinefromthelastvertex ofeachcurvetoitsfirstvertex标志曲线是否闭合 *color–Polylinecolor *thickness–Thicknessofthepolylineedges *lineType–Typeofthelinesegments.Seetheline()description *shift–Numberoffractionalbitsinthevertexcoordinates -----------------------------------------------------------------------------*/ polylines(image,ppt,npt,2,TRUE,randomColor(rng),rng.uniform(1,10),lineType); imshow(wndName,image); if(waitKey(DELAY)>=0) return0; } //drawpolygonswithfilledarea for(inti=0;i<randomNumber;i++) { Pointpt[2][3]; pt[0][0].x=rng.uniform(x1,x2); pt[0][0].y=rng.uniform(y1,y2); pt[0][1].x=rng.uniform(x1,x2); pt[0][1].y=rng.uniform(y1,y2); pt[0][2].x=rng.uniform(x1,x2); pt[0][2].y=rng.uniform(y1,y2); pt[1][0].x=rng.uniform(x1,x2); pt[1][0].y=rng.uniform(y1,y2); pt[1][1].x=rng.uniform(x1,x2); pt[1][1].y=rng.uniform(y1,y2); pt[1][2].x=rng.uniform(x1,x2); pt[1][2].y=rng.uniform(y1,y2); constPoint*ppt[2]={pt[0],pt[1]}; intnpt[]={3,3}; /*--------------fillstheareaboundedbyoneormorepolygons--------------- *C++:voidfillPoly(Mat&img,constPoint**pts,constint*npts,intncontours, constScalar&color,intlineType=8,intshift=0,Pointoffset=Point()) *img–image *pts–Arrayofpolygonswhereeachpolygonisrepresentedasanarrayofpoints *npts–Arrayofpolygonvertexcounters *ncontours–Numberofcontoursthatbindthefilledregion *color–Polygoncolor *lineType–Typeofthepolygonboundaries.Seetheline()description *shift–Numberoffractionalbitsinthevertexcoordinates *offset–Optionaloffsetofallpointsofthecontours -----------------------------------------------------------------------------*/ fillPoly(image,ppt,npt,2,randomColor(rng),lineType); imshow(wndName,image); if(waitKey(DELAY)>=0) return0; } //drawcircle for(inti=0;i<randomNumber;i++) { Pointcenter; center.x=rng.uniform(x1,x2); center.y=rng.uniform(y1,y2); /*-----------------------------drawacircle---------------------------------- *C++:voidcircle(Mat&img,Pointcenter,intradius,constScalar&color, intthickness=1,intlineType=8,intshift=0) *img–Imagewherethecircleisdrawn *center–Centerofthecircle *radius–Radiusofthecircle *color–Circlecolor *thickness–Thicknessofthecircleoutline,ifpositive. Negativethicknessmeansthata *filledcircleistobedrawn *lineType–Typeofthecircleboundary.Seetheline()description *shift–Numberoffractionalbitsinthecoordinatesofthecenterand intheradiusvalue -----------------------------------------------------------------------------*/ circle(image,center,rng.uniform(0,300),randomColor(rng),rng.uniform(-1,9),lineType); imshow(wndName,image); if(waitKey(DELAY)>=0) return0; } //puttextontheimage for(inti=0;i<randomNumber;i++) { Pointorg; org.x=rng.uniform(x1,x2); org.y=rng.uniform(y1,y2); putText(image,"Testingtextrendering",org,rng.uniform(0,8)/*fonttype*/, rng.uniform(0,100)*0.05+0.1/*fontscale*/, randomColor(rng),rng.uniform(1,10)/*thickness*/,lineType); imshow(wndName,image); if(waitKey(DELAY)>=0) return0; } /*------------------calculatesthewidthandheightofatextstring-------------- *C++:SizegetTextSize(conststring&text,intfontFace,doublefontScale, intthickness,int*baseLine) *text–Inputtextstring *fontFace–Fonttouse.SeetheputText()fordetails *fontScale–Fontscale.SeetheputText()fordetails *thickness–Thicknessoflinesusedtorenderthetext *baseLine–Outputparameter-y-coordinateofthebaselinerelative tothebottom-mosttextpoint. --------------------------------------------------------------------------------------*/ //stringtext="OpenCVForever!"; //intfontFace=FONT_HERSHEY_COMPLEX; //doublefontScale=2; //intthickness=3; //intbaseline=0; //baseline+=thickness; //SizetextSize=getTextSize(text,fontFace, //fontScale,thickness,&baseline); SizetextSize=getTextSize("OpenCVForever!",FONT_HERSHEY_COMPLEX,3,5,0); Pointorg((width-textSize.width)/2,(height-textSize.height)/2); Matimage2; for(inti=0;i<255;i+=2) { image2=image-Scalar::all(i); putText(image2,"OpenCVForever!",org,FONT_HERSHEY_COMPLEX, 3,Scalar(i,i,255),5,lineType); imshow(wndName,image2); if(waitKey(DELAY)>=0) return0; } waitKey(); return0; } 结果

例2.

[cpp]view plaincopy/** *@fileDrawing_1.cpp *@briefSimplesamplecode */ #include<opencv2/core/core.hpp> #include<opencv2/highgui/highgui.hpp> #definew400 usingnamespacecv; ///Functionheaders voidMyEllipse(Matimg,doubleangle); voidMyFilledCircle(Matimg,Pointcenter); voidMyPolygon(Matimg); voidMyLine(Matimg,Pointstart,Pointend); /** *@functionmain *@briefMainfunction */ intmain(void){ ///Windowsnames charatom_window[]="Drawing1:Atom"; charrook_window[]="Drawing2:Rook"; ///Createblackemptyimages Matatom_image=Mat::zeros(w,w,CV_8UC3); Matrook_image=Mat::zeros(w,w,CV_8UC3); ///1.Drawasimpleatom: ///----------------------- ///1.a.Creatingellipses MyEllipse(atom_image,90); MyEllipse(atom_image,0); MyEllipse(atom_image,45); MyEllipse(atom_image,-45); ///1.b.Creatingcircles MyFilledCircle(atom_image,Point(w/2,w/2)); ///2.Drawarook ///------------------ ///2.a.Createaconvexpolygon MyPolygon(rook_image); ///2.b.Creatingrectangles rectangle(rook_image, Point(0,7*w/8), Point(w,w), Scalar(0,255,255), -1, 8); RotatedRectrRect=RotatedRect(Point2f(100,100),Size2f(100,50),30); ellipse(rook_image,rRect,Scalar(255,255,0)); ///2.c.Createafewlines MyLine(rook_image,Point(0,15*w/16),Point(w,15*w/16)); MyLine(rook_image,Point(w/4,7*w/8),Point(w/4,w)); MyLine(rook_image,Point(w/2,7*w/8),Point(w/2,w)); MyLine(rook_image,Point(3*w/4,7*w/8),Point(3*w/4,w)); ///3.Displayyourstuff! imshow(atom_window,atom_image); moveWindow(atom_window,0,200); imshow(rook_window,rook_image); moveWindow(rook_window,w,200); waitKey(0); return(0); } ///FunctionDeclaration /** *@functionMyEllipse *@briefDrawafixed-sizeellipsewithdifferentangles */ voidMyEllipse(Matimg,doubleangle) { intthickness=2; intlineType=8; ellipse(img, Point(w/2,w/2), Size(w/4,w/16), angle, 0, 360, Scalar(255,0,0), thickness, lineType); } /** *@functionMyFilledCircle *@briefDrawafixed-sizefilledcircle */ voidMyFilledCircle(Matimg,Pointcenter) { intthickness=-1; intlineType=8; circle(img, center, w/32, Scalar(0,0,255), thickness, lineType); } /** *@functionMyPolygon *@functionDrawasimpleconcavepolygon(rook) */ voidMyPolygon(Matimg) { intlineType=8; /**Createsomepoints*/ Pointrook_points[1][20]; rook_points[0][0]=Point(w/4,7*w/8); rook_points[0][1]=Point(3*w/4,7*w/8); rook_points[0][2]=Point(3*w/4,13*w/16); rook_points[0][3]=Point(11*w/16,13*w/16); rook_points[0][4]=Point(19*w/32,3*w/8); rook_points[0][5]=Point(3*w/4,3*w/8); rook_points[0][6]=Point(3*w/4,w/8); rook_points[0][7]=Point(26*w/40,w/8); rook_points[0][8]=Point(26*w/40,w/4); rook_points[0][9]=Point(22*w/40,w/4); rook_points[0][10]=Point(22*w/40,w/8); rook_points[0][11]=Point(18*w/40,w/8); rook_points[0][12]=Point(18*w/40,w/4); rook_points[0][13]=Point(14*w/40,w/4); rook_points[0][14]=Point(14*w/40,w/8); rook_points[0][15]=Point(w/4,w/8); rook_points[0][16]=Point(w/4,3*w/8); rook_points[0][17]=Point(13*w/32,3*w/8); rook_points[0][18]=Point(5*w/16,13*w/16); rook_points[0][19]=Point(w/4,13*w/16); constPoint*ppt[1]={rook_points[0]}; intnpt[]={20}; fillPoly(img, ppt, npt, 1, Scalar(255,255,255), lineType); } /** *@functionMyLine *@briefDrawasimpleline */ voidMyLine(Matimg,Pointstart,Pointend) { intthickness=2; intlineType=8; line(img, start, end, Scalar(0,0,0), thickness, lineType); } 结果

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。