【OpenCV】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有哪些常用的数据结构和函数 

目录

本文是《OpenCV实战从入门到精通》系列之第10篇

 程序我已经详细注释

结果如图:


 程序我已经详细注释


  
  1. #include <opencv2/opencv.hpp>
  2. #include <iostream>
  3. #include <string>
  4. #define WINDOW_WIDTH 600 //窗口大小
  5. #define WINDOW_NAME1 "【绘制图1】"
  6. #define WINDOW_NAME2 "【绘制图2】"
  7. using namespace std;
  8. using namespace cv;
  9. //------------------------【绘制椭圆】------------------------------------------------------------------
  10. // 自定义,绘制不同角度,相同尺寸的椭圆
  11. // 画在img图上,中心点坐标,大小位于矩形内(长、宽),旋转角度,扩展弧度0,360,图形颜色,线宽,线型
  12. //------------------------------------------------------------------------------------------------------
  13. void DrawEllipse(Mat img, double angle)
  14. {
  15. int thickness = 2;
  16. int lineType = 2;
  17. 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);
  18. }
  19. //----------------------------【实心圆绘制】----------------------------------
  20. // 自定义,绘制实心圆
  21. // 画在img图上,圆心,圆的半径,圆的颜色,线宽-1为实心,线型
  22. //----------------------------------------------------------------------------
  23. void DrawFilledCircle(Mat img, Point center)
  24. {
  25. int thickness = -1;
  26. int lineType = 8;
  27. circle(img, center, WINDOW_WIDTH / 32, Scalar(0, 0, 255), thickness, lineType);
  28. }
  29. //---------------------------【凹边型绘制】--------------------------------------
  30. // 自定义,凹边型绘制
  31. //将多边形画到图像上,多边形顶点集为ppt,多边形顶点数为npt,多边形数量为1,多边形定义白色
  32. //---------------------------------------------------------------------
  33. void DrawPolygon(Mat img)
  34. {
  35. int lineType = 8;
  36. //创建一些点
  37. Point rookPoint[1][20]; //二位数组,因为Point 要保存两个值
  38. rookPoint[0][0] = Point(WINDOW_WIDTH / 4, 7 * WINDOW_WIDTH / 8);
  39. rookPoint[0][1] = Point(3 * WINDOW_WIDTH / 4, 7 * WINDOW_WIDTH / 8);
  40. rookPoint[0][2] = Point(3 * WINDOW_WIDTH / 4, 13 * WINDOW_WIDTH / 16);
  41. rookPoint[0][3] = Point(11 * WINDOW_WIDTH / 16, 13 * WINDOW_WIDTH / 16);
  42. rookPoint[0][4] = Point(19 * WINDOW_WIDTH / 32, 3 * WINDOW_WIDTH / 8);
  43. rookPoint[0][5] = Point(3 * WINDOW_WIDTH / 4, 3 * WINDOW_WIDTH / 8);
  44. rookPoint[0][6] = Point(3 * WINDOW_WIDTH / 4, WINDOW_WIDTH / 8);
  45. rookPoint[0][7] = Point(26 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 8);
  46. rookPoint[0][8] = Point(26 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 4);
  47. rookPoint[0][9] = Point(22 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 4);
  48. rookPoint[0][10] = Point(22 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 8);
  49. rookPoint[0][11] = Point(18 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 8);
  50. rookPoint[0][12] = Point(18 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 4);
  51. rookPoint[0][13] = Point(14 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 4);
  52. rookPoint[0][14] = Point(14 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 8);
  53. rookPoint[0][15] = Point(WINDOW_WIDTH / 4, WINDOW_WIDTH / 8);
  54. rookPoint[0][16] = Point(WINDOW_WIDTH / 4, 3 * WINDOW_WIDTH / 8);
  55. rookPoint[0][17] = Point(13 * WINDOW_WIDTH / 32, 3 * WINDOW_WIDTH / 8);
  56. rookPoint[0][18] = Point(5 * WINDOW_WIDTH / 16, 13 * WINDOW_WIDTH / 16);
  57. rookPoint[0][19] = Point(WINDOW_WIDTH / 4, 13 * WINDOW_WIDTH / 16);
  58. const Point* ppt[1] = { rookPoint[0] };
  59. int npt[] = { 20 };
  60. fillPoly(img, ppt, npt, 1, Scalar(255, 255, 255), lineType);
  61. }
  62. //-------------------------------【画直线段】------------------------------
  63. // 在img图片上画一条从start到end的直线段
  64. // 黑色,线粗,线型
  65. //-----------------------------------------------------------------
  66. void DrawLine(Mat img, Point start, Point end)
  67. {
  68. int thickness = 2;
  69. int lineType = 8;
  70. line(img, start, end, Scalar(0, 0, 0), thickness, lineType);
  71. }
  72. //-------------【main函数,程序主入口】--------------
  73. int main()
  74. {
  75. //创建空白Mat图像
  76. Mat atomImage = Mat::zeros(WINDOW_WIDTH, WINDOW_WIDTH, CV_8UC3);
  77. Mat rookImage = Mat::zeros(WINDOW_WIDTH, WINDOW_WIDTH, CV_8UC3);
  78. //----------------------<1>绘制第一幅图------------------------------------
  79. //【1.1】绘制椭圆
  80. DrawEllipse(atomImage, 90);
  81. DrawEllipse(atomImage, 0);
  82. DrawEllipse(atomImage, 45);
  83. DrawEllipse(atomImage, -45);
  84. //【1.2】绘制圆心
  85. DrawFilledCircle(atomImage, Point(WINDOW_WIDTH / 2, WINDOW_WIDTH / 2));
  86. //-----------------------<2>绘制第二幅图--------------------------
  87. //【2.1】绘制椭圆
  88. DrawPolygon(rookImage);
  89. //【2.2】绘制矩形
  90. rectangle(rookImage, Point(0, 7 * WINDOW_WIDTH / 8), Point(WINDOW_WIDTH, WINDOW_WIDTH), Scalar(0, 255, 255), -1, 8);
  91. //【2.3】绘制线段
  92. DrawLine(rookImage, Point(0, 15 * WINDOW_WIDTH / 16), Point(WINDOW_WIDTH, 15 * WINDOW_WIDTH / 16));
  93. DrawLine(rookImage, Point(WINDOW_WIDTH / 4, 7 * WINDOW_WIDTH / 8), Point(WINDOW_WIDTH / 4, WINDOW_WIDTH));
  94. DrawLine(rookImage, Point(WINDOW_WIDTH / 2, 7 * WINDOW_WIDTH / 8), Point(WINDOW_WIDTH / 2, WINDOW_WIDTH));
  95. DrawLine(rookImage, Point(3 * WINDOW_WIDTH / 4, 7 * WINDOW_WIDTH / 8), Point(3 * WINDOW_WIDTH, WINDOW_WIDTH / 4));
  96. //-------------------------<3>显示图像--------------------------
  97. imshow(WINDOW_NAME1, atomImage);
  98. moveWindow(WINDOW_NAME1, 0, 200);
  99. imshow(WINDOW_NAME2, rookImage);
  100. moveWindow(WINDOW_NAME2, WINDOW_WIDTH, 200);
  101. waitKey();
  102. system("pause");
  103. return 0;
  104. }

结果如图:

文章来源: kings.blog.csdn.net,作者:人工智能博士,版权归原作者所有,如需转载,请联系作者。

原文链接:kings.blog.csdn.net/article/details/102459003

(完)