cvui是一个简单却使用的UI库,除了依赖OpenCV自身以外,无任何其他依赖。
只需要在工程中添加cvui.h,无需编译库即可使用。
cvui的地址:https://github.com/Dovyski/cvui。
#define CVUI_IMPLEMENTATION
#include <opencv2/opencv.hpp>
#include "cvui.h"
#define WINDOW_NAME "CVUI Canny Edge"
int main()
{
cv::Mat lena = cv::imread("lena.jpg");
cv::Mat frame = lena.clone();
int low_threshold = 50;
int high_threshold = 150;
bool use_canny = false;
cv::namedWindow(WINDOW_NAME);
cvui::init(WINDOW_NAME);
while (true)
{
if (use_canny)
{
cv::cvtColor(lena, frame, cv::COLOR_BGR2GRAY);
cv::Canny(frame, frame, low_threshold, high_threshold, 3);
}
else
{
lena.copyTo(frame);
}
cvui::window(frame, 10, 50, 180, 180, "Settings");
cvui::checkbox(frame, 15, 80, "Use Canny Edge", &use_canny);
cvui::trackbar(frame, 15, 110, 165, &low_threshold, 5, 150);
cvui::trackbar(frame, 15, 180, 165, &high_threshold, 80, 300);
cvui::update();
cv::imshow(WINDOW_NAME, frame);
if (cv::waitKey(20) == 27)
{
break;
}
}
return 0;
}
可以选择checkbox并滑动滑条调节canny的阈值。
注:我使用的OpenCV版本是4.1.2,在cvui::window会crash。
最后定位到是这个地方:
解决办法是将cv::rectangle这句代码注释掉。
// Then the filling.
theContent.x++; theContent.y++; theContent.width -= 2; theContent.height -= 2;
//cv::rectangle(aOverlay, theContent, cv::Scalar(0x31, 0x31, 0x31), CVUI_FILLED);
即可解决问题。