Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.