cv2 cvtColor Python Example with Steps

cv2 cvtColor Python Example with Steps

Do you want to convert an image into different colors in python? Then there is a function in OpenCV that allows you to do so and it is cv2 cvtColor. In this entire tutorial, you will know how to implement cv2 cvtcolor in python through steps.

Steps to Implement cv2 cvtColor

In this section, you will know all the steps required for converting an image to a different color.

Step 1: Import the necessary libraries

In my example, I am using only OpenCV that why I will import it only using the import statement.

import cv2

If you have not installed OpenCV in your system. Then read how to install opencv using pip.

Step 2: Read the sample image

The second step is to read the input image for conversion. In OpenCV, you can read images using imread() method. Let’s read the image.

img = cv2.imread("bird.jpg")

Below is the image I am using.

bird image for cv2 cvtColor
bird image for cv2 cvtColor

Step 3: Convert the image

In this step let’s convert the input image to different colors. There are more than 150 color-space conversion methods available in OpenCV. In this tutorial, I am converting the BGR image to gray and HSV. The following is  the syntax for the cv2.cvtColor().

cv2.cvtColor(input_image, flag)

Here the input_image is the image you want to convert. And the flag is the type of conversion. You can know all the flags using the following code.

import cv2
flags = [i for i in dir(cv2) if i.startswith('COLOR_')]
print flags

Convert image to Gray

You can convert any image to gray by passing the flag as cv2.RGB2GRAY. Use the below lines of code to convert the colored image to Gray.

import cv2
img = cv2.imread("bird.jpg")
grey_image = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
cv2.imshow("Gray Image of Bird",grey_image)
cv2.waitKey(0)

To show the image you will use imshow() method provided by OpenCV. Below is the output you will get when you will run the above code.

Output

gray image of a bird
gray image of a bird

Converting image to HSV

HSV is a cylindrical color model that remaps the RGB primary colors. If you will pass cv2.RGB2HSV inside the cv2.cvtColor() then you will get the image in HSV color. Execute the below lines of code and see the output.

import cv2
img = cv2.imread("bird.jpg")
grey_image = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
hsv_image = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
cv2.imshow("HSV Image of Bird",hsv_image)
cv2.waitKey(0)

Output

HSV image of a bird
HSV image of a bird

Conclusion

These are the steps to implement cv2 cvtColor. I have given two colors examples for this method. But there are also other colors you can convert using this method. Hope you have liked this tutorial. If you have any queries then you can contact us for more help.

Source:

OpenCV Python Documentation

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner