How to change the color space of an image in OpenCV

This recipe helps you change the color space of an image in OpenCV

Recipe Objective: How to change the color space of an image in OpenCV?

Let us learn how to change the color space of an image in OpenCV

Step 1: Import library and read the image

The image that we use here is

project.jpg 

import cv2 image_gray=cv2.cvtColor(image, cv2.COLOR_BGR2GRAY )

Step 2: Change color space using cv2.cvtColor()

We can change the color space of an image using the cv2.cvtColor() function, which takes the image and the color space conversion code as mandatory parameters.

There are about 150 color space codes available in OpenCV. Let us try two of them in this recipe

The cv2.COLOR_BGR2GRAY converts an image from BGR color space to GRAYSCALE

image_gray=cv2.cvtColor(image, cv2.COLOR_BGR2GRAY )
cv2.imshow('BGR to GRAYSCALE',image_gray)
cv2.waitKey(0)

Output:

gray.jpg

The cv2.COLOR_BGR2RGB converts an image from BGR color space to RGB

image_rgb=cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
cv2.imshow('BGR to RGB',image_rgb)
cv2.waitKey(0)

Output:

rgb.jpg

Download Materials

What Users are saying..

profile image

Savvy Sahai

Data Science Intern, Capgemini
linkedin profile url

As a student looking to break into the field of data engineering and data science, one can get really confused as to which path to take. Very few ways to do it are Google, YouTube, etc. I was one of... Read More

Relevant Projects

Recommender System Machine Learning Project for Beginners-4
Collaborative Filtering Recommender System Project - Comparison of different model based and memory based methods to build recommendation system using collaborative filtering.

Build a Music Recommendation Algorithm using KKBox's Dataset
Music Recommendation Project using Machine Learning - Use the KKBox dataset to predict the chances of a user listening to a song again after their very first noticeable listening event.

End-to-End Snowflake Healthcare Analytics Project on AWS-1
In this Snowflake Healthcare Analytics Project, you will leverage Snowflake on AWS to predict patient length of stay (LOS) in hospitals. The prediction of LOS can help in efficient resource allocation, lower the risk of staff/visitor infections, and improve overall hospital functioning.

Build a Multi Touch Attribution Machine Learning Model in Python
Identifying the ROI on marketing campaigns is an essential KPI for any business. In this ML project, you will learn to build a Multi Touch Attribution Model in Python to identify the ROI of various marketing efforts and their impact on conversions or sales..

Build a Credit Default Risk Prediction Model with LightGBM
In this Machine Learning Project, you will build a classification model for default prediction with LightGBM.

MLOps Project for a Mask R-CNN on GCP using uWSGI Flask
MLOps on GCP - Solved end-to-end MLOps Project to deploy a Mask RCNN Model for Image Segmentation as a Web Application using uWSGI Flask, Docker, and TensorFlow.

Build Regression (Linear,Ridge,Lasso) Models in NumPy Python
In this machine learning regression project, you will learn to build NumPy Regression Models (Linear Regression, Ridge Regression, Lasso Regression) from Scratch.

ML Model Deployment on AWS for Customer Churn Prediction
MLOps Project-Deploy Machine Learning Model to Production Python on AWS for Customer Churn Prediction

Create Your First Chatbot with RASA NLU Model and Python
Learn the basic aspects of chatbot development and open source conversational AI RASA to create a simple AI powered chatbot on your own.

Build a Multi ClassText Classification Model using Naive Bayes
Implement the Naive Bayes Algorithm to build a multi class text classification model in Python.