当前位置: 首页 > news >正文

OpenCV6-图形绘制

OpenCV6-图形绘制

    • 1.绘制圆形
    • 2.绘制直线
    • 3.绘制椭圆
    • 4.绘制多边形
    • 5.文字生成
    • 6.demo


1.绘制圆形

void cv::circle(InputOutputArray img,  // 需要绘制圆形的图像Point center,          // 圆心坐标int radius,            // 半径,单位为像素const Scalar& color,   // 圆形颜色int thickness = 1,     // 轮廓宽度,如果数值为负,则绘制一个实心圆int lineType = LINE_8, // 边界的类型,可取值FILLED、LINE_4、LINE_8、LINE_AAint shift = 0          // 中心坐标和半径数值中的小数位数
);// example
circle(img, Point(50, 50), 25, Scalar(255, 255, 255), -1);  //绘制一个实心圆
circle(img, Point(100, 50), 20, Scalar(255, 255, 255), 4);  //绘制一个空心圆

2.绘制直线

void line(InputOutputArray img,   Point pt1, Point pt2,  // 直线起点和终点const Scalar& color,   // 直线颜色,三通道表示int thickness = 1, int lineType = LINE_8, int shift = 0
);line(img, Point(100, 100), Point(200, 100), Scalar(255, 255, 255), 2, LINE_4, 0);  //绘制一条直线

3.绘制椭圆

void ellipse(InputOutputArray img,  Point center,          // 椭圆的中心坐标Size axes,             // 椭圆主轴大小的一半。double angle,          // 椭圆旋转的角度double startAngle,     // 椭圆弧起始的角度,度为单位double endAngle,       // 椭圆弧终止的角度,度为单位const Scalar& color,   int thickness = 1,int lineType = LINE_8, int shift = 0
);

在OpenCV中还提供了ellipse2Poly函数用于输出椭圆边界的像素坐标,但是不会在图像中绘制椭圆:

void ellipse2Poly( Point center, Size axes, int angle,int arcStart, int arcEnd, int delta,  // 后续折线顶点之间的角度,他定义了近似精度CV_OUT std::vector<Point>& pts // 椭圆边缘像素坐标向量集合
);
/*
该函数与绘制椭圆所需的参数类似,只不过不再将椭圆输出到图像中,而是通过vector(向量)将椭圆边缘的坐标点存储起来,便于后续的再处理。
*/

4.绘制多边形

矩形的绘制:

