认识调用鼠标操作的api及其参数设定
首先说一下原理:首先是在opencv产生的窗口中设定一个监听事件,用来监听鼠标的操作,鼠标的操作将会被系统捕获,然后返回到opencv中的鼠标的响应函数中去,这个响应函数在定义之初就被绑定到鼠标操作的事件上去了,这样的一个流程下来就形成了用户鼠标操作,在opencv窗口中引起相应的变化的这么一个过程。
1.调用鼠标操作的api
setMouseCallback("鼠标操作", on_draw, (void*)(&img));
2.鼠标操作结束之后系统监听到的事件所定义的鼠标响应函数
static void on_draw(int event, int x, int y, int flag, void* userdata)
下面是完整的代码演示
static void on_draw(int event, int x, int y, int flag, void* userdata)
{
Mat img = *(Mat*)userdata;
if (event == EVENT_LBUTTONDOWN)
{
sp.x = x, sp.y = y;
std::cout << "the start point is (" << sp.x << "," << sp.y << ")"<<endl;
}
else if (event == EVENT_LBUTTONUP)
{
ep.x = x, ep.y = y;
std::cout << "the end point is (" << sp.x << "," << sp.y << ")" << endl;
int dx = ep.x - sp.x;
int dy = ep.y - sp.y;
if (dx > 0 && dy > 0)
{
Rect box(sp.x, sp.y, dx, dy);
rectangle(img, box, Scalar(0, 0, 255), 2, LINE_AA, 0);
imshow("鼠标操作", img);
imshow("ROI区域", img(box));
sp.x = -1, sp.y = -1;
}
}
else if (event == EVENT_MOUSEMOVE)
{
if (sp.x > 0 && sp.y >0 )
{
ep.x = x;
ep.y = y;
int dx = ep.x - sp.x;
int dy = ep.y - sp.y;
if (dx > 0 && dy > 0)
{
Rect box(sp.x, sp.y, dx, dy);
temp.copyTo(img);
rectangle(img, box, Scalar(0,0,255),2,LINE_8,0);
imshow("鼠标操作", img);
}
}
}
}
void QuickDemo::Mouse_drawing(Mat &img)
{
namedWindow("鼠标操作", WINDOW_AUTOSIZE);
temp = img.clone();
setMouseCallback("鼠标操作", on_draw, (void*)(&img));
imshow("鼠标操作", img);
}
|