本篇内容主要讲解“OpenCV基本图形绘制函数有哪些”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“OpenCV基本图形绘制函数有哪些”吧!
用于绘制直线的line函数;
用于绘制椭圆的ellipse函数;
用于绘制矩形的rectangle函数;
用于绘制圆的circle函数;
用于绘制填充的多边形的fillPoly函数。
#include <opencv2/core/core.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <opencv2/highgui/highgui.hpp> using namespace cv; using namespace std; #define WINDOW_WIDTH 600 // 定义窗口大小的宏 #define WINDOW_NAME1 "原子图" //为窗口标题定义的宏 #define WINDOW_NAME2 "多边形图以及画线" //为窗口标题定义的宏 /***绘制椭圆***/ void DrawEllipse(Mat img, double angle) { int thickness = 2; // 线宽 int lineType = 8; // 线性,8 代表联通线性 ellipse(img, Point(WINDOW_WIDTH / 2, WINDOW_WIDTH / 2), Size(WINDOW_WIDTH / 4, WINDOW_WIDTH / 16), angle, // 角度,0~360 度 0, 360, Scalar(255, 129, 0), thickness, lineType); } /*绘制实心圆*/ void DrawFilledCircle(Mat img, Point center) { int thickness = -1; // 线粗-1,代表实心 int lineType = 8; // 线性,8 代表联通线性 circle(img, center, WINDOW_WIDTH / 32, Scalar(0, 0, 255), thickness, 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基本图形绘制函数有哪些”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。