图像几何形状绘制
绘制圆形
circle(img, center, radius, color, thickness)
- img:需要绘制的图像
- center:圆形的圆心位置坐标
- radius:圆形的半径,单位为像素
- color:圆形的颜色
- thickness:轮廓的粗细,若为负值,则绘制一个实心圆
绘制矩形
// 原型一 rectangle(img, pt1, pt2, color, thickness) // 原型二 rectangle(img, rec, color, thickness)
-
pt1:矩形的一个顶点
-
pt2:矩形中与pt1相对的顶点,即两个点在对角线上
-
rec:矩形左上角顶点和长宽
补:Rect变量在 OpenCV 4 中表示矩形的含义,与Point、Vec3b等类型相同,都是在图像处理中常用的类型。Rect表示的是矩形左上角像素坐标,以及矩形的长和宽,该类型定义的格式为Rect(像素的x坐标,像素的y坐标, 矩形的宽,矩形的高)
,其中可以存放的数据类型也分别为int型(Rect2i或者Rect)、double类型(Rect2d)和float类型(Rect2f)
绘制直线
line(img, pt1, pt2, color, thickness)
- pt1:直线起点在图像中的坐标
- pt2:直线终点在图像中的坐标
- color:直线的颜色,用三通道表示
绘制椭圆
ellipse(img, center, axes, angle, startAngle, endAngle, color, thickness)
- center:椭圆的中心坐标
- axes:椭圆的长轴和短轴长度
- angle:椭圆的旋转角度,单位为度
- startAngle:椭圆弧的起始角度,单位为度
- endAngle:椭圆弧的终止角度,单位为度
补:通过椭圆弧起始和终止角度,可以绘制完整的椭圆或一部分椭圆弧
添加文本
putText(img, text, org, fontFace, fontScale, color, thickness)
-
text:输出到图像中的文字,类型为string,目前只支持英文
-
org:指定文本字符串左上角的起始位置
-
fontFace:字体样式,可选参数如下:
标志 取值 含义 FONT_HERSHEY_SIMPLEX 0 正常大小的无衬线字体 FONT_HERSHEY_PLAIN 1 小尺寸的无衬线字体 FONT_HERSHEY_DUPLEX 2 正常大小的较复杂的无衬线字体 FONT_HERSHEY_COMPLEX 3 正常大小的衬线字体 FONT_HERSHEY_TRIPLEX 4 正常大小的较复杂的衬线字体 FONT_HERSHEY_COMPLEX_SMALL 5 小尺寸的衬线字体 FONT_HERSHEY_SCRIPT_SIMPLEX 6 手写风格的字体 FONT_HERSHEY_SCRIPT_COMPLEX 7 复杂的手写风格字体 FONT_ITALIC 16 斜体字体 -
fontScale:字体的大小,类型为 double
-
color:字体颜色,类型为 Scalar
示例代码
#include <opencv2/opencv.hpp> using namespace cv; int main() { Mat_<Vec3b> img = imread("/home/kslas/OpenCV/sample.jpg"); imshow("Original Image", img); /* Draw the image from point A to B */ Mat_<Vec3b> line_image = img.clone(); Point pointA(200,80); Point pointB(450,80); line(line_image, pointA, pointB, Scalar(255, 255, 0), 3); imshow("Line on Image", line_image); /* Draw a circle using the circle() Function */ Mat_<Vec3b> circle_image = img.clone(); // define the center of circle Point circle_center(415,190); // define the radius of circle int radius = 100; circle(circle_image, circle_center, radius, Scalar(0, 0, 255), 3); imshow("Circle on Image", circle_image); Mat_<Vec3b> Filled_circle_image = img.clone(); circle(Filled_circle_image, circle_center, radius, Scalar(255, 0, 0), -1); imshow("Filled Circle on Image", Filled_circle_image); /* Draw a rectangle using the rectangle() function */ Mat_<Vec3b> rect_image = img.clone(); // Define the starting and end points for the rectangle Point start_point(300,115); Point end_point(475,225); rectangle(rect_image, start_point, end_point, Scalar(0,0,255), 3); imshow("Rectangle on Image", rect_image); /* Draw an ellipse using the ellipse() function */ Mat_<Vec3b> imageEllipse = img.clone(); // define the center point of ellipse Point ellipse_center(415,190); // define the major and minor axes of the ellipse Point axis1(100, 50); Point axis2(125, 50); //Horizontal ellipse(imageEllipse, ellipse_center, axis1, 0, 0, 360, Scalar(255, 0, 0), 3); // Vertical ellipse(imageEllipse, ellipse_center, axis2, 90, 0, 360, Scalar(0, 0, 255), 3); imshow("Ellipses on Image", imageEllipse); /* draw the Half Ellipse, just the outline */ Mat_<Vec3b> halfEllipse = img.clone(); ellipse(halfEllipse, ellipse_center, axis1, 0, 180, 360, Scalar(255, 0, 0), 3); // if you want to draw a Filled ellipse, use this line of code ellipse(halfEllipse, ellipse_center, axis1, 0, 0, 180, Scalar(0, 0, 255), -2); imshow("Half-Ellipses on Image", halfEllipse); /* Write text using putText() function */ Mat_<Vec3b> imageText = img.clone(); putText(imageText, "I am a Happy dog!", Point(50,350), FONT_HERSHEY_COMPLEX, 1.5, Scalar(250,225,100)); imshow("Text on Image", imageText); waitKey(0); destroyAllWindows(); return 0; }
运行结果:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!