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

Ameeruddin Mohammed

ETL (Abintio) developer at IBM
linkedin profile url

I come from a background in Marketing and Analytics and when I developed an interest in Machine Learning algorithms, I did multiple in-class courses from reputed institutions though I got good... Read More

Relevant Projects

Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.

Isolation Forest Model and LOF for Anomaly Detection in Python
Credit Card Fraud Detection Project - Build an Isolation Forest Model and Local Outlier Factor (LOF) in Python to identify fraudulent credit card transactions.

Build a Hybrid Recommender System in Python using LightFM
In this Recommender System project, you will build a hybrid recommender system in Python using LightFM .

Avocado Machine Learning Project Python for Price Prediction
In this ML Project, you will use the Avocado dataset to build a machine learning model to predict the average price of avocado which is continuous in nature based on region and varieties of avocado.

Customer Churn Prediction Analysis using Ensemble Techniques
In this machine learning churn project, we implement a churn prediction model in python using ensemble techniques.

Learn How to Build a Logistic Regression Model in PyTorch
In this Machine Learning Project, you will learn how to build a simple logistic regression model in PyTorch for customer churn prediction.

Deploy Transformer BART Model for Text summarization on GCP
Learn to Deploy a Machine Learning Model for the Abstractive Text Summarization on Google Cloud Platform (GCP)

Learn to Build Generative Models Using PyTorch Autoencoders
In this deep learning project, you will learn how to build a Generative Model using Autoencoders in PyTorch

NLP and Deep Learning For Fake News Classification in Python
In this project you will use Python to implement various machine learning methods( RNN, LSTM, GRU) for fake news classification.

Hands-On Approach to Master PyTorch Tensors with Examples
In this deep learning project, you will learn how to perform various operations on the building block of PyTorch : Tensors.