This article will help you to work with OpenCV & Python and with the help of that how you can convert the image into grayscale using Odinub. OpenCV is a Library which is used to carry out image processing using programming languages like Python. In this tutorial we will check how to read an image and how to convert that color image into grayscale.
Step 1: Install OpenCVIf you haven’t yet install OpenCV then Install first OpenCV. To install it click here.
Step 2: The code to convert the color image into grayscale- Install the geditor on your system for installing you need to enable Wi-Fi
sudo apt-get install gedit
- Create a new Python file using editor by following command:
gedit filename.py
- To get started, import the cv2 module as below, which will make available the functionalities needed to read images from most formats and convert that images into grayscale.
import cv2
- To read the original image simply call the imread() of the cv2 module and pass the path of image as input parameter, as a string.
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
- To display the images call the imshow() in which there are 2 arguments passing as an input. The first argument is the string with the name to assign to the new window and second argument is the image which you want to show.
cv2.imshow('Original image',image) cv2.imshow('Gray image', gray)
- Now, here there is waitKey() which will wait for keyboard event. This particular function will take input as delay, specified in milliseconds.
- If you will pass the input value as 0 then it will wait indefinitely until a key event occurs.
cv2.waitKey(0)
- Here to destroy previously created window call the destroyAllWindows() after the key pressed.
cv2.destroyAllWindows()
The input of the above code is given below:
The output of the above code is given below:
To execute the above code fire the following command:
python filename.py
For many applications of Image Processing, It is difficult to visualize the image in color for that you need the conversion of color image into the grayscale. I hope above article is helpful you to develop your application and fulfill all your requirements which you needed.
Comments
Please log in or sign up to comment.