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