鼠标事件响应
void setMouseCallback(const String& winname, MouseCallback onm ouse, void* userdata = 0);
/*******************************************************************
* winname: 监听窗口名称
* onMouse: 鼠标事件回调函数
* userdata: 递给回调函数的可选参数
*********************************************************************/
鼠标事件回调函数
typedef void (*MouseCallback)(int event, int x, int y, int flags, void* userdata);
//MouseCallback onm ouse
void onm ouse(int event,int x,int y,int flag,void *param)
/*******************************************************************
* event: 事件类型
* x: 鼠标所在图像的坐标
* y:
* flags: 代表拖拽事件
* param: 自己定义的onMouse事件的ID
*********************************************************************/
鼠标事件类型
enum MouseEventTypes {
EVENT_MOUSEMOVE = 0, //鼠标移动
EVENT_LBUTTONDOWN = 1, //鼠标左键按下
EVENT_RBUTTONDOWN = 2, //鼠标右键按下
EVENT_MBUTTONDOWN = 3, //鼠标中键按下
EVENT_LBUTTONUP = 4, //鼠标左键弹起
EVENT_RBUTTONUP = 5, //鼠标右键弹起
EVENT_MBUTTONUP = 6, //鼠标中键弹起
EVENT_LBUTTONDBLCLK = 7, //鼠标左键双击
EVENT_RBUTTONDBLCLK = 8, //鼠标右键双击
EVENT_MBUTTONDBLCLK = 9, //鼠标中间双击
EVENT_MOUSEWHEEL = 10, //鼠标滚轮 正数和负数分别表示向前和向后滚动
EVENT_MOUSEHWHEEL = 11 //鼠标滚轮 正数和负数分别表示向右和向左滚动
};
鼠标拖拽类型
enum MouseEventFlags {
EVENT_FLAG_LBUTTON = 1, //左键拖动
EVENT_FLAG_RBUTTON = 2, //右键拖动
EVENT_FLAG_MBUTTON = 4, //中键拖动
EVENT_FLAG_CTRLKEY = 8, //ctr拖动
EVENT_FLAG_SHIFTKEY = 16, //shift拖动
EVENT_FLAG_ALTKEY = 32 //alt拖动
};
综合代码
//Opencv鼠标左键画圆,右键画方
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
class DrawShape
{
public:
DrawShape() :mat(imread("mm.jpg")) {}
void Show(string wName = "drawShape")
{
imshow(wName, mat);
}
void DrawCircle(int x = 300, int y = 300, int r = 10)
{
circle(mat, Point(x, y), r, Scalar(0, 255, 0));
}
void DrawRecagnle(int x = 200, int y = 200, int w = 40, int h = 40)
{
rectangle(mat, Rect(x, y, w, h), Scalar(255, 0, 0));
}
static void onm ouse(int event, int x, int y, int flag, void* param);
protected:
Mat mat;
};
void DrawShape::OnMouse(int event, int x, int y, int flag, void* param)
{
DrawShape* pshape = (DrawShape*)param;
switch(event)
{
case EVENT_LBUTTONDOWN:
cout << "左键按下......." << endl;
pshape->DrawCircle(x,y,10);
break;
case EVENT_RBUTTONDOWN:
cout << "右键按下......." << endl;
pshape->DrawRecagnle(x-5,y-5,10,10);
break;
}
}
int main()
{
DrawShape* pshape = new DrawShape;
namedWindow("drawShape");
setMouseCallback("drawShape", &DrawShape::OnMouse, pshape);
//主循环
while (1)
{
pshape->Show();
if (waitKey(10) == 27)
{
break;
}
}
delete pshape;
pshape = nullptr;
return 0;
}
标签:pshape,鼠标,int,void,C++,OpenCV,DrawShape,EVENT
From: https://blog.51cto.com/u_15959862/6181062