下面是官方文档:
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
简单的使用:
主要参数:图片数据,写入字符,基准坐标,字体,字体比例,颜色,粗细等
cv2.putText(img_arr, 'Fmg_b', (base_x,base_y), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2)
官方文档:
参考:https://blog.csdn.net/Dontla/article/details/103139195
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)
返回的格式如下,(是以putText的坐标做为基准点)
(width,height),bottom
代码中参数的含义:
base_x,base_y:写入文本的左下角基准点坐标
其他参数如图片中标注
import cv2
img_name = r'test.jpg'
img_arr = cv2.imread(img_name,-1)
base_x = 40;base_y=50
cv2.putText(img_arr, 'Fmg_b', (base_x,base_y), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2)
(base_width,base_h),bottom = cv2.getTextSize('Fmg_b',cv2.FONT_HERSHEY_SIMPLEX, 1, 2)
print((base_width,base_h),bottom) #(190, 43) 19
cv2.circle(img_arr,(base_x ,base_y),3,(0,0,255),2,) ## 字符显示基准红色原点,字符左下方
cv2.line(img_arr,(base_x ,base_y),(base_x+base_width ,base_y),(0,0,255),2) # 红色字符下线范围
cv2.line(img_arr,(base_x ,base_y),(base_x ,base_y-base_h),(0,255,0),2) # 绿色高度范围
# 图片中文字的最下方
cv2.line(img_arr,(base_x ,base_y+bottom),(base_x+base_width ,base_y+bottom),(0,255,255),2) # 绿色高度范围
## 保存标注后的图片
cv2.imwrite('./line_picture.jpg',img_arr)
cv2.imshow('text',img_arr)
cv2.waitKey(0)