Converting Color video to grayscale using OpenCV in Python
OpenCV is a huge open-source library for computer vision, machine learning, and image processing. It can process images and videos to identify objects, faces, or even the handwriting of a human. In this article, we will see how to convert a colored video to a gray-scale format.
Approach:
- Import the cv2 module.
- Read the video file to be converted using the cv2.VideoCapture() method.
- Run an infinite loop.
- Inside the loop extract the frames of the video using the read() method.
- Pass the frame to the cv2.cvtColor() method with cv2.COLOR_BGR2GRAY as a parameter to convert it into gray-scale.
- Display the frame using the cv2.imshow() method.
Example: Suppose we have the video file CountdownTimer.mov as the input.
Python3
import cv2
source = cv2.VideoCapture( 'Countdown Timer.mov' )
while True :
ret, img = source.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow( "Live" , gray)
key = cv2.waitKey( 1 )
if key = = ord ( "q" ):
break
cv2.destroyAllWindows()
source.release()
|
Output:
Last Updated :
03 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...