#include <opencv2opencv.hpp>
using namespace cv;
struct mouse_para
{
cv::Mat org;
cv::Mat img;
std::string winName = "";
// todo: you can add your own members here.
};
void on_mouse(int event, int x, int y, int flags, void *_ustc) // event鼠标事件代号,x,y鼠标坐标,flags拖拽和键盘操作的代号
{
mouse_para *ustc = static_cast<mouse_para*>(_ustc);
static Point pre_pt = (-1, -1);//初始坐标
static Point cur_pt = (-1, -1);//实时坐标
char temp[16];
if (event == CV_EVENT_LBUTTONDOWN)
{
std::cout << "Left buttom down !" << std::endl;
// todo: you can add your own code here.
}
else if (event == CV_EVENT_MOUSEMOVE && !(flags & CV_EVENT_FLAG_LBUTTON))//左键没有按下的情况下鼠标移动的处理函数
{
std::cout << "(" << x << ", " << y << ")" << std::endl;
// todo: you can add your own code here.
}
else if (event == CV_EVENT_MOUSEMOVE && (flags & CV_EVENT_FLAG_LBUTTON))//左键按下时,鼠标移动
{
std::cout << "Left buttom down and mouse move!" << std::endl;
// todo: you can add your own code here.
}
else if (event == CV_EVENT_LBUTTONUP)//左键松开
{
std::cout << "Left buttom up!" << std::endl;
// todo: you can add your own code here.
}
//ustc->org.copyTo(ustc->img);
//imshow("img", ustc->img);
}
int main()
{
mouse_para mp;
mp.org = cv::Mat::ones(500, 500, CV_8UC1);
mp.org.copyTo(mp.img);
mp.winName = "img";
namedWindow(mp.winName);//定义一个img窗口
setMouseCallback(mp.winName, on_mouse, &mp);//调用回调函数
for (int i=0; i<255;++i)
{
i = i % 250;
mp.org = i*cv::Mat::ones(500,500, CV_8UC1);
mp.org.copyTo(mp.img);
imshow(mp.winName, mp.img);
cv::waitKey(10);
}
return 0;
}