Opencv C++ 滑動塊工具 鼠標點擊事件
圖片路徑:
鏈接:https://pan.baidu.com/s/1TCdforn5chqDqzexYw8pgA
提取碼:7c08
滑動塊工具函數
int cv::createTrackbar (
const String & trackbarname, //滑動塊名字;
const String & winname, //所掛窗口名字;
int * value, //關聯 變量的引用;
int count, //最大值
TrackbarCallback onChange = 0, //回調函數名稱;
void * userdata = 0 //用戶數據; 這裏傳遞圖片數據;
)
鼠標點擊事件
void cv::setMouseCallback (
const String & winname, //窗口名字;
MouseCallback onMouse, //回調函數名稱;
void * userdata = 0 //用戶數據 這裏傳遞圖片數據;
)
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
using namespace cv;
int blurAmount = 15;
//滑塊變更事件;
static void onChange(int pos, void* userInput);
//鼠標響應事件;
static void onMouse(int event, int x, int y, int, void* userInput);
int main()
{
//讀取圖片;
Mat lena = imread("../Data/lena.jpg");
namedWindow("Lena");
//創建滑塊工具 函數;
createTrackbar("Lena", "Lena", &blurAmount, 30, onChange, &lena);
//爲窗口創建 鼠標響應事件;
setMouseCallback("Lena", onMouse, &lena);
//初始化調用 滑塊事件;
onChange(blurAmount, &lena);
//關閉對話框;
waitKey(0);
destroyAllWindows();
system("pause");
return 0;
}
//滑塊變更事件;
static void onChange(int pos, void* userInput)
{
if (pos <= 0) return;
Mat imgBlur;
Mat* img = (Mat*)userInput;
//執行模糊處理;
blur(*img, imgBlur, Size(pos, pos));
//重新顯示模糊圖片;
imshow("Lena", 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);
}