OpenCV基础教程——绘制文字2【cv::getTextSize】

文字绘制函数
函数名称 描述
cv::putText() 在图片上绘制指定文字
cv::getTextSize() 获取一个文字的宽度和高度

获取文字宽度与高度【cv::getTextSize】

API详解如下

cv::Size cv::getTextSize(
    const string& text,
    cv::Point origin,
    int fontFace,
    double fontScale,
    int thickness,
    int* baseLine
);

cv::getTextSize()函数能够实现如果把文字绘制出来将有多大,而不用实际将文字绘制到图上

cv::getTextSize()唯一的新的参数就是baseLine,这实际上是一个输出参数,baseLine是和文字最低点相关的文字基线的y坐标值

#include <iostream>
#include <opencv2\opencv.hpp>

using namespace std;
using namespace cv;

int main()
{
    string text = "Hello Kobe!";

    int fontFace = FONT_HERSHEY_SCRIPT_COMPLEX;
    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;
    Point textOrg((img.cols - textSize.width) / 2, (img.rows - textSize.height) / 2);
    rectangle(img, textOrg + Point(0, baseline), textOrg + Point(textSize.width, -textSize.height), Scalar(0, 0, 255));
    line(img, textOrg + Point(0, thickness), textOrg + Point(textSize.width, thickness), Scalar(0, 0, 255));
    putText(img, text, textOrg, fontFace, fontScale, Scalar::all(255), thickness, 8);
    imshow("kobe", img);

    waitKey(0);
    return 0;
}

OpenCV基础教程——绘制文字1【cv::putText】见下面网址

https://blog.csdn.net/Gary_ghw/article/details/103746662

发布了12 篇原创文章 · 获赞 27 · 访问量 782

猜你喜欢

转载自blog.csdn.net/Gary_ghw/article/details/103746709