OpenCv滑动条与鼠标点击事件
OpenCv鼠标点击事件
#include <opencv2/opencv.hpp>
#include <opencv2/opencv_lib.h>
using namespace std;
using namespace cv;
struct callparamter{
Mat img;
int F;
Point pt;
string window_name;
string trackbar_name;
};
callparamter ptr;
void BGblur_Demo(int F, void *param)
{
//callparamter *ptr = (callparamter*) param;
imshow(ptr.window_name, ptr.img);
ptr.F = getTrackbarPos(ptr.trackbar_name, ptr.window_name);
cout << "F: "<< ptr.F << endl;
}
void on_mouse(int event,int x,int y,int flags,void *ustc)
{
static Point pre_pt = (-1,-1);//初始坐标
static Point cur_pt = (-1,-1);//实时坐标
char temp[16];
if (event == CV_EVENT_LBUTTONDOWN)//左键按下,读取初始坐标,并在图像上该点处划圆
{
//ptr.img.copyTo(img);//将原始图片复制到img中
sprintf(temp,"(%d,%d)",x,y);
pre_pt = Point(x,y);
putText(ptr.img,temp,pre_pt,FONT_HERSHEY_SIMPLEX,0.5,Scalar(0,0,255),1,8);//在窗口上显示坐标
circle(ptr.img,pre_pt,2,Scalar(255,0,0,0),CV_FILLED,CV_AA,0);//划圆
imshow(ptr.window_name,ptr.img);
ptr.pt = pre_pt;
cout << "坐标:" << ptr.pt.x << " " << ptr.pt.y << endl;
}
}
void main()
{
//trackbar
Mat img = imread("left.jpg",1);
string window_name = "win";
string trackbar_name = "Fvalue";
int Fparamter = 1;
int maxF = 5;
ptr.window_name =window_name;
ptr.trackbar_name = trackbar_name;
ptr.img = img.clone();
ptr.F = Fparamter;
namedWindow(window_name,1);
createTrackbar( trackbar_name, window_name, &Fparamter, maxF, BGblur_Demo );
BGblur_Demo(Fparamter, (void*)(&ptr));
//mouseclick
setMouseCallback(ptr.window_name, on_mouse,0);
for(;;)
{
int c;
c = waitKey( 20 );
if( (char)c == 27 )
{
break;
}
}
}
可以同时调节F的数值和点击图像中的鼠标坐标获取位置
参考: