1.函数介绍
setMouseCallback
CV_EXPORTS void setMouseCallback(const string& winname, MouseCallback onMouse, void* userdata = 0);
函数参数介绍
const string& winname,windows视窗名称,对名为winname的视窗进行鼠标监控。
MouseCallback onMouse,鼠标响应处理函数,监听鼠标的点击,移动,松开,判断鼠标的操作类型,并进行响应的函数处理。
void* userdata = 0 鼠标响应处理函数的ID,与鼠标相应处理函数相匹配就行,暂时只用到默认为0的情况。
函数使用实例:
namedWindow("img");
setMouseCallback("img",on_mouse,0);
2.鼠标相应处理函数
void on_mouse(int event,int x,int y,int flags,void *ustc)
函数参数介绍:
int event,鼠标操作时间的整数代号,在opencv中,event鼠标事件总共有10中,从0-9依次代表如下:
Event:
#define CV_EVENT_MOUSEMOVE 0 滑动
#define CV_EVENT_LBUTTONDOWN 1 左键点击
#define CV_EVENT_RBUTTONDOWN 2 右键点击
#define CV_EVENT_MBUTTONDOWN 3 中间点击
#define CV_EVENT_LBUTTONUP 4 左键释放
#define CV_EVENT_RBUTTONUP 5 右键释放
#define CV_EVENT_MBUTTONUP 6 中间释放
#define CV_EVENT_LBUTTONDBLCLK 7 左键双击
#define CV_EVENT_RBUTTONDBLCLK 8 右键双击
#define CV_EVENT_MBUTTONDBLCLK 9 中间释放
int x,int y,代表鼠标位于窗口的(x,y)坐标位置,窗口左上角默认为原点,向右为x轴,向下为y轴,
int flags,代表鼠标的拖拽事件,以及键盘鼠标联合事件,总共有32种事件,依次如下:
flags:
#define CV_EVENT_FLAG_LBUTTON 1 左键拖拽
#define CV_EVENT_FLAG_RBUTTON 2 右键拖拽
#define CV_EVENT_FLAG_MBUTTON 4 中间拖拽
#define CV_EVENT_FLAG_CTRLKEY 8 (8~15)按Ctrl不放事件
#define CV_EVENT_FLAG_SHIFTKEY 16 (16~31)按Shift不放事件
#define CV_EVENT_FLAG_ALTKEY 32 (32~39)按Alt不放事件(后面8-39还有待研究)
void *ustc,函数参数的编号(暂时用不到)
3.操作实例
#include
#include
#include
#include
using namespace std;
using namespace cv;
cv::Mat org, dst, img, tmp;
void on_mouse(int event, int x, int y, int flags, void *ustc)//event鼠标事件代号,x,y鼠标坐标,flags拖拽和键盘操作的代号
{
static Point pre_pt = (-1, -1);//初始坐标
static Point cur_pt = (-1, -1);//实时坐标
char temp[16];
if (event == CV_EVENT_LBUTTONDOWN)//左键按下,读取初始坐标,并在图像上该点处划圆
{
org.copyTo(img);//将原始图片复制到img中
sprintf(temp, "(%d,%d)", x, y);
pre_pt = Point(x, y);
putText(img, temp, pre_pt, FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 0, 255), 1, 8);//在窗口上显示坐标
circle(img, pre_pt, 2, Scalar(255, 0, 0, 0), CV_FILLED, CV_AA, 0);//划圆
imshow("img", img);
}
else if (event == CV_EVENT_MOUSEMOVE && !(flags & CV_EVENT_FLAG_LBUTTON))//左键没有按下的情况下鼠标移动的处理函数
{
img.copyTo(tmp);//将img复制到临时图像tmp上,用于显示实时坐标
sprintf(temp, "(%d,%d)", x, y);
cur_pt = Point(x, y);
putText(tmp, temp, cur_pt, FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 0, 255));//只是实时显示鼠标移动的坐标
imshow("img", tmp);
}
else if (event == CV_EVENT_MOUSEMOVE && (flags & CV_EVENT_FLAG_LBUTTON))//左键按下时,鼠标移动,则在图像上划矩形
{
img.copyTo(tmp);
sprintf(temp, "(%d,%d)", x, y);
cur_pt = Point(x, y);
putText(tmp, temp, cur_pt, FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 0, 255));
rectangle(tmp, pre_pt, cur_pt, Scalar(0, 255, 0, 0), 1, 8, 0);//在临时图像上实时显示鼠标拖动时形成的矩形
imshow("img", tmp);
}
else if (event == CV_EVENT_LBUTTONUP)//左键松开,将在图像上划矩形
{
org.copyTo(img);
sprintf(temp, "(%d,%d)", x, y);
cur_pt = Point(x, y);
putText(img, temp, cur_pt, FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 0, 255));
circle(img, pre_pt, 2, Scalar(255, 0, 0, 0), CV_FILLED, CV_AA, 0);
rectangle(img, pre_pt, cur_pt, Scalar(0, 255, 0, 0), 1, 8, 0);//根据初始点和结束点,将矩形画到img上
imshow("img", img);
img.copyTo(tmp);
//截取矩形包围的图像,并保存到dst中
int width = abs(pre_pt.x - cur_pt.x);
int height = abs(pre_pt.y - cur_pt.y);
if (width == 0 || height == 0)
{
printf("width == 0 || height == 0");
return;
}
dst = org(Rect(min(cur_pt.x, pre_pt.x), min(cur_pt.y, pre_pt.y), width, height));
namedWindow("dst");
imshow("dst", dst);
waitKey(0);
}
}
int main()
{
org = imread("1.jpg");
org.copyTo(img);
org.copyTo(tmp);
namedWindow("img");//定义一个img窗口
setMouseCallback("img", on_mouse, 0);//调用回调函数
imshow("img", img);
cv::waitKey(0);
}
转自:https://www.cnblogs.com/lidabo/p/3437587.html