O'Reilly logo

Computer Vision with Python 3 by Saurabh Kapur

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Converting to grayscale

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:

Figure 1: The image ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required