opencv python 中cv2.putText()函数的用法

本文详细介绍OpenCV中cv2.putText()函数的使用方法,包括参数解析、快速使用指南及官方文档引用。通过实例演示如何将文字添加到摄像头捕获的图像上,展示文字位置、字体、大小、颜色和粗细的设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

opencv python 中cv2.putText()函数的用法

文章目录:


虽然用啦很多次,还是决定记录一下


一、快速使用

cv2.putText(image, text, (5,50 ), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2)

位置参数说明:

  • 图片
  • 要添加的文字
  • 文字添加到图片上的位置
  • 字体的类型
  • 字体大小
  • 字体颜色
  • 字体粗细

二、官方文档

def putText(img, text, org, fontFace, fontScale, color, thickness=None, lineType=None, bottomLeftOrigin=None): # real signature unknown; restored from __doc__
    """
    putText(img, text, org, fontFace, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]]) -> img
    .   @brief Draws a text string.
    .   
    .   The function cv::putText renders the specified text string in the image. Symbols that cannot be rendered
    .   using the specified font are replaced by question marks. See #getTextSize for a text rendering code
    .   example.
    .   
    .   @param img Image.
    .   @param text Text string to be drawn.
    .   @param org Bottom-left corner of the text string in the image.
    .   @param fontFace Font type, see #HersheyFonts.
    .   @param fontScale Font scale factor that is multiplied by the font-specific base size.
    .   @param color Text color.
    .   @param thickness Thickness of the lines used to draw a text.
    .   @param lineType Line type. See #LineTypes
    .   @param bottomLeftOrigin When true, the image data origin is at the bottom-left corner. Otherwise,
    .   it is at the top-left corner.
    """
    pass

三、使用举例

下面举例把文字添加到摄像头中

import cv2
cap = cv2.VideoCapture(0)
while (1):  
    ret, img = cap.read()
    start = time.time()
    result = discern(img)
    end = time.time()
    seconds = end - start  # 处理一帧所用的时间
    fps = 1 / seconds  # 一秒钟可以处理多少帧
    fps = "%.2f fps"%fps
    print(fps)

    # 图片 添加的文字 位置 字体 字体大小 字体颜色 字体粗细
    cv2.putText(result, fps, (5,50 ), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2)
    cv2.imshow("image", result)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
cap.release()  
cv2.destroyAllWindows()  

结果如下(ps:还是不要漏出自己的丑照,免得影响你们学习)
在这里插入图片描述

在这里插入图片描述


在这里插入图片描述


在这里插入图片描述
♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠

评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值