Python OpenCV: Converting webcam video to gray scale

In this tutorial we will check how to obtain video from a webcam and convert it to gray scale, using OpenCV and Python.

Introduction

In this tutorial we will check how to obtain video from a webcam and convert it to gray scale, using OpenCV and Python.

For an introductory tutorial on how to obtain video from a camera using OpenCV, please check here. For a tutorial explaining how to convert an image to gray scale, please check here.

This tutorial was tested on Windows 8.1, with version 4.0.0 of OpenCV. The Python version used was 3.7.2.

The code

As usual, we will start our code by importing the cv2 module.

import cv2

Then, we need to create an object of class VideoCapture, which will allow us to get the frames from a webcam.

As input of the constructor, we need to pass the identifier of the camera we want to use, as a number. If we only have a webcam attached to the computer, we can pass the value 0.

capture = cv2.VideoCapture(0)

Then, we will need to obtain the frames of our camera one by one and convert them to gray. This means we should write that logic inside an infinite loop, so we keep obtaining frames, converting them to gray and displaying them.

Since we want to have a way to break the loop and finish the program, we will use the waitKey function to check if a given key was pressed. So, if that key was pressed, we will break the loop.

We will look for a ESC key press. Note that the waitKey function returns the pressed key as a number. The ESC key corresponds to 27.

while(True):
     
    # Frame obtain / convert logic
     
    if cv2.waitKey(1) == 27:
        break

In order to obtain a frame from the camera, we need to call the read method on our VideoCapture object.

This method takes no arguments and returns as output a tuple. The first value of the tuple will be a Boolean indicating if the frame was obtained with success. We will ignore this value but, in a real application scenario, you should use it for error checking.

As second output, the read method will return the captured image, as a numpy ndarray object.

ret, frame = capture.read()

Now, to convert the frame to gray scale, we simply need to call the cvtColor function from the cv2 module.

As first input, this function receives the image to be converted to a different color space. In our case, it will be the frame we have just obtained.

As second input, the function receives the color space conversion code. Note that, by default, the VideoCapture will convert the frame to the BGR color space [1]. So, we should use the color space conversion code COLOR_BGR2GRAY.

As output, the cvtColor function will return the converted image.

grayFrame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

Then, we will simply display the grayed frame in a window, by calling the imshow function. As first input this function receives the name of the window and as second input the image to display.

cv2.imshow('video gray', grayFrame)

For comparison, we will also display the original frame in another window. Note that the name of this second window should differ from the first.

cv2.imshow('video original', frame)

The complete loop can be seen below.

while(True):
     
    ret, frame = capture.read()

    grayFrame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    cv2.imshow('video gray', grayFrame)
    cv2.imshow('video original', frame)
     
    if cv2.waitKey(1) == 27:
        break

To finalize, after the loop breaks, we should release the camera by calling the release method on our VideoCapture object.

capture.release()

We also need to call the destroyAllWindows function of the cv2 module to destroy the two windows where we were showing the original and the grayed frames.

cv2.destroyAllWindows()

The final code can be seen below.

import cv2
 
capture = cv2.VideoCapture(0)
 
while(True):
     
    ret, frame = capture.read()

    grayFrame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    cv2.imshow('video gray', grayFrame)
    cv2.imshow('video original', frame)
     
    if cv2.waitKey(1) == 27:
        break
 
capture.release()
cv2.destroyAllWindows()

Testing the code

To test the code, simply run the script from the previous section, using a tool of your choice. In my case I’ll be using IDLE, a Python IDE.

You should get a result similar to figure 1. As can be seen, two windows are created, one displaying the original frames and the other displaying the grayed version. If you click the ESC button, both windows should disappear and the script execution should finish.

Displaying video capture and grey scale version, obtained from webcap with OpenCV and Python
Figure 1 – Displaying original camera video capture and gray scale converted version.

Related Posts

References

[1] http://answers.opencv.org/question/120277/default-color-space-for-frames-in-video/

Leave a Reply