برای دریافت کلیدهای موس بر روی پنجره OpenCV کار ساده ای هستش. برای اینکار شما نیاز دارید یک تابع callback استفاده کنید و سپس کار بعدی که باید انجام بدید نسبت دادن این تابع به پنجره مورد نظر از طریق نام پنجره هستش. علاوه بر نوع کلید فشرده شده به شما موقعیت موس را هم میده . در اینجا مثالی برای شما قرار دادم که کلید های موس و مختصات را به شما نشان میده.
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace std;
using namespace cv;
void CallBackFunc(int event, int x, int y, int flags, void* userdata)
{
if ( event == EVENT_LBUTTONDOWN )
{
cout << "Left button of the mouse is clicked - position (" << x << ", " << y << ")" << endl;
}
else if ( event == EVENT_RBUTTONDOWN )
{
cout << "Right button of the mouse is clicked - position (" << x << ", " << y << ")" << endl;
}
else if ( event == EVENT_MBUTTONDOWN )
{
cout << "Middle button of the mouse is clicked - position (" << x << ", " << y << ")" << endl;
}
else if ( event == EVENT_MOUSEMOVE )
{
cout << "Mouse move over the window - position (" << x << ", " << y << ")" << endl;
}
}
int main(int argc, char** argv)
{
// Read image from file
Mat img = imread("MyPic.JPG");
//if fail to read the image
if ( img.empty() )
{
cout << "Error loading the image" << endl;
return -1;
}
//Create a window
namedWindow("My Window", 1);
//set the callback function for any mouse event
setMouseCallback("My Window", CallBackFunc, NULL);
//show the image
imshow("My Window", img);
// Wait until user press some key
waitKey(0);
return 0;
}
مهم ترین چیزی که در این کد دیده میشه تابع زیر هستش.
void setMouseCallback(const string& winname, MouseCallback onMouse, void* userdata = 0)
-
پارامتر اول نام پنجره مورد نظر شما
-
پارامتر دوم نام تابع callback ی که نوشتید
-
پارامتر سوم را یک اشاره گر می تونید پاس بدید برای داده های احتمالی که نیاز داشته باشید.
خود تابع callback هم پارامترهاش به شرح زیر :
پارامتر event که نشان دهنده کلید و تعداد کلیک هستش
-
EVENT_MOUSEMOVE
-
EVENT_LBUTTONDOWN
-
EVENT_RBUTTONDOWN
-
EVENT_MBUTTONDOWN
-
EVENT_LBUTTONUP
-
EVENT_RBUTTONUP
-
EVENT_MBUTTONUP
-
EVENT_LBUTTONDBLCLK
-
EVENT_RBUTTONDBLCLK
-
EVENT_MBUTTONDBLCLK
پارامتر دوم و سوم مختصات x و y هستش
پارامتر چهارم فلگ که برای نشان داده کلید های صفحه کلیدی که همزمان با موس فشرده شده
پارامتر پنجم این همان اشاره گری هستش که شما برای اطلاات اضافی پاس میدید.
در زیر یک مثال دیگه براتون میزارم که کلیدهای صفحه کلیدی که همزمان با کلید موس فشرده شده را نشان میده
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace std;
using namespace cv;
void CallBackFunc(int event, int x, int y, int flags, void* userdata)
{
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_RBUTTON + EVENT_FLAG_SHIFTKEY) )
{
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;
}
}
int main(int argc, char** argv)
{
// Read image from file
Mat img = imread("MyPic.JPG");
//if fail to read the image
if ( img.empty() )
{
cout << "Error loading the image" << endl;
return -1;
}
//Create a window
namedWindow("My Window", 1);
//set the callback function for any mouse event
setMouseCallback("My Window", CallBackFunc, NULL);
//show the image
imshow("My Window", img);
// Wait until user press some key
waitKey(0);
return 0;
}
ویرایش :
برای انتقال موقعیت موس از تابع SetCursorPos استفاده نمائید و برای کلیک در موقعیت مشخص از تابع mouse_event به صورت زیر استفاده نمائید.
mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_RIGHTDOWN, nX, nY, 0, 0);
mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_RIGHTUP, nX, nY, 0, 0);