OpenCV Tutorial – Image Colorspace Conversion using cv2.cvtColor()

OpenCV Tutorial - Image Colorspace Conversion using cv2.cvtColor()
OpenCV Tutorial - Image Colorspace Conversion using cv2.cvtColor()


OpenCV provides a method named cv2.cvtColor() which is used to convert an image from one color space to another. There are more than 150 color spaces are available in OpenCV. We will discuss the important ones in this article. Following Color spaces we are going to cover in this tutorials –

  • Grayscale
  • HSV Color Space
  • RGB Color Space
  • LAB Color Space
Importing OpenCV library
In [1]:
import cv2

Color space Conversion: cv2.cvtColor()

cvtColor() function is used to convert colored images to grayscale.


cv2.cvtColor(src, code, dst, dstCn)


Deep Learning Specialization on Coursera

src: It is the image whose color space is to be changed.

code: It is the color space conversion code. It is basically an integer code representing the type of the conversion, for example, RGB to Grayscale.

Some codes are

cv2.COLOR_BGR2GRAY: This code is used to convert BGR colored image to grayscale

cv2.COLOR_BGR2HSV : This code is used to change the BGR color space to HSV color space.

cv2.COLOR_BGR2RGB : This code is used to change the BGR color space to RGB color space.

cv2. cv2.COLOR_BGR2LAB: This code is used to change the BGR color space to LAB color space.

dst: It is the output image of the same size and depth as src image. It is an optional parameter.

dstCn: It is the number of channels in the destination image. If the parameter is 0 then the number of the channels is derived automatically from src and code. It is an optional parameter.

Reading the Sample Image

In [2]:
# Reading the sample image
In [3]:
#Displaying the sample image
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
Sample Image
Sample Image

Conversion of BGR to Gray space using cv2.cvtColor() with code cv2.COLOR_BGR2GRAY

Let us now convert this sample image into a grayscale image.
In [4]:
#Converting the image into grayscale
image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY )
In [5]:
#Displaying the grayscale image
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.cvtColor() Gray Scale Conversion
cv2.cvtColor() – Gray Scale Conversion

Conversion of BGR to HSV space using cv2.cvtColor() with code cv2.COLOR_BGR2HSV

The HSV color space has the following three components.

H – Hue ( Dominant Wavelength ).

S – Saturation ( shades of the color ).

V – Value ( Intensity ).

The best thing is that it uses only one channel to describe color (H), making it very intuitive to specify color.

In [6]:
#Converting image to HSV Space
image_HSV = cv2.cvtColor(img, cv2.COLOR_BGR2HSV )
In [7]:
#Displaying the HSV image
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.cvtColor() HSV Conversion
cv2.cvtColor() – HSV Conversion

Conversion of BGR to RGB space using cv2.cvtColor() with code cv2.COLOR_BGR2RGB

The RGB colorspace has the following properties –

  • It is an additive colorspace where colors are obtained by a linear combination of Red, Green, and Blue values.
  • The three channels are correlated by the amount of light hitting the surface.
In [8]:
#Converting image to RGB Space
image_RGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB )
In [9]:
#Displaying the RGB Image
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.cvtColor() RGB Conversion
cv2.cvtColor() – RGB Conversion

Conversion of BGR to LAB space using cv2.cvtColor() with code cv2.COLOR_BGR2LAB

The Lab color space has three components.

  1. L – Lightness ( Intensity ).
  2. a – color component ranging from Green to Magenta.
  3. b – color component ranging from Blue to Yellow.

The Lab color space is quite different from the RGB color space.

In RGB color space the color information is separated into three channels but the same three channels also encode brightness information.

On the other hand, in Lab color space, the L channel is independent of color information and encodes brightness only. The other two channels encode color.

In [10]:
#Converting image to LAB Space
image_LAB = cv2.cvtColor(img, cv2.COLOR_BGR2LAB )
In [11]:
#Displaying the LAB image
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)


cv2.cvtColor() LAB Conversion
cv2.cvtColor() – LAB Conversion


So in this article, we saw how we can change the color of an image to various color spaces using cvtcolor() function of OpenCV. We converted a sample image into grayscale, HSV, RGB and LAB color space.

Reference –

Like and Comment section (Community Members)

Create Your ML Profile!

Don't miss out to join exclusive Machine Learning community


No comments yet