【OpenCV】OpenCV实战从入门到精通之十 -- 教你如何使用OpenCV绘制基本图形
【摘要】 本文是《OpenCV实战从入门到精通》系列之第10篇
【OpenCV】OpenCV实战从入门到精通之一 -- OpenCV宏的讲解
【OpenCV】OpenCV实战从入门到精通之二 -- OpenCV如何进行图像腐蚀操作
【OpenCV】OpenCV实战从入门到精通之三 -- canny边缘检测
【OpenCV】OpenCV实战从入门到精通之四 -- 常用的函数讲解(...
本文是《OpenCV实战从入门到精通》系列之第10篇
【OpenCV】OpenCV实战从入门到精通之一 -- OpenCV宏的讲解
【OpenCV】OpenCV实战从入门到精通之二 -- OpenCV如何进行图像腐蚀操作
【OpenCV】OpenCV实战从入门到精通之三 -- canny边缘检测
【OpenCV】OpenCV实战从入门到精通之四 -- 常用的函数讲解(mat、imread、imshow、imwrite、nameWindow)
【OpenCV】OpenCV实战从入门到精通之五 -- 教你使用Rect()函数
【OpenCV】OpenCV实战从入门到精通之六 -- 教你如何使用滑动条-createTrackbar()函数
【OpenCV】OpenCV实战从入门到精通之七 -- 教你如何使用鼠标操作SetMouseCallback
【OpenCV】OpenCV实战从入门到精通之八 -- 带你深入理解Mat容器
【OpenCV】OpenCV实战从入门到精通之九 -- OpenCV有哪些常用的数据结构和函数
目录
程序我已经详细注释
#include <opencv2/opencv.hpp>
#include <iostream>
#include <string>
#define WINDOW_WIDTH 600 //窗口大小
#define WINDOW_NAME1 "【绘制图1】"
#define WINDOW_NAME2 "【绘制图2】"
using namespace std;
using namespace cv;
//------------------------【绘制椭圆】------------------------------------------------------------------
// 自定义,绘制不同角度,相同尺寸的椭圆
// 画在img图上,中心点坐标,大小位于矩形内(长、宽),旋转角度,扩展弧度0,360,图形颜色,线宽,线型
//------------------------------------------------------------------------------------------------------
void DrawEllipse(Mat img, double angle)
{
int thickness = 2;
int lineType = 2;
ellipse(img, Point(WINDOW_WIDTH / 2, WINDOW_WIDTH / 2), Size(WINDOW_WIDTH / 4, WINDOW_WIDTH / 16), angle, 0, 360, Scalar(255, 129, 0), thickness, lineType);
}
//----------------------------【实心圆绘制】----------------------------------
// 自定义,绘制实心圆
// 画在img图上,圆心,圆的半径,圆的颜色,线宽-1为实心,线型
//----------------------------------------------------------------------------
void DrawFilledCircle(Mat img, Point center)
{
int thickness = -1;
int lineType = 8;
circle(img, center, WINDOW_WIDTH / 32, Scalar(0, 0, 255), thickness, lineType);
}
//---------------------------【凹边型绘制】--------------------------------------
// 自定义,凹边型绘制
//将多边形画到图像上,多边形顶点集为ppt,多边形顶点数为npt,多边形数量为1,多边形定义白色
//---------------------------------------------------------------------
void DrawPolygon(Mat img)
{
int lineType = 8;
//创建一些点
Point rookPoint[1][20]; //二位数组,因为Point 要保存两个值
rookPoint[0][0] = Point(WINDOW_WIDTH / 4, 7 * WINDOW_WIDTH / 8);
rookPoint[0][1] = Point(3 * WINDOW_WIDTH / 4, 7 * WINDOW_WIDTH / 8);
rookPoint[0][2] = Point(3 * WINDOW_WIDTH / 4, 13 * WINDOW_WIDTH / 16);
rookPoint[0][3] = Point(11 * WINDOW_WIDTH / 16, 13 * WINDOW_WIDTH / 16);
rookPoint[0][4] = Point(19 * WINDOW_WIDTH / 32, 3 * WINDOW_WIDTH / 8);
rookPoint[0][5] = Point(3 * WINDOW_WIDTH / 4, 3 * WINDOW_WIDTH / 8);
rookPoint[0][6] = Point(3 * WINDOW_WIDTH / 4, WINDOW_WIDTH / 8);
rookPoint[0][7] = Point(26 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 8);
rookPoint[0][8] = Point(26 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 4);
rookPoint[0][9] = Point(22 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 4);
rookPoint[0][10] = Point(22 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 8);
rookPoint[0][11] = Point(18 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 8);
rookPoint[0][12] = Point(18 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 4);
rookPoint[0][13] = Point(14 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 4);
rookPoint[0][14] = Point(14 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 8);
rookPoint[0][15] = Point(WINDOW_WIDTH / 4, WINDOW_WIDTH / 8);
rookPoint[0][16] = Point(WINDOW_WIDTH / 4, 3 * WINDOW_WIDTH / 8);
rookPoint[0][17] = Point(13 * WINDOW_WIDTH / 32, 3 * WINDOW_WIDTH / 8);
rookPoint[0][18] = Point(5 * WINDOW_WIDTH / 16, 13 * WINDOW_WIDTH / 16);
rookPoint[0][19] = Point(WINDOW_WIDTH / 4, 13 * WINDOW_WIDTH / 16);
const Point* ppt[1] = { rookPoint[0] };
int npt[] = { 20 };
fillPoly(img, ppt, npt, 1, Scalar(255, 255, 255), lineType);
}
//-------------------------------【画直线段】------------------------------
// 在img图片上画一条从start到end的直线段
// 黑色,线粗,线型
//-----------------------------------------------------------------
void DrawLine(Mat img, Point start, Point end)
{
int thickness = 2;
int lineType = 8;
line(img, start, end, Scalar(0, 0, 0), thickness, lineType);
}
//-------------【main函数,程序主入口】--------------
int main()
{
//创建空白Mat图像
Mat atomImage = Mat::zeros(WINDOW_WIDTH, WINDOW_WIDTH, CV_8UC3);
Mat rookImage = Mat::zeros(WINDOW_WIDTH, WINDOW_WIDTH, CV_8UC3);
//----------------------<1>绘制第一幅图------------------------------------
//【1.1】绘制椭圆
DrawEllipse(atomImage, 90);
DrawEllipse(atomImage, 0);
DrawEllipse(atomImage, 45);
DrawEllipse(atomImage, -45);
//【1.2】绘制圆心
DrawFilledCircle(atomImage, Point(WINDOW_WIDTH / 2, WINDOW_WIDTH / 2));
//-----------------------<2>绘制第二幅图--------------------------
//【2.1】绘制椭圆
DrawPolygon(rookImage);
//【2.2】绘制矩形
rectangle(rookImage, Point(0, 7 * WINDOW_WIDTH / 8), Point(WINDOW_WIDTH, WINDOW_WIDTH), Scalar(0, 255, 255), -1, 8);
//【2.3】绘制线段
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, WINDOW_WIDTH / 4));
//-------------------------<3>显示图像--------------------------
imshow(WINDOW_NAME1, atomImage);
moveWindow(WINDOW_NAME1, 0, 200);
imshow(WINDOW_NAME2, rookImage);
moveWindow(WINDOW_NAME2, WINDOW_WIDTH, 200);
waitKey();
system("pause");
return 0;
}
结果如图:
文章来源: kings.blog.csdn.net,作者:人工智能博士,版权归原作者所有,如需转载,请联系作者。
原文链接:kings.blog.csdn.net/article/details/102459003
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)