OpenCV图形用户界面
滑块与鼠标事件
鼠标回调事件:
- EVENT_MOUSEMOVE 移动鼠标
- EVENT_LBUTTONDOWN 单击左键
- EVENT_RBUTTONDOWN 单击右键
- EVENT_MBUTTONDOWN 单击中键
- EVENT_LBUTTONUP 释放左键
- EVENT_RBUTTONUP 释放右键
- EVENT_MBUTTONUP 释放中键
- EVENT_LBUTTONDBLCLK 双击左键
- EVENT_RBUTTONDBLCLK 双击右键
- EVENT_MBUTTONDBLCLK 双击中键
- EVENT_MOUSEWHEEL 滚轮垂直滚动
- EVENT_MOUSEHWHEEL 滚轮水平滚动
结合了滤波以及画图的调用事件代码
#include<opencv2/imgproc.hpp>
#include<opencv2/highgui.hpp>
#include<iostream>
using namespace cv;
using namespace std;
int blurAmount = 15;
static void onChange(int pos, void* userInput);
static void onMouse(int event, int x, int y, int, void* userInput);
int main(int, char** argv) {
// read image
Mat image = imread("C:/Users/86177/Desktop/opencv c++/目标追踪/image/24.jpg");
// check image
if (!image.data) {
cout << "image missing" << endl;
return -1;
}
// create windows
namedWindow("image", WINDOW_AUTOSIZE);
// create a trackbar
createTrackbar("image", "image", &blurAmount, 30, onChange, &image);
setMouseCallback("image", onMouse, &image);
//wait for a key to exit
waitKey(0);
destroyAllWindows();
return 0;
}
static void onChange(int pos, void* userInput) {
if (pos <= 0) return;
Mat imgBlur;
Mat* img = (Mat*)userInput;
// apply a blur filter
blur(*img, imgBlur, Size(pos, pos));
// show
imshow("image", imgBlur);
}
static void onMouse(int event, int x, int y, int, void* userInput) {
if (event != EVENT_LBUTTONDOWN) return;
Mat* img = (Mat*)userInput;
circle(*img, Point(x, y), 10, Scalar(0, 255, 0), 3);
onChange(blurAmount, img);
}