Python OpenCV: Converting an image to gray scale

In this tutorial we will check how to read an image and convert it to gray scale, using OpenCV and Python.

 

Introduction

In this tutorial we will check how to read an image and convert it to gray scale, using OpenCV and Python.

If you haven’t yet installed OpenCV, you can check here how to do it. You also need to install Numpy, which can be done with pip, the Python package manager, by sending the following command on the command line:

pip install numpy

 

The code

To get started, we need to import the cv2 module, which will make available the functionalities needed to read the original image and to convert it to gray scale.

import cv2

To read the original image, simply call the imread function of the cv2 module, passing as input the path to the image, as a string.

For simplicity, we are assuming the file exists and everything loads fine, so we will not be doing any error check. Nonetheless, for a robust code, you should handle these type of situations.

As additional note, which will be important for the conversion to gray scale, the imread function will have the channels stored in BGR (Blue, Green and Red) order by default [1].

image = cv2.imread('C:/Users/N/Desktop/Test.jpg') 

Next, we need to convert the image to gray scale. To do it, we need to call the cvtColor function, which allows to convert the image from a color space to another.

As first input, this function receives the original image. As second input, it receives the color space conversion code. Since we want to convert our original image from the BGR color space to gray, we use the code COLOR_BGR2GRAY.

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

Now, to display the images, we simply need to call the imshow function of the cv2 module. This function receives as first input a string with the name to assign to the window, and as second argument the image to show.

We will display both images so we can compare the converted image with the original one.

cv2.imshow('Original image',image)
cv2.imshow('Gray image', gray)

Finally, we will call the waitKey function, which will wait for a keyboard event. This function receives as input a delay, specified in milliseconds. Nonetheless, if we pass the value 0, then it will wait indefinitely until a key event occurs.

Finally, once the user pressed a key, we call the destroyAllWindows function, which will destroy the previously created windows.

cv2.waitKey(0)
cv2.destroyAllWindows()

The final code can be seen below.

import cv2

image = cv2.imread('C:/Users/N/Desktop/Test.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

cv2.imshow('Original image',image)
cv2.imshow('Gray image', gray)

cv2.waitKey(0)
cv2.destroyAllWindows()

 

Testing the code

To test the code, simply run the previous program on the Python environment of your choice. Don’t forget to pass to the imread function the correct path to the image you want to test. You should get an output similar to figure 1, which shows the original image and the final one, converted to gray scale.

Python OpenCV convert image to gray scale.png

Figure 1 – Original image vs gray scale converted image.


References

[1] https://docs.opencv.org/3.0-beta/modules/imgcodecs/doc/reading_and_writing_images.html#imread

Advertisements
This entry was posted in Python and tagged , , , , . Bookmark the permalink.

One Response to Python OpenCV: Converting an image to gray scale

  1. Pingback: Python OpenCV: Saving an image to the file system | techtutorialsx

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s