-
Notifications
You must be signed in to change notification settings - Fork 0
/
canvas-example.cpp
75 lines (57 loc) · 2.51 KB
/
canvas-example.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
This demo showcases some components of cvui being used to control
the application of the Canny edge algorithm to a loaded image.
Code licensed under the MIT license
*/
#include <opencv2/opencv.hpp>
#define CVUI_IMPLEMENTATION
#include "viewer/cvui.h"
#define WINDOW_NAME "CVUI Canvas"
int main(int argc, const char* argv[]) {
cv::Mat lena = cv::imread("../samples/lena.jpg");
cv::Mat frame = lena.clone();
int low_threshold = 50;
int high_threshold = 150;
bool use_canny = false;
int panel_width = 180;
int panel_height = 180;
int pad = 15;
cvui::init(WINDOW_NAME);
while (true) {
cv::Mat canvas = cv::Mat::zeros(cv::Size((frame.cols + panel_width + pad * 2), frame.rows), CV_8UC3);
// Should we apply Canny edge?
if (use_canny) {
// Yes, we should apply it.
cv::cvtColor(lena, frame, cv::COLOR_BGR2GRAY);
cv::Canny(frame, frame, low_threshold, high_threshold, 3);
cv::cvtColor(frame, frame, cv::COLOR_GRAY2BGR);
} else {
// No, so just copy the original image to the displaying frame.
lena.copyTo(frame);
}
// Render the settings window to house the checkbox
// and the trackbars below.
cvui::window(canvas, frame.cols + pad, 50, panel_width, panel_height, "Settings");
// Checkbox to enable/disable the use of Canny edge
cvui::checkbox(canvas, frame.cols + 15, 80, "Use Canny Edge", &use_canny);
// Two trackbars to control the low and high threshold values
// for the Canny edge algorithm.
cvui::trackbar(canvas, frame.cols + 15, 110, 165, &low_threshold, 5, 150);
cvui::trackbar(canvas, frame.cols + 15, 180, 165, &high_threshold, 80, 300);
cvui::printf(canvas, std::lround(frame.cols + 15), std::lround(300), 1.5 * cvui::DEFAULT_FONT_SCALE, 0x00ff00, "Low threshold: %d",
low_threshold);
cvui::printf(canvas, std::lround(frame.cols + 15), std::lround(320), 1.5 * cvui::DEFAULT_FONT_SCALE, 0x00ff00, "High threshold: %d",
high_threshold);
// 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
frame.copyTo(canvas(cv::Rect(0, 0, frame.cols, frame.rows)));
cv::imshow(WINDOW_NAME, canvas);
// Check if ESC was pressed
if (cv::waitKey(30) == 27) {
break;
}
}
return 0;
}