平时在写代码时可能用到鼠标在图上做标记等其他作用,opencv主要用setMouseCallback()这个函数,下面来讲解:
void setMousecallback(const string& winname, MouseCallback onMouse, void* userdata=0)
winname:窗口的名字
onMouse:鼠标响应函数,回调函数。指定窗口里每次鼠标时间发生的时候,被调用的函数指针。 这个函数的原型应该为void on_Mouse(int event, int x, int y, int flags, void* param);
userdate:传给回调函数的参数
再来介绍MouseCallback onMouse的函数原型:
void on_Mouse(int event, int x, int y, int flags, void* param);
event是 CV_EVENT_*变量之一
x和y是鼠标指针在图像坐标系的坐标(不是窗口坐标系)
flags是CV_EVENT_FLAG的组合, param是用户定义的传递到setMouseCallback函数调用的参数。
#defineCV_EVENT_MOUSEMOVE
#defineCV_EVENT_LBUTTONDOWN
#defineCV_EVENT_RBUTTONDOWN
#defineCV_EVENT_LBUTTONUP
#defineCV_EVENT_RBUTTONUP
和标志位flags有关的:
#defineCV_EVENT_FLAG_LBUTTON
用if()else()方法:
#include
#include
using namespace std;
using namespace cv;
#define WINDOW "原图"
Mat g_srcImage,g_dstImage;
Point previousPoint;
bool P = false;
void On_mouse(int event, int x, int y, int flags, void*);
int main()
{
g_srcImage = imread("D://vvoo//face.jpg", 1);
imshow(WINDOW, g_srcImage);
setMouseCallback(WINDOW, On_mouse, 0);
waitKey(0);
return 0;
}
void On_mouse(int event, int x, int y, int flags, void*)
{
/*if (event == EVENT_LBUTTONUP ||!( flags&EVENT_FLAG_LBUTTON))
{
previousPoint = Point(-1, -1);
}*/
if (event == EVENT_LBUTTONDOWN)
{
previousPoint = Point(x, y);
}
else if (event == EVENT_MOUSEMOVE && (flags&EVENT_FLAG_LBUTTON))
{
Point pt(x, y);
line(g_srcImage, previousPoint, pt, Scalar(0,0,255), 2, 5, 0);
previousPoint = pt;
imshow(WINDOW, g_srcImage);
}
}
void On_mouse(int event, int x, int y, int flags, void*)
{
switch (event)
{
case EVENT_LBUTTONUP:
{
P = false;
}
break;
case EVENT_LBUTTONDOWN:
{
previousPoint = Point(x, y);
P = true;
}
break;
case EVENT_MOUSEMOVE :
{
if(P)
{
Point pt(x, y);
line(g_srcImage, previousPoint, pt, Scalar(0,0,255), 2, 5, 0);
previousPoint = pt;
imshow(WINDOW, g_srcImage);
}
}
break;
}
}
结果:
#include
#include
using namespace std;
using namespace cv;
Rect select;
bool select_flag = false;
Mat img, showImg;
void A_on_Mouse(int event, int x, int y, int flags, void*param)//实现画矩形框
{
Point p1, p2;
if (event == EVENT_LBUTTONDOWN)
{
select.x = x;
select.y = y;
select_flag = true;
}
else if (select_flag &&event == EVENT_MOUSEMOVE)
{
img.copyTo(showImg);
p1 = Point(select.x, select.y);
p2 = Point(x, y);
rectangle(showImg, p1, p2, Scalar(0, 255, 0), 2);
imshow("img", showImg);
}
else if (select_flag && event == EVENT_LBUTTONUP)
{
select_flag = false;
}
}
void B_on_Mouse(int event, int x, int y, int flags, void*param)//实现画矩形框并截图
{
Point p1, p2;
switch (event)
{
case EVENT_LBUTTONDOWN:
{
select.x = x;
select.y = y;
select_flag = true;
}
break;
case EVENT_MOUSEMOVE:
{
if (select_flag)
{
img.copyTo(showImg);
p1 = Point(select.x, select.y);
p2 = Point(x, y);
rectangle(showImg, p1, p2, Scalar(0, 255, 0), 2);
imshow("img", showImg);
}
}
break;
case EVENT_LBUTTONUP:
{
//显示框出的ROI
//改成Rect roi = Rect(p1, p2);为什么不对?
Rect roi = Rect(Point(select.x, select.y), Point(x, y));
if (roi.width && roi.height)//点一下时会没有反应
{
Mat roiImg = img(roi);
imshow("roi", roiImg);
}
select_flag = false;
}
break;
}
}
int main()
{
img = imread("D://vvoo//face.jpg", 1);
showImg = img.clone();
select.x = select.y = 0;
imshow("img", showImg);
while (1)
{
int key = waitKey(10);
switch (key)
{
case 'a':
setMouseCallback("img", A_on_Mouse, 0);
break;
case 'b':
setMouseCallback("img", B_on_Mouse, 0);
break;
}
if (key == 27||key=='q')
break;
}
waitKey(0);
return 0;
}
结果:
根据按键实现两种功能,这里只截图了第二种画矩形框并把它截图功能:
2.http://www.xuebuyuan.com/1528309.html
3.http://www.zybang.com/question/151ef07151a0e64dd54811485a418dcd.html