Advertisement
dan-masek

Untitled

Sep 1st, 2018
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.86 KB | None | 0 0
  1. #include <opencv2/opencv.hpp>
  2. #include <iostream>
  3. // ============================================================================
  4. struct mouse_state
  5. {
  6.     mouse_state()
  7.         : position(-1, -1)
  8.         , left_button_held(false)
  9.         , left_button_clicked(false)
  10.     {}
  11.  
  12.     void new_iteration()
  13.     {
  14.         left_button_clicked = false;
  15.     }
  16.  
  17.     cv::Point2i position; // Last position where the LMB was down
  18.     bool left_button_held; // Is the LMB down right now?
  19.     bool left_button_clicked; // Was the LMB down in the last iteration?
  20. };
  21. // ============================================================================
  22. void mouse_callback(int event, int x, int y, int flag, void* param)
  23. {
  24.     mouse_state* state(static_cast<mouse_state*>(param));
  25.  
  26.     if (event == cv::EVENT_LBUTTONDOWN) {
  27.         std::cout << "LMB down @ (" << x << "," << y << ")\n";
  28.         state->position = cv::Point2i(x, y);
  29.         state->left_button_held = true;
  30.     } else if (event == cv::EVENT_LBUTTONUP) {
  31.         std::cout << "LMB up @(" << x << "," << y << ")\n";
  32.         state->position = cv::Point2i(x, y);
  33.         state->left_button_held = false;
  34.         state->left_button_clicked = true;
  35.     } else if ((flag == cv::EVENT_FLAG_LBUTTON) && (event == cv::EVENT_MOUSEMOVE)) {
  36.         std::cout << "LMB held, mouse moved to (" << x << "," << y << ")\n";
  37.         state->position = cv::Point2i(x, y);
  38.     }
  39. }
  40. // ----------------------------------------------------------------------------
  41. int main(int argc, char** argv)
  42. {
  43.     cv::String const WINDOW_NAME("Original Feed");
  44.     cv::namedWindow(WINDOW_NAME, CV_WINDOW_AUTOSIZE);
  45.  
  46.     mouse_state ms;
  47.     cv::setMouseCallback(WINDOW_NAME, mouse_callback, &ms);
  48.    
  49.     int const FRAME_WIDTH(720);
  50.     int const FRAME_HEIGHT(540);
  51.  
  52.     cv::VideoCapture cap(0);
  53.     if (!cap.isOpened()) {
  54.         std::cerr << "Unable to open camera, exiting...\n";
  55.         return -1;
  56.     }
  57.  
  58.     cap.set(CV_CAP_PROP_FRAME_WIDTH, FRAME_WIDTH);
  59.     cap.set(CV_CAP_PROP_FRAME_HEIGHT, FRAME_HEIGHT);
  60.  
  61.     for (;;) {
  62.         // We can have `image` here, since `cap.read` always gives us
  63.         // a reference to its internal buffer
  64.         cv::Mat image;
  65.         if (!cap.read(image)) {
  66.             std::cerr << "Failed to read image, exiting...\n";
  67.             break; // Failed to read image, nothing else to do
  68.         }
  69.  
  70.         if (ms.left_button_clicked || ms.left_button_held) {
  71.             cv::circle(image, ms.position, 3, cv::Scalar(0, 0, 255));
  72.             cv::circle(image, ms.position, 10, cv::Scalar(0, 0, 255), 2);
  73.         }
  74.  
  75.         ms.new_iteration();
  76.  
  77.         cv::imshow(WINDOW_NAME, image);
  78.         if (cv::waitKey(30) == 27) {
  79.             std::cout << "Esc pressed, exiting...\n";
  80.             break;
  81.         }
  82.     }
  83.     return 0;
  84. }
  85. // ============================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement