如何在 C++ 中使用 OpenCV 處理滑鼠事件?
滑鼠事件是 OpenCV 中最實用的功能之一。在 OpenCV 中,我們可以追蹤滑鼠指標的位置以及點選(右鍵、左鍵和中鍵)。OpenCV 在機器人學和計算機視覺中有著廣泛的應用。在機器人學和計算機視覺中,追蹤滑鼠指標和點選被廣泛使用。
本文將瞭解如何在影像上追蹤滑鼠指標的位置和點選。
以下程式展示瞭如何追蹤滑鼠指標的位置和點選。
示例
#include<iostream> #include<opencv2/highgui/highgui.hpp> #include<opencv2/imgproc/imgproc.hpp> using namespace std; using namespace cv; void locator(int event, int x, int y, int flags, void* userdata){ //function to track mouse movement and click// if (event == EVENT_LBUTTONDOWN){ //when left button clicked// cout << "Left click has been made, Position:(" << x << "," << y << ")" << endl; } else if (event == EVENT_RBUTTONDOWN){ //when right button clicked// cout << "Rightclick has been made, Position:(" << x << "," << y << ")" << endl; } else if (event == EVENT_MBUTTONDOWN){ //when middle button clicked// cout << "Middleclick has been made, Position:(" << x << "," << y << ")" << endl; } else if (event == EVENT_MOUSEMOVE){ //when mouse pointer moves// cout << "Current mouse position:(" << x << "," << y << ")" << endl; } } int main() { Mat image = imread("bright.jpg");//loading image in the matrix// namedWindow("Track");//declaring window to show image// setMouseCallback("Track", locator, NULL);//Mouse callback function on define window// imshow("Track", image);//showing image on the window// waitKey(0);//wait for keystroke// return 0; }
輸出
廣告