 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to work with Mouse Events using OpenCV in C++?
Mouse Events is one of the most useful features of OpenCV. In OpenCV, we can track the mouse pointer's position and track the clicks (right, left and middle-click). OpenCV has a wide application in robotics and computer vision. In robotics and computer vision tracking mouse pointer and clicks are frequently used.
Here we will understand how to track the mouse pointer's location on an image and track the clicks.
The following program demonstrates how to track the location of the mouse pointer and clicks.
Example
#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; }  Output


Advertisements
 