VOOZH about

URL: https://www.geeksforgeeks.org/python/click-response-on-video-output-using-events-in-opencv-python/

⇱ Click response on video output using Events in OpenCV – Python - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Click response on video output using Events in OpenCV – Python

Last Updated : 23 Jul, 2025

OpenCV is a computer vision library that contains various functions to perform operations on Images or videos. OpenCV library can be used to perform multiple operations on videos. 

In this article, we will create a program with a click response on video output using events in OpenCV Python library. We will be using “cv2.EVENT_LBUTTONDOWN” in case whenever the left mouse button is clicked and “cv2.EVENT_RBUTTONDOWN” in case whenever the Right mouse button is clicked.

To use the OpenCV library in python, we need to install these libraries as a prerequisite:

  1. Numpy Library: The computer processes images in the form of a matrix for which NumPy is used and  OpenCV uses it in the background.
  2. OpenCV library: OpenCV library previously it was cv but the updated version is cv2. It is used to manipulate images and videos.

To install these libraries, we need to run these pip commands in cmd:

pip install opencv-python
pip install numpy

The steps to read, display video and add click response on video output in OpenCV are:

Step 1:  Import the required modules

Step 2: Create the object of the VideoCapture to read the input file 

cv2.VideoCapture(0): For the first camera or webcam.
cv2.VideoCapture(1): For the second camera or webcam.
cv2.VideoCapture(“file name.mp4”): For video file

Step 3:  Check if the input file is opened or not

: It returns True or False based on whether or not our cap object has started capturing the frames.

Step 4:  Entire file is read frame by frame and set mouse Callback function

cv2.waitkey(): This function of OpenCV allows users to display a window for given milliseconds or until any key is pressed. It takes time in milliseconds as a parameter and waits for the given time to destroy the window, if 0 is passed in the argument it waits till any key is pressed. 

cap.read(): This function returns 2 values:-

  1.  ret: This is a boolean value that is true if the frame is read successfully
  2. frame: This is the actual frame that is read.

A string representing the name of the window in which image to be displayed. 

: It is the image or frame that is to be displayed.

: It doesn’t returns anything.

Step 5:  Define function to handle mouse click

Below is the complete implementation

Output:

👁 Image
 

Note: Video file should have in the same directory where program is executed.

Comment