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

Introduction

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.

Syntax

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

Parameters:

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
img=cv2.imread("image.jpg")
In [3]:
#Displaying the sample image
window_name='image'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,img)
cv2.waitKey(0)
cv2.destroyAllWindows()
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
window_name='image_Gray'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,image)
cv2.waitKey(0)
cv2.destroyAllWindows()
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
window_name='image_HSV'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,image_HSV)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.cvtColor() HSV Conversion
cv2.cvtColor() – HSV Conversion

[adrotate banner=”3″]

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
window_name='image_RGB'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,image_RGB)
cv2.waitKey(0)
cv2.destroyAllWindows()
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
window_name='image_LAB'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,image_LAB)
cv2.waitKey(0)
cv2.destroyAllWindows()

 

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

Conclusion

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 – https://docs.opencv.org/master/d6/d00/tutorial_py_root.html

  • Sachin Mohan

    My name is Sachin Mohan, an undergraduate student of Computer Science and Engineering. My area of interest is ‘Artificial intelligence’ specifically Deep learning and Machine learning. I have attended various online and offline courses on Machine learning and Deep Learning from different national and international institutes My interest toward Machine Learning and deep Learning made me intern at ISRO and also I become the 1st Runner up in TCS EngiNX 2019 contest. I always love to share my knowledge and experience and my philosophy toward learning is "Learning by doing". So thats why I believe in education which have include both theoretical as well as practical knowledge.

Follow Us

Leave a Reply

Your email address will not be published. Required fields are marked *