canny.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. This is a demo application to showcase the UI components of cvui.
  3. Code licensed under the MIT license, check LICENSE file.
  4. */
  5. #include <opencv2/opencv.hpp>
  6. #include "cvui.h"
  7. #define WINDOW_NAME "CVUI Canny Edge"
  8. int main(int argc, const char *argv[])
  9. {
  10. cv::Mat lena = cv::imread("lena.jpg");
  11. cv::Mat frame = lena.clone();
  12. int low_threshold = 50, high_threshold = 150;
  13. bool use_canny = false;
  14. // Init a OpenCV window and tell cvui to use it.
  15. // If cv::namedWindow() is not used, mouse events will
  16. // not be captured by cvui.
  17. cv::namedWindow(WINDOW_NAME);
  18. cvui::init(WINDOW_NAME);
  19. while (true) {
  20. // Should we apply Canny edge?
  21. if (use_canny) {
  22. // Yes, we should apply it.
  23. cv::cvtColor(lena, frame, cv::COLOR_BGR2GRAY);
  24. cv::Canny(frame, frame, low_threshold, high_threshold, 3);
  25. } else {
  26. // No, so just copy the original image to the displaying frame.
  27. lena.copyTo(frame);
  28. }
  29. // Render the settings window to house the checkbox
  30. // and the trackbars below.
  31. cvui::window(frame, 10, 50, 180, 180, "Settings");
  32. // Checkbox to enable/disable the use of Canny edge
  33. cvui::checkbox(frame, 15, 80, "Use Canny Edge", &use_canny);
  34. // Two trackbars to control the low and high threshold values
  35. // for the Canny edge algorithm.
  36. cvui::trackbar(frame, 15, 110, 165, &low_threshold, 5, 150);
  37. cvui::trackbar(frame, 15, 180, 165, &high_threshold, 80, 300);
  38. // This function must be called *AFTER* all UI components. It does
  39. // all the behind the scenes magic to handle mouse clicks, etc.
  40. cvui::update();
  41. // Show everything on the screen
  42. cv::imshow(WINDOW_NAME, frame);
  43. // Check if ESC was pressed
  44. if (cv::waitKey(30) == 27) {
  45. break;
  46. }
  47. }
  48. return 0;
  49. }