CVUI::基於OpenCv的ui庫
這裏介紹一個簡單卻強大的ui庫,CVUI,該ui庫在Opencv的繪圖基礎上,不需要openGL,不需要imgui,只需要一個.h頭文件+Opencv+CMake即可
向該項目原作者致敬,github:https://github.com/Dovyski/cvui
該庫可實現的效果:
一、helloworld測試
cvui庫的helloworld基本流程:
- 初始化 cvui::init()
- 創建背景mat cv::mat
- 設置mat顏色基調 cv::Scalar()
- 創建文本cvui::text()
- 顯示 imshow()
核心函數:
(1)cvui初始化:
void init(const cv::String& theWindowName, int theDelayWaitKey = -1, bool theCreateNamedWindow = true);
- 第一個參數:窗口名字
- 第二個參數:相當於cvwaitkey() ,括號的值,自動調用waitkey()
- 第三個參數:是否由init來創建窗口,即是否自動調用namewindow(),如果填false,則需自己手動namewindow()
(2)文本初始化
void text(cv::Mat& theWhere, int theX, int theY, const cv::String& theText, double theFontScale = 0.4, unsigned int theColor = 0xCECECE);
- 第一個參數:要畫上文本的圖像
- 第二個參數:文本的位置x
- 第三個參數:文本的位置y
- 第四個參數:傳入的文本
- 第五個參數:字體縮放因子
- 第六個參數:文本顏色
測試代碼:
#include <iostream>
using namespace std;
#define CVUI_IMPLEMENTATION
#include "cvui.h"
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<chrono>
using namespace cv;
#define WINDOW_NAME "CVUI Hello World!"
int main()
{
cv::Mat frame = cv::Mat(200, 500, CV_8UC3);
cvui::init(WINDOW_NAME);
while (true) {
// Fill the frame with a nice color
frame = cv::Scalar(49, 52, 49);
// Render UI components to the frame
cvui::text(frame, 110, 80, "Hello, world!");
cvui::text(frame, 110, 120, "cvui is awesome!");
// Update cvui stuff and show everything on the screen
cvui::imshow(WINDOW_NAME, frame);
if (cv::waitKey(20) == 'q') {
break;
}
}
return 0;
}