Python OpenCV Mouse Events Introduction

In this Python OpenCV article we are going to talk about Python OpenCV Mouse Events Introduction, so OpenCV (Open Source Computer Vision Library) is an open source computer vision and machine learning software library. It is designed to help developers create real time computer vision applications. OpenCV provides different functions and tools to process and manipulate images and videos, perform object detection and recognition, track moving objects, extract features from images and many more. it is written in C++ and has bindings for many other programming languages, including Python, Java and MATLAB. OpenCV is widely used in academic research, robotics, autonomous vehicles and different industries including healthcare, security and entertainment.

 

 

To install OpenCV for Python you can use pip.

 

 

 

Mouse Events in Python OpenCV

Mouse events in Python OpenCV refer to the actions taken by the user when they interact with the mouse on the displayed image or video using the OpenCV library. these events include clicking, double-clicking, dragging, scrolling and releasing the mouse button.

In OpenCV you can set up mouse callback function that will be called whenever a mouse event occurs. this function can be used to perform various actions, such as drawing shapes, selecting regions of interest, tracking objects or processing the image based on the location of the mouse cursor.

 

These are the different types of mouse events that you can handle in OpenCV:

  • cv2.EVENT_MOUSEMOVE: mouse pointer moves over the window.
  • cv2.EVENT_LBUTTONDOWN: left mouse button is pressed.
  • cv2.EVENT_LBUTTONUP: The left mouse button is released.
  • cv2.EVENT_RBUTTONDOWN: right mouse button is pressed.
  • cv2.EVENT_RBUTTONUP: The right mouse button is released.
  • cv2.EVENT_MBUTTONDOWN: middle mouse button is pressed.
  • cv2.EVENT_MBUTTONUP: The middle mouse button is released.
  • cv2.EVENT_MOUSEWHEEL: mouse wheel is scrolled up or down.
  • cv2.EVENT_MOUSEHWHEEL: mouse wheel is scrolled horizontally.

To handle mouse events in OpenCV, you can use the cv2.setMouseCallback() function to set up callback function that will be called whenever mouse event occurs. callback function should take four parameters: the event type, the x and y coordinates of the mouse cursor and some optional user defined data.

 

 

 

So this is the complete code for Mouse Events in OpenCV.

So in the above code first we have imported OpenCV with Numpy, in this article we are using an empty image that we have created with Numpy. after that we have created our drawCircle()  method , and in this method we are going to check that if the user double click on left mouse button , if that was the case we are going to draw a circle using cv2.circle() we add some information like x and y position the radius of circle and the color of the circle.

 

 

This is the method that we are checking, if the user double click on left mouse, after double clicking we are drawing the circle with the features that we want for the circle.

 

 

 

 

 

If you run the code this will be the output, you need to click on the screen to draw for drawing of the circle.

Python OpenCV Mouse Events Introduction
Python OpenCV Mouse Events Introduction

 

 

 

Also you can check Python GUI Development Tutorials in the below link.

 

 

 

Also you can watch the complete video for this article

Subscribe and Get Free Video Courses & Articles in your Email

 

Leave a Comment

Codeloop
Share via
Copy link
Powered by Social Snap
×