Python中使用Opencv-python库绘制直线、矩形、圆、文本

Python中使用Opencv-python库绘制直线、矩形、圆、文字

在Python中使用Opencv-python绘制直线、矩形、圆、文本非常简单,分别使用到line、rectangle、circle、putText这几个函数,具体可以参考https://docs.opencv.org/4.9.0/d6/d6e/group__imgproc__draw.html#ga7078a9fae8c7e7d13d24dac2520ae4a2官方文档

line 绘制直线

line函数原型如下:

line()
void cv::line	(	InputOutputArray 	img,
Point 	pt1,
Point 	pt2,
const Scalar & 	color,
int 	thickness = 1,
int 	lineType = LINE_8,
int 	shift = 0 
)		
cv.line(	img, pt1, pt2, color[, thickness[, lineType[, shift]]]	) ->	img

line函数说明

rectangle 绘制矩形

rectangle 函数原型如下:

rectangle() [1/2]
void cv::rectangle	(	InputOutputArray 	img,
Point 	pt1,
Point 	pt2,
const Scalar & 	color,
int 	thickness = 1,
int 	lineType = LINE_8,
int 	shift = 0 
)		
Python:
cv.rectangle(	img, pt1, pt2, color[, thickness[, lineType[, shift]]]	) ->	img
cv.rectangle(	img, rec, color[, thickness[, lineType[, shift]]]	) ->	img

rectangle 001
rectangle函数在opencv-python库中还有重载形式,原型如下所示:

rectangle() [2/2]
void cv::rectangle	(	InputOutputArray 	img,
Rect 	rec,
const Scalar & 	color,
int 	thickness = 1,
int 	lineType = LINE_8,
int 	shift = 0 
)		
Python:
cv.rectangle(	img, pt1, pt2, color[, thickness[, lineType[, shift]]]	) ->	img
cv.rectangle(	img, rec, color[, thickness[, lineType[, shift]]]	) ->	img

rectangle函数重载形式 002

circle 绘制圆

circle 函数原型如下:

circle()
void cv::circle	(	InputOutputArray 	img,
Point 	center,
int 	radius,
const Scalar & 	color,
int 	thickness = 1,
int 	lineType = LINE_8,
int 	shift = 0 
)		
Python:
cv.circle(	img, center, radius, color[, thickness[, lineType[, shift]]]	) ->	img

circle函数原型

putText 绘制文字

putText 函数原型如下:

 putText()
void cv::putText	(	InputOutputArray 	img,
const String & 	text,
Point 	org,
int 	fontFace,
double 	fontScale,
Scalar 	color,
int 	thickness = 1,
int 	lineType = LINE_8,
bool 	bottomLeftOrigin = false 
)		
Python:
cv.putText(	img, text, org, fontFace, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]]	) ->	img

putText函数原型

python中使用Opencv-python库绘制直线、矩形、圆、文本的示例代码

python示例代码如下:

import cv2
import numpy as np

img = np.zeros((512, 512, 3), np.uint8) # 构造高512,宽512, 3通道的Numpy数组
# img = np.zeros((1000, 512, 3), np.uint8)    # 高为1000,宽度为512
# print(img)
print(img.shape)    # (width,height,channels)
# img[:] = 255,0,0    # 将图像img中的所有像素赋值为蓝色(B,G,R)
# img[0:100,200:300] = 0,255,0    # 将高度0-100,宽度200-300的区间像素全部赋值为绿色

cv2.line(img, (0, 0), (img.shape[1], img.shape[0]), (0, 255, 0), 3) # 在图像左上角原点(0,0)到右下角画一条绿色的直线,线条厚度为3
cv2.rectangle(img, (0, 0), (250, 350), (0, 0, 255), 2)  # 在左上角顶点(0,0)和右下角(250,350)处绘制一个红色矩形,边界线条厚度为2
cv2.circle(img, (400, 50), 30, (255, 255, 0), 5)    # 以(400,50)为中心,绘制半径为30的圆,颜色为青色(绿+蓝=青(Cyan))
cv2.putText(img, "OpenCV", (350, 300), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 255, 255), 2)   # 在(350,300)处绘制文字,字体为FONT_HERSHEY_COMPLEX,比例为1,颜色为黄色,厚度为2


cv2.imshow("image", img)    # 绘制图像
cv2.waitKey(0)  # 永久等待用户输入,直到输入按键退出
cv2.destroyAllWindows() # 销毁所有窗口

在PyCharm或者Visual Studio中运行上述代码,运行结果如下图所示:
使用Opencv-python库绘制直线、矩形、圆、文本

参考资料

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值