void rectangle(nputOutputArray img, Point pt1, // 左上角Point pt2, // 右下角const Scalar& color,   int thickness = 1,int lineType = LINE_8, int shift = 0
);void rectangle(InputOutputArray img, Rect rec, // 矩形const Scalar& color, int thickness = 1,int lineType = LINE_8, int shift = 0);typedef Rect2i Rect;
typedef Rect_<int> Rect2i;template<typename _Tp> class Rect_
{
public:// ..._Tp x; //!< x coordinate of the top-left corner_Tp y; //!< y coordinate of the top-left corner_Tp width; //!< width of the rectangle_Tp height; //!< height of the rectangle
};

下面是绘制多边形的方法:可以一次绘制多个多边形。

void fillPoly(InputOutputArray img, const Point** pts,  // 多边形顶点数组。存放多个多边形顶点坐标的数组。const int* npts,    // 每个多边形顶点数组中顶点的个数int ncontours,      // 绘制多边形的个数const Scalar& color, int lineType = LINE_8, int shift = 0,Point offset = Point() // 所有顶点的可选偏移
);

5.文字生成

目前只支持英文文本的输出。

void putText(InputOutputArray img, const String& text,   // 输出的文本内容Point org,            // 图像中文字字符串的左下角像素坐标int fontFace,         // 字体类型double fontScale,     // 字体大小Scalar color,int thickness = 1, int lineType = LINE_8,bool bottomLeftOrigin = false // 图像数据原点的位置,默认为左上角;如果参数为true,则原点为左下角。
);/*
FONT_HERSHEY_SIMPLEX:正常大小的无衬线文字
FONT_HERSHEY_PLAIN:小尺寸无衬线文字
FONT_ITALIC:斜体字体
*/
// fontFace字体类型
enum HersheyFonts {FONT_HERSHEY_SIMPLEX        = 0, //!< normal size sans-serif fontFONT_HERSHEY_PLAIN          = 1, //!< small size sans-serif fontFONT_HERSHEY_DUPLEX         = 2, //!< normal size sans-serif font (more complex than FONT_HERSHEY_SIMPLEX)FONT_HERSHEY_COMPLEX        = 3, //!< normal size serif fontFONT_HERSHEY_TRIPLEX        = 4, //!< normal size serif font (more complex than FONT_HERSHEY_COMPLEX)FONT_HERSHEY_COMPLEX_SMALL  = 5, //!< smaller version of FONT_HERSHEY_COMPLEXFONT_HERSHEY_SCRIPT_SIMPLEX = 6, //!< hand-writing style fontFONT_HERSHEY_SCRIPT_COMPLEX = 7, //!< more complex variant of FONT_HERSHEY_SCRIPT_SIMPLEXFONT_ITALIC                 = 16 //!< flag for italic font
};

6.demo

#include <opencv2\opencv.hpp>
#include <opencv2/core/utils/logger.hpp> // debug no log
#include <iostream>
using namespace cv;
using namespace std;int main()
{cout << "OpenCV Version: " << CV_VERSION << endl;utils::logging::setLogLevel(utils::logging::LOG_LEVEL_SILENT);Mat img = Mat::zeros(Size(512, 512), CV_8UC3);  //生成一个黑色图像用于绘制几何图形//绘制圆形circle(img, Point(50, 50), 25, Scalar(255, 255, 255), -1);  //绘制一个实心圆circle(img, Point(100, 50), 20, Scalar(255, 255, 255), 4);  //绘制一个空心圆//绘制直线line(img, Point(100, 100), Point(200, 100), Scalar(255, 255, 255), 2, LINE_4, 0);  //绘制一条直线//绘制椭圆ellipse(img, Point(300, 255), Size(100, 70), 0, 0, 100, Scalar(255, 255, 255), -1);  //绘制实心椭圆的一部分ellipse(img, RotatedRect(Point2f(150, 100), Size2f(30, 20), 0), Scalar(0, 0, 255), 2);  //绘制一个空心椭圆vector<Point> points;ellipse2Poly(Point(200, 400), Size(100, 70), 0, 0, 360, 2, points);  //用一些点来近似一个椭圆for (int i = 0; i < points.size() - 1; i++)  //用直线把这个椭圆画出来{if (i == points.size() - 1){line(img, points[0], points[i], Scalar(255, 255, 255), 2);  //椭圆中后于一个点与第一个点连线break;}line(img, points[i], points[i + 1], Scalar(255, 255, 255), 2);  //当前点与后一个点连线}//绘制矩形rectangle(img, Point(50, 400), Point(100, 450), Scalar(125, 125, 125), -1);rectangle(img, Rect(400, 450, 60, 50), Scalar(0, 125, 125), 2);//绘制多边形Point pp[2][6];pp[0][0] = Point(72, 200);pp[0][1] = Point(142, 204);pp[0][2] = Point(226, 263);pp[0][3] = Point(172, 310);pp[0][4] = Point(117, 319);pp[0][5] = Point(15, 260);pp[1][0] = Point(359, 339);pp[1][1] = Point(447, 351);pp[1][2] = Point(504, 349);pp[1][3] = Point(484, 433);pp[1][4] = Point(418, 449);pp[1][5] = Point(354, 402);Point pp2[5];pp2[0] = Point(350, 83);pp2[1] = Point(463, 90);pp2[2] = Point(500, 171);pp2[3] = Point(421, 194);pp2[4] = Point(338, 141);const Point* pts[3] = { pp[0],pp[1],pp2 };  //pts变量的生成int npts[] = { 6,6,5 };  //顶点个数数组的生成fillPoly(img, pts, npts, 3, Scalar(125, 125, 125), 8);  //绘制3个多边形//生成文字putText(img, "Learn OpenCV 4", Point(100, 400), 2, 1, Scalar(255, 255, 255));imshow("", img);waitKey(0);return 0;
}
http://www.lryc.cn/news/190912.html

相关文章:

  • kafka消费者程序日志报错Offset commit failed问题研究
  • SpringBoot+原生HTML+MySQL开发的电子病历系统源码
  • 软件测试/测试开发/人工智能丨聊聊AutoGPT那些事儿
  • KdMapper扩展实现之SOKNO S.R.L(speedfan.sys)
  • MATLAB算法实战应用案例精讲-【图像处理】计算机视觉
  • docker应用的缓存 docker缓存机制
  • 借助 ZooKeeper 生成唯一 UUID
  • Redis哨兵机制原理
  • Maven Web应用
  • 考古:MFC界面的自适应缩放(代码示例)
  • 计算机网络 | 物理层
  • Centos下编译ffmpeg动态库
  • 深度学习:UserWarning: The parameter ‘pretrained‘ is deprecated since 0.13..解决办法
  • leetcode-279. 完全平方数
  • MySQL常用指令
  • Pulsar 之架构,客户端以及多区域容灾
  • 【SQL】MySQL中的索引,索引优化
  • uniapp 跳转到指定位置
  • 基于java的图书馆预约座位系统的设计与实现(部署+源码+LW)
  • golang 拉取 bitbucket.org 私有库
  • Sub-1G射频收发器soc芯片 UM2080F32 低功耗 32 位 IoTP
  • 国际减灾日 | 智慧减灾——百分点科技的数据科学视角
  • ChatGLM流式输出的报错修复
  • HDLbits: ece241 2013 q12 // Exams/m2014 q4k
  • vue3模板-vscode设置(语法糖)
  • RFID超高频读写器的特点和应用
  • 诡异事件:开发的安卓摄像头应用突然不能保存图片,回滚代码都查不出来
  • validator库的使用详解
  • ADS版图中连接提示线设置
  • 【MySQL】内置函数——数学函数+其他函数