OpenCV笔记-使用getTextSize函数获取绘制文字的包围盒

getTextSize函数

getTextSize函数的函数声明如下:

CV_EXPORTS_W Size getTextSize(const String& text, int fontFace,
                            double fontScale, int thickness,
                            CV_OUT int* baseLine);

getTextSize用于给出opencv绘制文字时,文字的绘制范围,会计算出包围这些文字的包围盒;
主要参数

const String& text待绘制文字内容
int fontFace文字字体,例如FONT_HERSHEY_SCRIPT_SIMPLEX
double fontScale缩放系数
int thickness绘制文本的线条的粗细
CV_OUT int* baseLine对外输出系数,用于给出文字的包围盒的高度值

例子

在一张800*600的图像上,绘制Funny text inside the box文本,绘制文本的包围边框,以及baseline下划线;

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

imshow("Image", img);
waitKey(0);

在这里插入图片描述

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黑山老妖的笔记本

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值