python中cv2.putText和cv2.getTextSize
cv2.putText简单参数介绍
下面是官方文档:
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)
cv2.getTextSize得到文本尺寸参数
官方文档:
参考: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)
对cv2.getTextSize的返回值的详细介绍
返回的格式如下,(是以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)
python中cv2.putText和cv2.getTextSize相关推荐
- 为什么OpenCV3在Python中导入名称是cv2
我们来看一下: import cv3 print(cv3.__version__) 输出报错: Traceback (most recent call last):ImportError: No mo ...
- 在Python中使用OpenCV(CV2)对图像进行边缘检测
Modules used: 使用的模块: For this, we will use the opencv-python module which provides us various functi ...
- cv2.imread()、cv2.putText、cv2.imwrite()、cv2.waitKey()
cv2 cv2.imread() cv2.putText() cv2.imwrite() cv2.waitKey() cv2.imread() 用于读取图像数据 案例演示: import cv2# o ...
- OpenCV绘图函数:cv2.line、cv2.circle、cv2.rectangle、cv2.ellipse、 cv2.putText()、cv2.setMouseCallback
OpenCV是一个用于图像处理.分析.机器视觉方面的开源函数库. 不管你是做科学研究,还是商业应用,opencv都能够作为你理想的工具,它可以运行在Linux.Windows.Android和Mac ...
- python灰度图cv2到plt变颜色_python中plt.imshow与cv2.imshow显示颜色问题
python中plt.imshow与cv2.imshow显示颜色问题 在用plt.imshow和cv2.imshow显示同一幅图时可能会出现颜色差别很大的现象. 这是因为:opencv的接口使用BGR ...
- opencv python 中cv2.putText()函数的用法
opencv python 中cv2.putText()函数的用法 文章目录: 一.快速使用 二.官方文档 三.使用举例 虽然用啦很多次,还是决定记录一下 一.快速使用 cv2.putText(ima ...
- python中的字体英文名_对python opencv 添加文字 cv2.putText 的各参数介绍
如下所示: cv2.putText(img, str(i), (123,456)), font, 2, (0,255,0), 3) 各参数依次是:图片,添加的文字,左上角坐标,字体,字体大小,颜色,字 ...
- python中cv2.putText参数详解
cv2.putText(img, str(i), (123,456)), font, 2, (0,255,0), 3) 各参数依次是:图片,添加的文字,左上角坐标,字体,字体大小,颜色,字体粗细 其中 ...
- python opencv 如何给图片添加文字?cv2.putText() PIL
参考文章1:python如何在图片上添加文字(中文和英文)Python在图片上添加文字的两种方法:OpenCV和PIL 参考文章2:python之------如何在图片上面添加文字(多种类型的文字)[ ...
最新文章
- linux wireshark使用教程,技术|Ubuntu 上 Wireshark 的安装与使用
- Smart Card知识
- GlusterFS的安装及使用
- 使用gradle构建android项目,Android中使用Gradle来构建App项目的入门指南
- python第三方库有哪些常用的、请列举15个-python基础面试常见题
- 算法测试及对比度进一步增强
- mysql 插入毫秒数据_【转载】怎样在mybatis里向mysql中插入毫秒数的时间?
- Qt4访问sqlite数据库
- 学以致用六---Centos7.2+python3.6.2+django2.1.1 --搭建一个网站
- 《零基础入门学习Python》学习过程笔记【32,33,34异常处理】(没看)
- vb6.0中的Private Declare Function的含义
- unity案例星际迷航_《星际迷航》:自1964年以来启发人们和他们的技术
- Godaddy SSL证书解析到阿里云后配置nginx服务器https
- 实用的CAD技巧,你也可以成为大神!
- SQL SERVER 修改数据库名称(包括 db.mdf 名称的修改)
- 2022年MinGW-w64的安装及配置教程(傻瓜式)
- Unity 大气特效插件分析 - Aura #01
- 在linux安装java过程_挑战Java在Linux上安装过程分享
- matlab极点怎么输入法,[转载]MatLab中的极点配置方法
- 锁机制:读者写者问题 Linux C