参考学习opencv3编程入门:学习了绘制几种图形的函数方法以及具体应用:具体看代码。
#include
#include
#include
using namespace cv;
#define WINDOW_NAME1 "【绘制图像1】"
#define WINDOW_NAME2 "【绘制图像2】"
#define WINDOW_WIDTH 600
//实现绘制不同角度、相同尺寸的椭圆
void DrawEllipse(Mat img, double angle)
{
int thickness = 2;
int lineType = 8;
ellipse(img, //将椭圆画在img上
Point(WINDOW_WIDTH/2, WINDOW_WIDTH/2), //椭圆中心
Size(WINDOW_WIDTH/4, WINDOW_WIDTH/16), //并且大小位于矩形内
angle, //椭圆旋转角度为angle
0, 360, //扩展弧度
Scalar(255, 129, 0), //图形颜色
thickness, //线宽
lineType); //线型
}
//实现实心圆的绘制
void DrawFilledCircle(Mat img, Point center)
{
int thickness = -1;
int lineType = 8;
circle(
img, //将实心圆画在img上
center, //圆心由center定义
WINDOW_WIDTH/32, //圆的半径
Scalar(0, 0, 255), //圆的颜色
thickness, //线宽定义为thickness = -1,绘制的是实心的。
lineType //线型 8 为联通线型
);
}
//实现凹多边形的绘制
void DrawPolygon(Mat img)
{
int lineType = 8;
//创建一些点
Point rookPoints[1][20];
rookPoints[0][0] = Point(WINDOW_WIDTH/4, 7*WINDOW_WIDTH/8);
rookPoints[0][1] = Point(3*WINDOW_WIDTH/4, 7*WINDOW_WIDTH/8);
rookPoints[0][2] = Point(3*WINDOW_WIDTH/4, 13*WINDOW_WIDTH/16);
rookPoints[0][3] = Point(11*WINDOW_WIDTH/16, 13*WINDOW_WIDTH/16);
rookPoints[0][4] = Point(19*WINDOW_WIDTH/32, 3*WINDOW_WIDTH/8);
rookPoints[0][5] = Point(3*WINDOW_WIDTH/4, 3*WINDOW_WIDTH/8);
rookPoints[0][6] = Point(3*WINDOW_WIDTH/4, WINDOW_WIDTH/8);
rookPoints[0][7] = Point(26*WINDOW_WIDTH/40,WINDOW_WIDTH/8);
rookPoints[0][8] = Point(26*WINDOW_WIDTH/40,WINDOW_WIDTH/4);
rookPoints[0][9] = Point(22*WINDOW_WIDTH/40,WINDOW_WIDTH/4);
rookPoints[0][10] = Point(22*WINDOW_WIDTH/40,WINDOW_WIDTH/8);
rookPoints[0][11] = Point(18*WINDOW_WIDTH/40,WINDOW_WIDTH/8);
rookPoints[0][12] = Point(18*WINDOW_WIDTH/40,WINDOW_WIDTH/4);
rookPoints[0][13] = Point(14*WINDOW_WIDTH/40,WINDOW_WIDTH/4);
rookPoints[0][14] = Point(26*WINDOW_WIDTH/40,WINDOW_WIDTH/8);
rookPoints[0][15] = Point(WINDOW_WIDTH/4,WINDOW_WIDTH/8);
rookPoints[0][16] = Point(WINDOW_WIDTH/4,3*WINDOW_WIDTH/8);
rookPoints[0][17] = Point(13*WINDOW_WIDTH/32,3*WINDOW_WIDTH/8);
rookPoints[0][18] = Point(5*WINDOW_WIDTH/16,13*WINDOW_WIDTH/16);
rookPoints[0][19] = Point(WINDOW_WIDTH/4,13*WINDOW_WIDTH/16);
const Point * ppt[1] = {rookPoints[0]};
int npt[] = {20};
fillPoly(
img, //将多边形画到图像img上
ppt, //多边形的顶点集为ppt
npt, //要绘制的顶点数目
1, //要绘制多边形的数量
Scalar(255,255,255), //多边形颜色定义
lineType
);
}
//实现线的绘制函数
void DrawLine(Mat img, Point start, Point end)
{
int thickness = 2;
int lineType = 8;
line(
img,
start,
end,
Scalar(0, 0, 0), //代表黑色
thickness,
lineType
);
}
int main()
{
//创建空白的Mat图像
Mat atomImage = Mat::zeros(WINDOW_WIDTH, WINDOW_WIDTH, CV_8UC3);
Mat rookImage = Mat::zeros(WINDOW_WIDTH, WINDOW_WIDTH, CV_8UC3);
//绘制椭圆
DrawEllipse(atomImage, 90);
DrawEllipse(atomImage, 0);
DrawEllipse(atomImage, 45);
DrawEllipse(atomImage, -45);
//绘制圆心
DrawFilledCircle(atomImage, Point(WINDOW_WIDTH/2, WINDOW_WIDTH/2));
//绘制椭圆
DrawPolygon(rookImage);
//绘制矩形
rectangle(rookImage, Point(0, 7*WINDOW_WIDTH/8), Point(WINDOW_WIDTH, WINDOW_WIDTH), Scalar(0, 255, 255), -1, 8);
//绘制一些线段
DrawLine(rookImage, Point(0, 15*WINDOW_WIDTH/16),Point(0, 15*WINDOW_WIDTH/16) );
imshow(WINDOW_NAME1, atomImage);
moveWindow(WINDOW_NAME1, 0, 200);
imwrite("yuanzi.jpg",atomImage);
imshow(WINDOW_NAME2, rookImage);
moveWindow(WINDOW_NAME2, WINDOW_WIDTH, 200);
imwrite("gege.jpg", rookImage);
waitKey(0);
return 0;
}