@[toc]
1、鼠标事件
1.1、setMouseCallback()
函数
void setMouseCallback(const String& winname, MouseCallback onMouse, void* userdata = 0);
- winname: 窗口名称
- OnMouse : 鼠标事件的回调函数
- userdata : 传递给回调函数的参数,可选
typedef void (*MouseCallback)(int event, int x, int y, int flags, void* userdata);
参数说明:
- event: 鼠标事件
- x : 鼠标事件的x坐标
- y : 鼠标事件的y坐标
- flags: 鼠标事件的标志
- userdata : 传递给回调函数的参数,可选
1.2、event事件
- CV_EVENT_MOUSEMOVE :鼠标移动
- CV_EVENT_LBUTTONDOWN : 鼠标左键按下
- CV_EVENT_RBUTTONDOWN : 鼠标右键按下
- CV_EVENT_MBUTTONDOWN : 鼠标中键按下
- CV_EVENT_LBUTTONUP : 鼠标左键放开
- CV_EVENT_RBUTTONUP : 右键放开
- CV_EVENT_MBUTTONUP : 中键放开
- CV_EVENT_LBUTTONDBLCLK : 左键双击
- CV_EVENT_RBUTTONDBLCLK : 右键双击
- CV_EVENT_MBUTTONDBLCLK : 中键双击
- CV_EVENT_MOUSEWHEEL : 鼠标向前(+)或向后(-)滑动
- CV_EVENT_MOUSEHWHEEL : 鼠标向右(+)或向左(-)滑动
1.3、flags
- CV_EVENT_FLAG_LBUTTON :左键拖拽
- CV_EVENT_FLAG_RBUTTON : 右键拖拽
- CV_EVENT_FLAG_MBUTTON : 中键拖拽
- CV_EVENT_FLAG_CTRLKEY : Ctrl按下不放
- CV_EVENT_FLAG_SHIFTKEY : shift按下不放
- CV_EVENT_FLAG_ALTKEY : alt按下不放
1.4、示例1-双击画圆
目标
双击鼠标左键时,在图像上以当前鼠标点为圆心绘制圆心和空心圆
代码
#include <QCoreApplication>
#include "opencv2/opencv.hpp"
using namespace cv;
Mat src = imread("./../../images/book.bmp");
//鼠标事件回调函数,双击左键时,以鼠标点为圆心,半径为100,画空心圆
void mouseCallBack(int event,int x,int y, int flags, void *data){
if (event == EVENT_LBUTTONDBLCLK){
//画圆
circle(src,Point(x,y),100,Scalar(255,0,0),1);
//画圆心
circle(src,Point(x,y),1,Scalar(255,0,0),-1);
}
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
//创建一个窗口
namedWindow("mouseDemo");
//鼠标事件回调
setMouseCallback("mouseDemo",mouseCallBack);
while (1) {
imshow("mouseDemo",src); //显示
if(waitKey(20) == 'q'){ //按q退出
break;
};
}
destroyAllWindows();
return 0;
}
结果
1.5、示例2-拖拽画矩形和园
目标
- modeFlag为真时,按下并拖拽鼠标画矩形
- modeFlag为假时,按下并拖拽鼠标时画连续的圆
代码
#include <QCoreApplication>
#include <QString>
#include "opencv2/opencv.hpp"
using namespace cv;
bool drawingFlag = false;//按下鼠标为真
bool modeFlag = true; //为真时绘制矩形,如果要绘制圆,把他设置为false即可
int ix = -1;
int iy = -1;
Mat src = imread("./../../images/book.bmp");
//鼠标事件回调函数
void mouseCallBack(int event,int x,int y, int flags, void *data){
if (event == EVENT_LBUTTONDOWN){ //鼠标左键按下
drawingFlag = true;
ix = x;
iy = y;
}
else if(event == EVENT_MOUSEMOVE){ //鼠标移动
if(drawingFlag == true){ //鼠标左键按下
if(modeFlag == true){ //画矩形
//以当前点坐标为矩形左上角,按下点坐标为矩形右下角,画矩形
rectangle(src,Point(ix,iy),Point(x,y),Scalar(0,255,0),-1);
}
else{ //画圆
circle(src,Point(x,y),50,Scalar(255,0,0),1);
//画圆心
circle(src,Point(x,y),1,Scalar(255,0,0),-1);
}
}
}
else if(event == EVENT_LBUTTONUP){ //鼠标左键松开
drawingFlag = false;
if(modeFlag == true){
//以当前点坐标为矩形左上角,按下点坐标为矩形右下角,画矩形
rectangle(src,Point(ix,iy),Point(x,y),Scalar(0,255,0),-1);
}
else{ //画圆
circle(src,Point(x,y),50,Scalar(255,0,0),1);
//画圆心
circle(src,Point(x,y),1,Scalar(255,0,0),-1);
}
}
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
namedWindow("mouseDemo");
//鼠标
setMouseCallback("mouseDemo",mouseCallBack);
while (1) {
imshow("mouseDemo",src);
if(waitKey(20) == 'q'){
break;
};
}
destroyAllWindows();
return 0;
}
结果