OpenCV Basics Tutorial - draw text cv :: getTextSize [2]

Text drawing functions
Function name description
cv :: putText () Draws the specified text in the picture
cv::getTextSize() Gets a character width and height

Capturing text width and height [cv :: getTextSize]

Detailed API follows

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

cv :: getTextSize () function can be achieved if the text will be drawn out much, without actually draw text to the map

The only new parameter cv :: getTextSize () is baseLine, which is actually an output parameter, baseLine is the y-coordinate value and the lowest point of the text relevant text baseline

#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 Basics Tutorial - draw text cv :: putText [1] See the following URL

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

 

 

Published 12 original articles · won praise 27 · views 782

Guess you like

Origin blog.csdn.net/Gary_ghw/article/details/103746709