惯例先放结果:
================评价====================
好方便啊好方便,只需要#include一个头文件即可!!!!
其实本质上也就是在图片上“画上”了对应的按钮之类的,Github上的一个项目,几百星啦~
-----------------------------------------------------------------------
我们写界面一般都是用Qt啊或者配合MFC之类的搞一些……
但很多时候对平台依赖性比较大,编译支持Qt的Opencv有时候也比较麻烦,这时候就适合cvui登场啦~
===============================================
当然首先需要下载cvui源码: 地址 (当然不缺积分的话,也可以从这里下载: 地址 )
===============================================
先来第一个例子: (按钮的使用,支持图片作为按钮)
代码一目了然,使用非常方便
#include <opencv2/opencv.hpp>
#include "cvui.h"
#define WINDOW_NAME "CVUI Hello World!"
int main(void)
{
cv::Mat frame = cv::Mat(200, 500, CV_8UC3);
int count = 0;
// 初始化窗口
cv::namedWindow(WINDOW_NAME);
cvui::init(WINDOW_NAME);
while (cv::waitKey(30)!=27) {
// 直接填充颜色
frame = cv::Scalar(49, 52, 49);
// 在位置110,80显示按钮
if (cvui::button(frame, 110, 80, "Hello, world!")) {
// 计数
count++;
}
// 在位置(250,90)显示,字号大小为0.4颜色为红色
cvui::printf(frame, 250, 90, 0.4, 0xff0000, "Button click count: %d", count);
//刷新
cvui::update();
// 显示
cv::imshow(WINDOW_NAME, frame);
}
return 0;
}
第二个例子——单选框及滑动条的使用
代码也是很简洁明了
#include <opencv2/opencv.hpp>
#include "cvui.h"
#define WINDOW_NAME "CVUI Canny Edge"
int main(void)
{
cv::Mat pic = cv::imread("test.jpg");
int width = pic.rows, height = pic.cols;
cv::Mat frame = cv::Mat(cv::Size(width + 30, height), 0);
int low_threshold = 50, high_threshold = 150;
bool use_canny = false;
cv::namedWindow(WINDOW_NAME);
cvui::init(WINDOW_NAME);
while (cv::waitKey(30) != 27) {
if (use_canny) {
cv::cvtColor(pic, frame, CV_BGR2GRAY);
cv::Canny(frame, frame, low_threshold, high_threshold, 3);
}
else {
pic.copyTo(frame);
}
cvui::window(frame, 360, 50, 180, 180, "Setting");
cvui::checkbox(frame, 375, 80, "Use Canny Edge", &use_canny);
cvui::trackbar(frame, 375, 110, 165, &low_threshold, 5, 150);
cvui::trackbar(frame, 375, 180, 165, &high_threshold, 80, 300);
cvui::update();
cv::imshow(WINDOW_NAME, frame);
}
return 0;
}
第三个例子——官网Demo,讲解了所有提供功能的用法
详情见代码:
/*
This is a demo application to showcase the UI components of cvui.
Copyright (c) 2016 Fernando Bevilacqua <dovyski@gmail.com>
Licensed under the MIT license.
*/
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "cvui.h"
#define WINDOW_NAME "CVUI Test"
int main(int argc, const char *argv[])
{
cv::Mat frame = cv::Mat(300, 600, CV_8UC3);
bool checked = false;
bool checked2 = true;
int count = 0;
double countFloat = 0.0;
double trackbarValue = 0.0;
// Init a OpenCV window and tell cvui to use it.
// If cv::namedWindow() is not used, mouse events will
// not be captured by cvui.
cv::namedWindow(WINDOW_NAME);
cvui::init(WINDOW_NAME);
while (true) {
// Fill the frame with a nice color
frame = cv::Scalar(49, 52, 49);
// Show some pieces of text.
cvui::text(frame, 50, 30, "Hey there!");
// You can also specify the size of the text and its color
// using hex 0xRRGGBB CSS-like style.
cvui::text(frame, 200, 30, "Use hex 0xRRGGBB colors easily", 0.4, 0xff0000);
// Sometimes you want to show text that is not that simple, e.g. strings + numbers.
// You can use cvui::printf for that. It accepts a variable number of parameter, pretty
// much like printf does.
cvui::printf(frame, 200, 50, 0.4, 0x00ff00, "Use printf formatting: %d + %.2f = %f", 2, 3.2, 5.2);
// Buttons will return true if they were clicked, which makes
// handling clicks a breeze.
if (cvui::button(frame, 50, 60, "Button")) {
std::cout << "Button clicked" << std::endl;
}
// If you do not specify the button width/height, the size will be
// automatically adjusted to properly house the label.
cvui::button(frame, 200, 70, "Button with large label");
// You can tell the width and height you want
cvui::button(frame, 410, 70, 15, 15, "x");
// Window components are useful to create HUDs and similars. At the
// moment, there is no implementation to constraint content within a
// a window.
cvui::window(frame, 50, 120, 120, 100, "Window");
// The counter component can be used to alter int variables. Use
// the 4th parameter of the function to point it to the variable
// to be changed.
cvui::counter(frame, 200, 120, &count);
// Counter can be used with doubles too. You can also specify
// the counter's step (how much it should change
// its value after each button press), as well as the format
// used to print the value.
cvui::counter(frame, 320, 120, &countFloat, 0.1, "%.1f");
// The trackbar component can be used to create scales.
// It works with all numerical types (including chars).
cvui::trackbar(frame, 420, 110, 150, &trackbarValue, 0., 50.);
// Checkboxes also accept a pointer to a variable that controls
// the state of the checkbox (checked or not). cvui::checkbox() will
// automatically update the value of the boolean after all
// interactions, but you can also change it by yourself. Just
// do "checked = true" somewhere and the checkbox will change
// its appearance.
cvui::checkbox(frame, 200, 160, "Checkbox", &checked);
cvui::checkbox(frame, 200, 190, "A checked checkbox", &checked2);
// Display the lib version at the bottom of the screen
cvui::printf(frame, frame.cols - 80, frame.rows - 20, 0.4, 0xCECECE, "cvui v.%s", cvui::VERSION);
// This function must be called *AFTER* all UI components. It does
// all the behind the scenes magic to handle mouse clicks, etc.
cvui::update();
// Show everything on the screen
cv::imshow(WINDOW_NAME, frame);
// Check if ESC key was pressed
if (cv::waitKey(20) == 27) {
break;
}
}
return 0;
}
最后附上丁老板玉照镇楼,233