Click response on video output using Events in OpenCV and Python


OpenCV is an Open Source Computer Vision Library in python. It provides numerous functions to perform various Image or video processing operations. The library uses the python Numpy module to represent all the video or image arrays as a ndarray type.

In this article, we will see click responses on video output using events in the python OpenCV module.

To identify which mouse event occurred on the video output. Here we will use the below mouse event −

  • cv2.EVENT_LBUTTONDOWN: indicates the left button is pressed

  • cv2.EVENT_RBUTTONDOWN: indicates the right button is pressed

Also, we will use the setMouseCallback() function to set the mouse event handler for the specified window

The setMouseCallback() function

The function is used to specify which function has to be called for a specific window. In other words, the function creates a mouse event handler for the specified window. Following is the syntax of this –

cv2.setMouseCallback(winname, onMouse, userdata)

Where,

  • Winname: Name of the specific window.

  • OnMouse: Callback function for mouse events.

  • Userdata: The optional parameter passed to the callback.

On a video file

The cv2.VideoCapture() function will read the video file data. Here we will define a mouse call-back function to identify the click response of video output.

Example

Let’s see an example to detect which click event happened on the video output.

import cv2
  
def check_mouse_event(event, x, y, flags, param):
   # to check if left mouse button was clicked
   if event == cv2.EVENT_LBUTTONDOWN:
      print("left click")
      cv2.imshow("Current Frame for left click", frame)
   
   # to check if right mouse button was clicked
   if event == cv2.EVENT_RBUTTONDOWN:
      print("right click")
      cv2.imshow("Current Frame for Right click", frame)  

cap = cv2.VideoCapture("Videos/blue-tit-2975.mp4")
  
if cap.isOpened() == False:
   # give error message
   print("Error in opening file.")
else:
   while(cap.isOpened()):
      ret, frame = cap.read()
      if ret == True:
         cv2.imshow("Input Window", frame)
         cv2.setMouseCallback('Input Window', check_mouse_event, param=frame)
         if cv2.waitKey(10) & 0xFF == ord('q'):
               break
      else:
         break

cap.release()
cv2.destroyAllWindows()

Input Window

Output – left click

Output – right click

Displayed the current frames of the input video file where the left and right click events are happened.

On a live video

Among all functions, we also use the namedWindow() function to create a window that can be used to copy the place of the images and trackbars. If a window with the same name already exists, then the function does not copy/do anything. Following is the syntax of this function –

cv.namedWindow(winname[, flags])

Where,

  • Winname: it specifies the name of the window.

  • Flags: used to specify the window flags.

Example

In this example, we will draw a red circle with a 10 radius where the left button is pressed.

import numpy as np
import cv2
cap = cv2.VideoCapture(0)
color = (0,0,255) #red color
line_width = 2
radius = 10
point = (0,0)
def click(event, x, y, flags, param):
   global point, pressed
   if event == cv2.EVENT_LBUTTONDOWN:
      print("left button Pressed at {},{} co-ordinates".format(x,y))
      point = (x,y)
        
cv2.namedWindow("live video")
cv2.setMouseCallback("live video",click)
while(True):
   ret, frame = cap.read()
   frame = cv2.resize(frame, (0,0), fx=0.5,fy=0.5)
   cv2.circle(frame, point, radius, color, line_width)
   cv2.imshow("live video",frame)
   
   if cv2.waitKey(1) & 0xFF == ord('q'):
      break
cap.release()
cv2.destroyAllWindows()

Output

left button Pressed at 29,36 co-ordinates
left button Pressed at 27,44 co-ordinates
left button Pressed at 150,82 co-ordinates
left button Pressed at 178,36 co-ordinates 

The red circle is drawn on the live video footage, where the left click event is happened.

Updated on: 30-May-2023

195 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements