目录
1.绘制直线line()
2.绘制圆形circle()
3.绘制椭圆形ellipse()
4.绘制矩形rectangle()
5.绘制多边形 fillPoly()
6.绘制文字putText()
7.例子
CV_EXPORTS_W void line(InputOutputArray img, Point pt1, Point pt2, const Scalar& color, int thickness = 1, int lineType = LINE_8, int shift = 0 );
CV_EXPORTS_W void circle(InputOutputArray img, Point center, int radius, const Scalar& color, int thickness = 1, int lineType = LINE_8, int shift = 0);
CV_EXPORTS_W void ellipse(InputOutputArray img, Point center, Size axes, double angle, double startAngle, double endAngle, const Scalar& color, int thickness = 1, int lineType = LINE_8, int shift = 0);
CV_EXPORTS_W void rectangle(InputOutputArray img, Point pt1, Point pt2, const Scalar& color, int thickness = 1, int lineType = LINE_8, int shift = 0);
CV_EXPORTS_W void fillPoly(InputOutputArray img, InputArrayOfArrays pts, const Scalar& color, int lineType = LINE_8, int shift = 0, Point offset = Point() );
CV_EXPORTS_W void putText( InputOutputArray img, const String& text, Point org, int fontFace, double fontScale, Scalar color, int thickness = 1, int lineType = LINE_8, bool bottomLeftOrigin = false );
其中,fontFace
参数指定了要使用的字体类型。下面是一些常用的字体类型选择标志:
FONT_HERSHEY_SIMPLEX
- 正常大小无衬线字体。FONT_HERSHEY_PLAIN
- 正常大小无衬线字体。FONT_HERSHEY_DUPLEX
- 正常大小无衬线字体(更加密集)。FONT_HERSHEY_COMPLEX
- 正常大小有衬线字体。FONT_HERSHEY_TRIPLEX
- 正常大小有衬线字体(更加密集)。FONT_HERSHEY_COMPLEX_SMALL
- 小型字体。FONT_HERSHEY_SCRIPT_SIMPLEX
- 手写风格字体。FONT_HERSHEY_SCRIPT_COMPLEX
- 手写风格字体,复杂版本。FONT_ITALIC
- 斜体字体。 // 生成一个黑色图像用于绘制几何图形
Mat img=Mat::zeros(Size(512,512),CV_8UC3);
//绘制圆形
circle(img,Point(50,50),25,Scalar(255,255,255),-1);//绘制一个实心圆
circle(img,Point(100,50),20,Scalar(255,255,255),4);//绘制一个空心圆
//绘制直线
line(img,Point(100,100),Point(200,100),Scalar(255,255,255),2,LINE_4,0);//绘制一条直线
//绘制椭圆
ellipse(img,Point(300,255),Size(100,70),0,0,100,Scalar(255,255,255),-1);
//绘制矩形
rectangle(img,Point(50,400),Point(100,450),Scalar(125,125,125),-1);
//绘制多边形
Point pp[2][6];
pp[0][0]=Point(72,200);
pp[0][1]=Point(142,204);
pp[0][2]=Point (226,263);
pp[0][3]=Point (172,310);
pp[0][4]=Point (117,319);
pp[0][5]=Point (15,260);
pp[1][0]=Point(359,339);
pp[1][1]=Point(447,351);
pp[1][2]=Point (504,349);
pp[1][3]=Point (484,433);
pp[1][4]=Point (418,449);
pp[1][5]=Point (354,402);
Point pp2[5];
pp2[0]=Point (350,83);
pp2[1]=Point(463,90);
pp2[2]=Point (500,171);
pp2[3]=Point (421,194);
pp2[4]=Point (338,141);
const Point *pts[3]={pp[0],pp[1],pp2};//pts变量的生成
int npts[]={6,6,5};
fillPoly(img,pts,npts,3,Scalar(125,125,125),8);//绘制3个多边形
putText(img,"Learn OpenCV 4",Point(100,400),2,1,Scalar(255,255,255));
imwrite("/sdcard/DCIM/img.jpg",img);