Cv2 Rgb To Gray

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

Cv2

Introduction

Img1 = cv2.cvtColor(img, cv2.COLORGRAY2BGR) img2 = contoursimg # Create a mask of contours and create its inverse mask also img2gray = cv2.cvtColor(img2,cv2.COLORBGR2GRAY) ret, mask = cv2.threshold(img2gray, 10, 255, cv2.THRESHBINARY) maskinv = cv2.bitwisenot(mask) # Now black-out the area of contours in original image img1bg = cv2. Import cv2 img = cv2.imread('demo-image.jpg') grayimage = cv2.cvtColor(img,cv2.COLORBGR2GRAY) cv2.imwrite('opencv-greyscale.png',grayimage) Output. Convert image to grayscale in python using OpenCV module Conclusion. Greyscale Image conversion is a must before doing any further image processing.

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.

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.

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.

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.

Cv2 Rgb To Gray

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.

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.

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.

The complete loop can be seen below.

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

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.

The final code can be seen below.

Testing the code

Cv2 Rgb To Gray

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.

Related Posts

References

Cv2 Rgb To Gray Code

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