【图像处理】opencv 动态在右上角写自定义的字

news/2024/7/21 4:18:16 标签: 图像处理, opencv, 人工智能

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()

在这里插入图片描述


http://www.niftyadmin.cn/n/5472193.html

相关文章

c#让不同的工厂生产不同的“鸭肉”

任务目标 实现对周黑鸭工厂的产品生产统一管理,主要产品包括鸭脖和鸭翅。武汉工厂能生生产鸭脖和鸭翅,南京工厂只能生产鸭翅,长沙工厂只能生产鸭脖。 分析任务 我们需要有武汉工厂、南京工厂、长沙工厂的类,类中需要实现生产鸭…

c/c++之编译链接

了解我们写的代码是如何转变成可执行文件.exe的是很有必要的,我们将这些底层的东西掌握清楚才能打好基础,筑高楼。 编译链接的全流程 我们平时写代码的文件是.c或者.cpp文件。这里面包括我们的代码,还有宏定义,引用头文件以及注…

洛谷 P4554 小明的游戏

思路:双端队列。 其实一开始你可以用BFS进行实验,由于我们需要找最小的费用,所以我们在BFS的时候可以这样想:在我们遍历到第一块板子的时候,在找周围的路时,我们可以改成这样的判断:如果周围的…

Berkeley CS

Eta Kappa Nu (HKN), Mu Chapter61 A计算机科学 61A — 计算机程序的结构和解释(4 学分) Python4 61 B计算机科学 61B — 数据结构(4 学分) Java4 61 C计算机科学 61C — 机器结构(4 学分)4 CS 70计算机…

Visual Studio安装下载进度为零已解决

因为在安装pytorch3d0.3.0时遇到问题,提示没有cl.exe,VS的C编译组件,可以添加组件也可以重装VS。查了下2019版比2022问题少,选择了安装2019版,下面是下载安装时遇到的问题记录,关于下载进度为零网上有三类解…

2024年浙江省中等职业学校职业能力大赛“网络建设与运维”比赛通知

浙江省中等职业学校职业能力大赛组委会办公室关于举办2024年浙江省中等职业学校职业能力大赛(学生技术技能类)“网络建设与运维”项目比赛的通知 各设区市教育局、有关学校: 根据《浙江省中等职业学校职业能力大赛组委会关于做好2024年浙江…

Java NIO Selector选择器源码分析

文章目录 前言Selector类结构Selector抽象类AbstractSelectorSelectorImplWindowsSelectorImpl三种SelectionKey集合 前言 Java NIO(New I/O)的Selector选择器是一个用于多路复用(Multiplexing)的I/O操作的关键组件。它允许一个单…

【二分与前缀和】python例题详解

文章目录 1、数的范围 2、数的三次方根 3、前缀和 4、子矩阵的和 5、机器人跳跃问题 1、数的范围 题目 给定一个按照升序排列的长度为 n 的整数数组,以及 q 个查询。对于每个查询,返回一个元素 k的起始位置和终止位置(位置从 00 开始计…