OpenCV界面(CVUI,跨平台,比QT方便,包括按钮等)

惯例先放结果:

================评价====================

好方便啊好方便,只需要#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

要在Python的OpenCV可视化界面中添加按钮,可以使用cv2.createButton()函数。这个函数的语法如下: ```python cv2.createButton(buttonName, onButtonCallback, userData, buttonType) ``` 其中,参数含义如下: - buttonName:按钮的名称,即显示在按钮上的文本; - onButtonCallback:按下按钮后执行的回调函数; - userData:回调函数的参数,可以是任意类型的对象; - buttonType:按钮类型,有以下几种类型可选: - cv2.QT_PUSH_BUTTON:普通按钮; - cv2.QT_CHECKBOX:复选框; - cv2.QT_RADIOBOX:单选框。 下面是一个例子,展示如何在OpenCV可视化界面中添加一个普通按钮: ```python import cv2 # 回调函数 def on_button_click(state, *args): print("Button clicked! State:", state) # 创建按钮 cv2.namedWindow("Window") cv2.createButton("Button", on_button_click, None, cv2.QT_PUSH_BUTTON) # 显示图像 img = cv2.imread("image.jpg") cv2.imshow("Window", img) cv2.waitKey(0) # 关闭窗口 cv2.destroyAllWindows() ``` 在这个例子中,我们先定义了一个回调函数`on_button_click()`,用于在按钮被按下时执行。然后,我们创建了一个名为“Window”的窗口,并在其中添加了一个名为“Button”的普通按钮,并将回调函数`on_button_click()`与它绑定。最后,我们显示了一张图像,并等待按下任意键后关闭窗口。 当我们运行这个代码时,会弹出一个名为“Window”的窗口,其中包含一个名为“Button”的普通按钮。当我们点击这个按钮时,回调函数`on_button_click()`会被执行,并输出一条消息。
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

朱铭德

五毛也是爱٩(●´৺`●)૭

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值