opencv常用的绘图函数

opencv常用的绘图函数:
  • cv2.line(), cv2.circle(), cv2.rectangle(),cv2.polylines(), cv2.putText()
import numpy as np

# 使用Numpy创建一张A4(2105×1487)纸
img = np.zeros((640, 1280, 3), np.uint8)
# 使用白色填充图片区域,默认为黑色
img.fill(255)

#1.
# img:图像,起点坐标,终点坐标,颜色,线的宽度
cv2.line(img, (10,10), (20,20), (0,255,0), 2)

#2.
#img:图像,圆心坐标,圆半径,颜色,线宽度(-1:表示对封闭图像进行内部填满)
cv2.circle(img, (100,100), 10, (0,255,0), -1)

#3.
# img:图像,起点坐标,终点坐标,颜色,线宽度
cv2.rectangle(img, (20,20), (200,200), (0,255,0), -1)

#4.
Pts = np.array([[5,5], [20,30], [40,60], [50,10]], np.int32)
Pts = Pts.reshape((-1,1,2))
#img:图像,顶点集,是否闭合,颜色,线宽度
cv2.polylines(img, [Pts], True, (0,255,0), 33)

#5.
font = cv2.FONT_HERSHEY_SIMPLEX
#img:图像,输入字符串,坐标,字体,字号,颜色,颜色,线宽度,线条种类。
cv2.putText(img, 'this is a test info.', (100,100), font, 2, (0,255,0), 2, cv2.LINE_A4)

cv2.imshow(img_id, img)
cv2.waitKey()
  • 在图片上写如中文

opencv不支持写英文,所以需要转换成PIL的 Image格式 ,画上中文后再转换回numpy
“…/cfg/simhei.ttf” 是字体文件,在C盘搜索可以得到

from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
def draw_chinese_word(frame, location, content, word_size=15, color=(255, 255, 255)):
    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    frame = Image.fromarray(frame)
    draw = ImageDraw.Draw(frame) # 括号中为需要打印的canvas,这里就是在图片上直接打印
    font = ImageFont.truetype("../cfg/simhei.ttf", word_size, encoding="utf-8") # 第一个参数为字体文件路径,第二个为字体大小
    draw.text(location, content, color, font=font) # 第一个参数为打印的坐标,第二个为打印的文本,第三个为字体颜色,第四个为字体
    frame = cv2.cvtColor(np.array(frame), cv2.COLOR_RGB2BGR)
    return frame
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值