Last active
June 19, 2020 12:17
-
-
Save RDCH106/39da5eaf2c5994ab4d68124cee08623f to your computer and use it in GitHub Desktop.
OpenCV Mouse Event Handler 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "opencv2/opencv.hpp" | |
#include <iostream> | |
using namespace cv; | |
using namespace std; | |
Mat frame; | |
void mouseEventHandler(int event, int x, int y, int flags, void* param) | |
{ | |
if ( flags == (EVENT_FLAG_CTRLKEY + EVENT_FLAG_LBUTTON) ) | |
{ | |
cout << "Left mouse button is clicked while pressing CTRL key - position (" << x << ", " << y << ")" << endl; | |
} | |
else if ( flags == (EVENT_FLAG_SHIFTKEY + EVENT_FLAG_RBUTTON) ) | |
{ | |
cout << "Right mouse button is clicked while pressing SHIFT key - position (" << x << ", " << y << ")" << endl; | |
} | |
else if ( event == EVENT_MOUSEMOVE && flags == EVENT_FLAG_ALTKEY) | |
{ | |
cout << "Mouse is moved over the window while pressing ALT key - position (" << x << ", " << y << ")" << endl; | |
} | |
} | |
void showWindow() | |
{ | |
//Create an autoresize window | |
namedWindow("My Window", 1); | |
//Set the callback function for any mouse event | |
setMouseCallback("My Window", mouseEventHandler, NULL); | |
//Show the image | |
frame = cv::imread("lena.png"); | |
imshow("My Window", frame); | |
//Wait until user press some key | |
waitKey(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment