cv2.putText(img, text, org, fontFace, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]])
简单的使用:
主要参数:图片数据,写入字符,基准坐标,字体,字体比例,颜色,粗细等
cv2.putText()
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.
cv2.getTextSize得到文本尺寸参数:text,
fontFace, fontScale, thickness
官方文档:
参考:https://blog.csdn.net/Dontla/article/details/103139195
对cv2.getTextSize的返回值的详细介绍,返回格式如下(width,height),bottom
def getTextSize(text, fontFace, fontScale, thickness): # real signature unknown; restored from __doc__
getTextSize(text, fontFace, fontScale, thickness) -> retval, baseLine
. @brief Calculates the width and height of a text string.
计算文本字符串的宽度和高度。
.
. The function cv::getTextSize calculates and returns the size of a box that contains the specified text.
. That is, the following code renders some text, the tight box surrounding it, and the baseline: :
计算并返回包含指定文本的框的大小。
。 也就是说,以下代码呈现了一些文本,其周围的紧框和基线:
. @code
. String text = "Funny text inside the box";
. int fontFace = FONT_HERSHEY_SCRIPT_SIMPLEX;
. double fontScale = 2;
. int thickness = 3;
.
. Mat img(600, 800, CV_8UC3, Scalar::all(0));
.
. int baseline=0;
. Size textSize = getTextSize(text, fontFace,
. fontScale, thickness, &baseline);
. baseline += thickness;
.
. // center the text 文字居中
. Point textOrg((img.cols - textSize.width)/2,
. (img.rows + textSize.height)/2);
.
. // draw the box 画盒子
. rectangle(img, textOrg + Point(0, baseline),
. textOrg + Point(textSize.width, -textSize.height),
. Scalar(0,0,255));
. // ... and the baseline first 首先是基线
. line(img, textOrg + Point(0, thickness),
. textOrg + Point(textSize.width, thickness),
. Scalar(0, 0, 255));
.
. // then put the text itself 然后把文字本身
. putText(img, text, textOrg, fontFace, fontScale,
. Scalar::all(255), thickness, 8);
. @endcode
.
. @param text Input text string. 输入文字字符串。
. @param fontFace Font to use, see #HersheyFonts. 要使用的字体,请参见#HersheyFonts。
. @param fontScale Font scale factor that is multiplied by the font-specific base size.
字体比例因子,用来被特定字体的基本大小相乘。
. @param thickness Thickness of lines used to render the text. See #putText for details.
用于渲染文本的线的粗细。 有关详细信息,请参见#putText。
. @param[out] baseLine y-coordinate of the baseline relative to the bottom-most text
. point. 基线相对于最底下的文本点的y坐标。
. @return The size of a box that contains the specified text. 包含指定文本的框的大小。
.
. @see putText
pass
简单使用:
主要使用参数:字符,字体,字符比例,粗细
cv2.getTextSize('Fmg_b',cv2.FONT_HERSHEY_SIMPLEX, 1, 2)
对cv2.getTextSize的返回值的详细介绍
返回的格式如下,(是以putText的坐标做为基准点)
(width, height),bottom
# ref:https://blog.csdn.net/weixin_40293999/article/details/137425447
import cv2
import numpy as np
text = "Hello World!!!"
# 要使用的字体
fontFace = cv2.FONT_HERSHEY_SIMPLEX
fontScale = 1
thickness = 2
(width, height), bottom = cv2.getTextSize(text, fontFace, fontScale=fontScale, thickness=thickness)
image = np.zeros((512, 512, 3), np.uint8)
H, W, C = image.shape
cv2.putText(image, text, (W - width, height), fontFace, fontScale, color=(0, 0, 255), thickness=thickness,
lineType=cv2.LINE_AA)
cv2.imshow("image", image)
cv2.waitKey(0)
cv2.destroyAllWindows()