Opencv学习笔记(五):cv2.putText()和cv2.rectangle()详细理解

一:cv2.putText()

函数介绍:cv2.putText(img, str(i), (123,456), font, 2, (0,255,0), 3)
参数意思:图片,添加的文字,左上角坐标,字体,字体大小(数值越大,字体越大,可以为小数),颜色,字体粗细(越大越粗)
字体选择:FONT_HERSHEY_SIMPLEX、normal size sans-serif font、small size sans-serif font、FONT_HERSHEY_COMPLEX

二:cv2.rectangle()

函数介绍:cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
参数意思:图片,左上点坐标,右下点坐标,rgb颜色,线的宽度

三:实战例子

import cv2
import math
class ssdface():
    def __init__(self, framework='caffe', threshold=0.7):
        if framework == 'caffe':
            self.net = cv2.dnn.readNetFromCaffe('D:\pycharm\compare_three_module\SSDV2_detect_face/deploy.prototxt', 'D:/pycharm\compare_three_module/SSDV2_detect_face/res10_300x300_ssd_iter_140000_fp16.caffemodel')
        self.conf_threshold = threshold
        self.framework = framework
    def detect(self, frame):
        frameOpencvDnn = frame.copy()
        frameHeight = frameOpencvDnn.shape[0]
        frameWidth = frameOpencvDnn.shape[1]
        if self.framework == 'caffe':
            blob = cv2.dnn.blobFromImage(frameOpencvDnn, 1.0, (300, 300), [104, 117, 123], False, False)
        else:
            blob = cv2.dnn.blobFromImage(frameOpencvDnn, 1.0, (300, 300), [104, 117, 123], True, False)
        self.net.setInput(blob)
        detections = self.net.forward()
        face_rois = []
        confidence1=[]
        for i in range(detections.shape[2]):
            confidence = detections[0, 0, i, 2]
            if confidence > self.conf_threshold:
                confidence1.append(confidence)
                x1 = int(detections[0, 0, i, 3] * frameWidth)
                y1 = int(detections[0, 0, i, 4] * frameHeight)
                print(x1,y1)
                # p1=(x1,y1)
                x2 = int(detections[0, 0, i, 5] * frameWidth)
                y2 = int(detections[0, 0, i, 6] * frameHeight)
                # p2=(x2,y2)
                # cv2.rectangle(frameOpencvDnn, (x1, y1), (x2, y2), (255, 0, 0), 2)
                title = math.floor(confidence * 10 ** 2) / (10 ** 2)
                print(title)
                cv2.rectangle(frameOpencvDnn,(x1, y1), (x2, y2), (0, 0, 255), thickness=2)
                # cv2.putText(frameOpencvDnn,title, (x1,y1), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 1)
                cv2.putText(frameOpencvDnn, 'acc:' + str(title), (x1+10, y1-10),cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0, 0, 255),1)
                face_rois.append(frame[y1:y2, x1:x2])
                # cv2.imshow('1',frame[y1:y2, x1:x2])
                # cv2.waitKey(0)
        return frameOpencvDnn, face_rois,confidence1
    def get_face(self, frame):
        frameOpencvDnn = frame.copy()
        frameHeight = frameOpencvDnn.shape[0]
        frameWidth = frameOpencvDnn.shape[1]
        if self.framework == 'caffe':
            blob = cv2.dnn.blobFromImage(frameOpencvDnn, 1.0, (300, 300), [104, 117, 123], False, False)
        else:
            blob = cv2.dnn.blobFromImage(frameOpencvDnn, 1.0, (300, 300), [104, 117, 123], True, False)
        self.net.setInput(blob)
        detections = self.net.forward()
        boxs, face_rois = [], []
        for i in range(detections.shape[2]):
            confidence = detections[0, 0, i, 2]
            if confidence > self.conf_threshold:
                x1 = int(detections[0, 0, i, 3] * frameWidth)
                y1 = int(detections[0, 0, i, 4] * frameHeight)
                x2 = int(detections[0, 0, i, 5] * frameWidth)
                y2 = int(detections[0, 0, i, 6] * frameHeight)
                boxs.append((x1, y1, x2, y2))
                face_rois.append(frame[y1:y2, x1:x2])
        return boxs, face_rois

if __name__ == "__main__" :
    import time
    ssdface_detect = ssdface(framework='caffe')
    imgpath = 's_l.jpg'
    save_path='D:/pycharm/compare_three_module/result/'
    srcimg = cv2.imread(imgpath)
    a = time.time()
    drawimg, face_rois,confidence = ssdface_detect.detect(srcimg)
    b = time.time()
    time = round(b - a, 3)  # 保留一位小数 2为两位
    cv2.putText(drawimg, 'time:' + str(time), (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 1,(0, 0, 255))
    cv2.namedWindow('SSDV2detect', cv2.WINDOW_NORMAL)
    cv2.imshow('SSDV2detect', drawimg)
    cv2.imwrite(save_path + 'SSDV2.jpg', drawimg)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

运行结果
在这里插入图片描述

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
cv2.putText()是OpenCV库中的一个函数,用于在图像上绘制中文字符。通过这个函数可以在图像上添加中文文字,用于标记、展示等目的。 cv2.putText()函数的使用方法如下: cv2.putText(img, text, org, fontFace, fontScale, color, thickness, lineType) 其中,img是待绘制文字的图像,text是要绘制的文字内容,org是文字的起始位置,fontFace是字体类型,fontScale是字体缩放比例,color是字体颜色,thickness是字体线条粗细,lineType是线条类型。 由于OpenCV默认不支持中文字符的显示,因此需要进行一些额外的设置,以便正确显示中文。首先,需要下载合适的中文字体文件,例如将SimSun.ttf字体文件保存在工程目录下。然后,可以使用OpenCV的内置函数cv2.putText()绘制中文字符。 具体的步骤如下: 1. 加载图像:使用cv2.imread()函数加载待绘制文字的图像。 2. 设定字体:使用cv2.FONT_HERSHEY_SIMPLEX设定字体类型,同时设定字体缩放比例、颜色和线条粗细。 3. 绘制文字:调用cv2.putText()函数,在图像上绘制中文字符。设定起始位置、字体类型、缩放比例、颜色、线条粗细和线条类型。 需要注意的是,由于中文字符的编码问题,可能会出现乱码或无法正常显示的情况。解决这个问题需要在程序中进行编码转换,以确保正确显示中文字符。 总之,cv2.putText()函数是OpenCV库中用于在图像上绘制中文字符的函数,通过设定文字内容、位置和样式等参数,可以方便地在图像上添加中文文字。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

ZZY_dl

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

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

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

打赏作者

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

抵扣说明:

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

余额充值