简体   繁体   English

如何在opencv-python中将文本打印到框架

[英]how to print a text to a frame in opencv-python

I am using opencv-python for my face detection experiment.我正在使用 opencv-python 进行人脸检测实验。 I am also to do a cv.drawRect function.我还要做一个 cv.drawRect 函数。 I tried with cv.putText but it is not supported in python.我尝试过 cv.putText 但它在 python 中不受支持。 Is there other functions other than cv.putText which can write text to an image frame除了 cv.putText 之外还有其他函数可以将文本写入图像帧吗

Got it. 得到它了。 In the older version of python-opencv use PutText instead of putText 在旧版本的python-opencv中,使用PutText而不是putText

font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 1, 1, 0, 3, 8) #Creates a font
x = 10 #position of text
y = 20 #position of text
cv.PutText(image,"Hello World!!!", (x,y),font, 255) #Draw the text

In case someone is interested to the new version of opencv-python:如果有人对新版本的 opencv-python 感兴趣:

import cv2

font = cv2.FONT_HERSHEY_DUPLEX
color = (255, 0, 0) # red
fontsize = 255
text = "test"
position = (10, 10)

cv2.putText(image, text, position, font, fontsize, color=color)

The available default fonts are defined in modules/imgproc/include/opencv2/imgproc.hpp header file:可用的默认字体定义在modules/imgproc/include/opencv2/imgproc.hpp头文件中:

enum HersheyFonts {
    FONT_HERSHEY_SIMPLEX        = 0, //!< normal size sans-serif font
    FONT_HERSHEY_PLAIN          = 1, //!< small size sans-serif font
    FONT_HERSHEY_DUPLEX         = 2, //!< normal size sans-serif font (more complex than FONT_HERSHEY_SIMPLEX)
    FONT_HERSHEY_COMPLEX        = 3, //!< normal size serif font
    FONT_HERSHEY_TRIPLEX        = 4, //!< normal size serif font (more complex than FONT_HERSHEY_COMPLEX)
    FONT_HERSHEY_COMPLEX_SMALL  = 5, //!< smaller version of FONT_HERSHEY_COMPLEX
    FONT_HERSHEY_SCRIPT_SIMPLEX = 6, //!< hand-writing style font
    FONT_HERSHEY_SCRIPT_COMPLEX = 7, //!< more complex variant of FONT_HERSHEY_SCRIPT_SIMPLEX
    FONT_ITALIC                 = 16 //!< flag for italic font
};

视觉字体示例

(Image source: Code Yarns ) (图片来源:码纱

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM