OpenCV 基本图形绘制函数详解
时间:2022-03-15 08:07:45|栏目:Python代码|点击: 次
- 用于绘制直线的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,代表实心 circle(img, center, WINDOW_WIDTH / 32, Scalar(0, 0, 255), /*凹多边形绘制*/ 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), // 多边形颜色:白色 /*线绘制*/ void DrawLine(Mat img, Point start, Point end) int thickness = 2; line(img, start, end, Scalar(0,0,0), // 黑色 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);
运行:
参考:
《OpenCV3 编程入门》 毛星云 P104
栏 目:Python代码
下一篇:Python.append()与Python.expand()用法详解
本文标题:OpenCV 基本图形绘制函数详解
本文地址:http://www.codeinn.net/misctech/196271.html