Skip to content

Instantly share code, notes, and snippets.

@RDCH106
Last active June 19, 2020 12:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RDCH106/39da5eaf2c5994ab4d68124cee08623f to your computer and use it in GitHub Desktop.
Save RDCH106/39da5eaf2c5994ab4d68124cee08623f to your computer and use it in GitHub Desktop.
OpenCV Mouse Event Handler 2
#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