8. CVUI 2.7.0 組件:Printf (官方文檔翻譯)

官方文檔鏈接: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;
}

運行結果
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章