Converting a video to grayscale is very easy. We have already learned how to convert an image to grayscale in the previous chapter. We will use this knowledge to convert each frame to grayscale. Similar to converting to grayscale, you can perform any other operation such as edge detection/contour detection.
The following is the code for converting a video to grayscale:
import cv2cam = cv2.VideoCapture(0)while (cam.isOpened()): ret, frame = cam.read() gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) cv2.imshow('gray_frame',gray_frame) cv2.imshow('original_frame',frame) if cv2.waitKey(1) & 0xFF == ord('q'): breakcam.release()cv2.destroyAllWindows()
Figure 1 is the output of the preceding code: