本文主要讲解“OpenCV的基本绘图功能有哪些”。感兴趣的朋友不妨看看。本文介绍的方法简单、快速、实用。让边肖带你学习“OpenCV的基本绘图功能有哪些”!
直线绘制功能;
用于绘制椭圆的椭圆函数;
矩形函数,用于绘制矩形;
画圆的圆函数;
用于绘制填充多边形的填充多边形功能。
#includeopencv2/core/core.hpp
# includeo penc v2/imgproc/imgproc . HPP
# includeo penc v2/high GUI/high GUI . HPP
使用usingnamespacecv
使用命名空间TD;
#defineWINDOW_WIDTH600//定义窗口大小的宏
#defineWINDOW_NAME1 '原子图'//为窗口标题定义的宏
#defineWINDOW_NAME2 '为窗口标题定义的多边形和线条绘图'//宏
/* * *画一个椭圆* * */
空心画椭圆(Matimg,双角)
{
int thickness=2;//线宽
int linetype=8;//线性,8表示联通线性。
椭圆(img,
点(窗口宽度/2,窗口宽度/2),
尺寸(窗宽/4,窗宽/16),
角度,//角度,0~360度
0,
360,
标量(255,129,0),
厚度,
线型);
}
/*画一个实心圆*/
无效绘制圆角(矩阵,点中心)
{
int thickness=-1;//线条粗细-1,代表实心
int linetype=8;//线性,8表示联通线性。
圆形(img,
中心,
WINDOW_WIDTH/32,
标量(0,0,255),
厚度,
nbsp; lineType);
}
/*凹多边形绘制*/
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(14 * 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,
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(int argc, char ** argv)
{
Mat atomImage = Mat::zeros(WINDOW_WIDTH, WINDOW_WIDTH, CV_8UC3);
Mat rookImage = Mat::zeros(WINDOW_WIDTH, WINDOW_WIDTH, CV_8UC3);
// 椭圆绘制
DrawEllipse(atomImage, 0);
DrawEllipse(atomImage, 90);
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(WINDOW_WIDTH, 15 * WINDOW_WIDTH / 16));
DrawLine(rookImage, Point(WINDOW_WIDTH / 4, 7 * WINDOW_WIDTH / 8), Point(WINDOW_WIDTH / 4, WINDOW_WIDTH));
DrawLine(rookImage, Point(WINDOW_WIDTH / 2, 7 * WINDOW_WIDTH / 8), Point(WINDOW_WIDTH / 2, WINDOW_WIDTH));
DrawLine(rookImage, Point(3 * WINDOW_WIDTH / 4, 7 * WINDOW_WIDTH / 8), Point(3 * WINDOW_WIDTH / 4, WINDOW_WIDTH));
// ---------------------------<3>显示绘制出的图像------------------------
imshow(WINDOW_NAME1, atomImage);
moveWindow(WINDOW_NAME1, 0, 200);
imshow(WINDOW_NAME2, rookImage);
moveWindow(WINDOW_NAME2, WINDOW_WIDTH, 200);
waitKey(0);
return(0);
}
运行:
到此,相信大家对“OpenCV基本图形绘制函数有哪些”有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
内容来源网络,如有侵权,联系删除,本文地址:https://www.230890.com/zhan/144749.html