官方文档链接:https://dovyski.github.io/cvui/components/printf/
printf
cvui::printf() 用于渲染一段可以使用 C printf() 样式格式化的文本。函数声明如下:
void printf(cv::Mat& theWhere, int theX, int theY, const char *theFmt, ...);
theWhere 是用于渲染图像的图像/帧,theX 是 X 坐标,theY 是 Y 的坐标,theFmt 是为 stdio 的 printf() 提供的格式化字符串,例如:printf(“Text: %d and %f”, 7, 3.1415)
cvui::printf() 几乎和 C语言 的 printf() 函数一样使用。下面是示例以及输出结果。
核心语句
double pi = 3.1415926;
cvui::printf(frame, 100, 80, "PI = %.7f", pi);
完整代码
#define CVUI_IMPLEMENTATION
#define CVUI_DISABLE_COMPILATION_NOTICES
#include "cvui.h"
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/imgcodecs/imgcodecs.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#define WINDOW_NAME "CVUI Test"
int main(int argc, char** argv)
{
cvui::init(WINDOW_NAME);
cv::Mat frame = cv::Mat(cv::Size(300, 200), CV_8UC3);
frame = cv::Scalar(49, 52, 200);
double pi = 3.1415926;
cvui::printf(frame, 100, 80, "PI = %.7f", pi);
cvui::imshow(WINDOW_NAME, frame);
cv::waitKey(0);
return 0;
}
运行结果
Text size and color
It is possible to customize the size and color of the text produced by cvui::printf(). The following function signature can be used in that case:
还可以自定义由 cvui::printf() 生成的文本的大小和颜色。在这种情况下,可以使用以下函数:
void printf (
cv::Mat& theWhere,
int theX,
int theY,
double theFontScale,
unsigned int theColor,
const char *theFmt,
...
);
where theWhere is the image/frame where the image will be rendered, theX is the position X, theY is the position Y, theFontScale is the size of the text, theColor is the color of the text in the format 0xRRGGBB, e.g. 0xff0000 for red, and theFmt is the formating string as it would be supplied for stdio’s printf(), e.g. “Text: %d and %f”, 7, 3.1415.
theWhere 是用于渲染的图像的图像/帧,theX 是 X 坐标,theY 是 Y 坐标,theFontScale 是文本的字号,theColor 是文本的颜色,格式为 0xRRGGBB,例如 0xff0000 表示红色,theFmt 是为 stdio 的 printf() 提供的格式化字符串,例如:printf(“Text: %d and %f”, 7, 3.1415)。
Below is an example of a text with customized size and color. Result on the screen is shown in Figure 2.
如下为具体示例和输出结果:
核心语句
double pi = 3.1415926;
cvui::printf(frame, 100, 80, 1.2, 0xffff00, "PI = %.7f", pi);
完整代码
#define CVUI_IMPLEMENTATION
#define CVUI_DISABLE_COMPILATION_NOTICES
#include "cvui.h"
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/imgcodecs/imgcodecs.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#define WINDOW_NAME "CVUI Test"
int main(int argc, char** argv)
{
cvui::init(WINDOW_NAME);
cv::Mat frame = cv::Mat(cv::Size(600, 200), CV_8UC3);
frame = cv::Scalar(49, 52, 200);
double pi = 3.1415926;
cvui::printf(frame, 100, 80, 1.2, 0xffff00, "PI = %.7f", pi);
cvui::imshow(WINDOW_NAME, frame);
cv::waitKey(0);
return 0;
}
运行结果