OpenCV在图片正中间画矩形框,并标出文字,Python

18 篇文章 1 订阅
import numpy as np
import cv2 as cv


def draw_rec(image, width, height, label, number):
    (H, W) = image.shape[:2]
    rec = (H, W)
    COLORS = np.random.randint(0, 255, size=2, dtype="uint8")
    color = [int(c) for c in COLORS]

    (w, h) = (width, height)
    (x, y) = (int((rec[1] - width) / 2), int((rec[0] - height) / 2))

    cv.rectangle(img=image, pt1=(x, y), pt2=(x + w, y + h), color=color[0], thickness=2)

    text = "{}:{:.3f}".format(label, number)
    cv.putText(image, text, (x, y - 5), cv.FONT_HERSHEY_COMPLEX, fontScale=0.5, color=color[1], thickness=1)


if __name__ == '__main__':
    imagePath = "./pic.jpg"
    image = cv.imread(imagePath)

    # 画一个高height,宽width的矩形。
    width = 250
    height = 80
    draw_rec(image, width, height, "zhangphil@csdn", 2019)

    cv.imshow("pic", image)
    cv.waitKey(0)

 

输出:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhangphil

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值