Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
How to change video resolution in OpenCV in Python
Next article icon

OpenCVis 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-pythonpip install numpy

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

Step 1:  Import the required modules

Python3
importcv2

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
Python3
cap=cv2.VideoCapture("test.mp4")

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

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

Python3
ifcap.isOpened()==False:# give error messageprint("Error in opening file.")else:# proceed forward

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.

Parameters:

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

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

Return Value: It doesn’t returns anything.

Python3
while(cap.isOpened()):ret,frame=cap.read()ifret==True:cv2.imshow("GFG",frame)cv2.setMouseCallback('GFG',mouse_click,param=frame)ifcv2.waitKey(25)&0xFF==ord('q'):breakelse:break

Step 5:  Define function to handle mouse click

Python3
defmouse_click(event,x,y,flags,param):# to check if left mouse button was clickedifevent==cv2.EVENT_LBUTTONDOWN:print("left click")cv2.imwrite("frame.jpg",param)# to check if right mouse button was clickedifevent==cv2.EVENT_RBUTTONDOWN:print("right click")cv2.imshow("Current Frame",frame)

Below is the complete implementation

Python3
importcv2defmouse_click(event,x,y,flags,param):# to check if left mouse button was clickedifevent==cv2.EVENT_LBUTTONDOWN:print("left click")cv2.imwrite("frame.jpg",param)# to check if right mouse button was clickedifevent==cv2.EVENT_RBUTTONDOWN:print("right click")cv2.imshow("Current Frame",frame)cap=cv2.VideoCapture("test.mp4")ifcap.isOpened()==False:# give error messageprint("Error in opening file.")else:# proceed forwardwhile(cap.isOpened()):ret,frame=cap.read()ifret==True:cv2.imshow("GFG",frame)cv2.setMouseCallback('GFG',mouse_click,param=frame)ifcv2.waitKey(25)&0xFF==ord('q'):breakelse:breakcap.release()cv2.destroyAllWindows()

Output:

 

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


Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp