Color Spaces and Conversion in OpenCV

FREE Online Courses: Transform Your Career – Enroll for Free!

In this article, we’ll be understanding what are color spaces in image processing, what are the different color spaces available in OpenCV, and how we can convert an image from one color space to another. We will understand the significance of each color space and implement them in OpenCV.

What are color spaces in image processing?

Colors spaces in image processing identify the combination of specific colors in various models and the mapping functions. Once identification of the color combination i.e. the color space is done, the associated color model will be recognized automatically. Color spaces represent the color channels of the image and represent the particular hues of the image.

There are numerous color spaces and their associated color models available for image processing tasks. Each color space has its significance.

Color spaces and color models

A color space is simply a grouping of colors that allows us to express and reproduce colors consistently. A color model is a mathematical representation of colors in the color space. RGB pixels of an image are represented by a three-integer tuple of Red, Green, and Blue values.

A color space describes both the color model and the abstract mapping function that is utilized to define actual colors when taken as a whole. Informally, selecting a color space also indicates that we are selecting a color model.

Color channels

An image comprises color channels. The color channels contain information regarding various components of the image. These color channels merged, to form a complete image.

Importance of lighting conditions in Computer vision

Lighting conditions play a crucial role in computer vision and image processing tasks. The quality of images input to the system will determine the success of the computer vision algorithm, application, and system that has been produced and will be developed in the future. We’ll certainly be able to improve our systems’ resiliency in the face of low lighting, but the quality of an image to be worked with cannot be assured. The computer vision algorithm’s success or failure can be determined by the illumination conditions for the image.

We need to review three parameters to achieve the optimum lighting conditions while working with computer vision and image processing tasks.

1. High contrast: Attempts should be made to maximize the contrast between the image’s regions of interest and the rest of the image. The region of interest should have distinguishable contrast from the rest of the image to be easily detectable and extracted.

2. Generalizable: The lighting circumstances should be steady, consistent, and reproducible from one object to the next.

3. Stable: The images for a certain computer vision application should have stable, consistent, and repeatable lighting conditions.

Color spaces available in OpenCV

There are various color spaces available in OpenCV. We’ll be discussing the following color spaces:

1. BGR/RGB Color Space

The default color space in OpenCV is RGB. OpenCV by default reads images in the BGR format. The sequence for the color channel is blue, green, and red instead of red, green, and blue. Each color is stored in a channel in the BGR format. The BGR or RGB color space model is an additive model where the different intensities of different channels give different shades of color. The values for each color in the BGR color spaces is represented as Blue(255, 0, 0), Green(0, 255, 0), Red(0, 0, 255)

Properties of RGB color space:

The BGR color space is an additive model where the linear combinations of the red, green, and blue channels give different colors. The amount of light reflected from the surface defines the correlation between the three channels.

Splitting RGB image into channels Implementation

# Splitting the image into different channels
r, g, b = cv2.split(img_rgb)

# Setting the grid size
plt.figure(figsize=(10,10))

# Plotting the original image
plt.subplot(221)
plt.title('Original')
plt.imshow(img_rgb)

# Plotting the channels
plt.subplot(222)
plt.title('Blue channel')
plt.imshow(b)

plt.subplot(223)
plt.title('Green channel')
plt.imshow(g)

plt.subplot(224)
plt.title('Red channel')
plt.imshow(r)

rgb

2. HSV Color space

The HSV color space in OpenCV defines the hue, saturation, and value of brightness of the image. HSV expands to Hue, Saturation, and value. Hue refers to the dominant wavelength of the image, saturation refers to the purity of the shades of the colors and the value represents the brightness of the image.

The HSV color space stores information about the color as cylindrical represent of the color points of RGB color space. The value for hue ranges from 0-178, the value for saturation ranges from 0-255, and the value for the brightness of the image ranges from 0-255. HSV color spaces aid in color segmentation processes and provide better performance when compared to other color spaces in OpenCV.

Properties of HSV color space:

HSV color spaces make use of only one channel to describe color i.e. Hue, making it intuitive for specifying colors. The best thing is that it uses only one channel to describe color (H), making it very intuitive to specify color. HSV color space is dependent on the device.

Splitting HSV image into channels Implementation

# Splitting the image into different channels
h,s,v = cv2.split(img_hsv)

# Setting the grid size
plt.figure(figsize=(10,10))

# Plotting the original image
plt.subplot(221)
plt.title('Original')
plt.imshow(img_hsv)

# Plotting the channels
plt.subplot(222)
plt.title('Hue channel')
plt.imshow(h)

plt.subplot(223)
plt.title('Saturation channel')
plt.imshow(s)

plt.subplot(224)
plt.title('Value channel')
plt.imshow(v)

hsv

3. HSL Color space

HSL stands for hue, saturation, and Luminance. The HSL Color space is a way of defining color more naturally. The Hue of the image specifies the base color, saturation defines the saturation of the color and luminance describes the brightness of the color.

Properties of HSL color space:

a. HSV color space tries to depict the colors as observed by the human eye.

b. The HSL color space describes color naturally.

Splitting HSL image into channels Implementation

# Splitting the image into different channels
h,l,s = cv2.split(img_hls)

# Setting the grid size
plt.figure(figsize=(10,10))

# Plotting the original image
plt.subplot(221)
plt.title('Original')
plt.imshow(img_hls)

# Plotting the channels
plt.subplot(222)
plt.title('Hue channel')
plt.imshow(h)

plt.subplot(223)
plt.title('Saturation channel')
plt.imshow(s)

plt.subplot(224)
plt.title('Luminance channel')
plt.imshow(l)

hsl

4. Gray color space

Grayscaling is the process of converting an image from any color space to grayscale color space. The grayscaling process simply translates to converting an image to shades of gray. The shades of gray vary between complete black and complete white.

Properties of Gray color space:

a. Reduces the dimension of the images

b. Due to the dimension reduction, the information provided to each pixel is comparatively less.

c. Reduces the complexity of the model

d. Much more information is extracted for some images through gray scaling which might not be possible if the same algorithms or processes are applied to a color image.

e. Since a grayscale image is a single-channel image, hence it cannot be separated into different channels.

5. YCrCb Color space

YCrCb color space has been derived from the RGB color space. The color space has three components.

Y in the YCrCb color space represents the Luminance or the Luma. This component is obtained from the RGB color space after gamma correction.

Cr represents the difference between the R color channel of RGB color space and the luminance component. It describes the difference between the red component and Luma.

Cb represents the difference between the R color channel of RGB color space and the luminance component. It describes the difference between the blue component and Luma.

Properties of YCrCb color space:

a. Separation of luminance and chrominance into different color channels is done.

b. YCrCb color space is used for compression of the Cr and Cb components.

c. YCrCb color space is dependent on the device.

Splitting YCrCb image into channels Implementation

# Splitting the image into different channels
y, cr, cb = cv2.split(img_YCrCb)

# Setting the grid size
plt.figure(figsize=(10,10))

# Plotting the original image
plt.subplot(221)
plt.title('Original')
plt.imshow(img_YCrCb)

# Plotting the channels
plt.subplot(222)
plt.title('Y channel')
plt.imshow(y)

plt.subplot(223)
plt.title('Cr channel')
plt.imshow(cr)

plt.subplot(224)
plt.title('Cb channel')
plt.imshow(cb)

ycrcb

6. LAB Color space

In the RGB color space, the three color channels encode both color and brightness information of the color for the image. In Lab color space, the encoding of color and brightness information is divided between channels. The LAB color space is three-dimensional and has three components.

The L channel encodes the brightness information of the color. The A channels stores information regarding the color components ranging from Green to Magenta color, while the B channels stores information regarding the color components ranging from Blue to Yellow color.

Properties of LAB color space:

a. It is a uniform color space and approximates how color is perceived.

b. The LAB color space is independent of the device.

Splitting LAB image into channels Implementation

# Splitting the image into different channels
L, A, B = cv2.split(img_LAB)

# Setting the grid size
plt.figure(figsize=(10,10))

# Plotting the original image
plt.subplot(221)
plt.title('Original')
plt.imshow(img_LAB)

# Plotting the channels
plt.subplot(222)
plt.title('L channel')
plt.imshow(L)

plt.subplot(223)
plt.title('A channel')
plt.imshow(A)

plt.subplot(224)
plt.title('B channel')
plt.imshow(B)

lab

Color Space Conversion in OpenCV

When we refer to color space conversion, we mean representing a color from one basis to another and converting an image from one color space to another while retaining as much similarity to the previous color space as possible to make the translated image look as similar to the original image as possible.

The cvtColor function is used for color space conversion in OpenCV. The cvtColor() function converts an image between color spaces. The OpenCV library offers more than 150 color space conversion functions.

Syntax:

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

Parameters:

a. src: Source image or the image which has to be converted to another color space.

b. code: color space conversion codes provided by OpenCV

c. dst: Output image of the same size and depth as the source image.

d. dstCn: Number of channels of the output image

e. Return Value: The function returns a converted image

Implementation

# Importing OpenCV
import cv2

# Importing matplotlib.pyplot
import matplotlib.pyplot as plt

# Reading image 
img = cv2.imread(r"C:\Users\tushi\Downloads\PythonGeeks\deer.png")

# Converting image from BGR color space to RGB color space
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

# Converting image from BGR color space to HSV color space
img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# Converting image from BGR color space to HLS color space
img_hls = cv2.cvtColor(img, cv2.COLOR_BGR2HLS)

# Converting image from BGR color space to GRAY color space
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Converting image from BGR color space to YCrCb color space
img_YCrCb = cv2.cvtColor(img, cv2.COLOR_BGR2YCrCb)

# Converting image from BGR color space to LAB color space
img_LAB = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)

# Setting the grid size
plt.figure(figsize=(20,20))

# Plotting the original image
plt.title('Original')
plt.imshow(img)

colour space conversion

# Plotting the RGB image
plt.subplot(231)
plt.title('RGB')
plt.imshow(img_rgb)

# Plotting the HSV image
plt.subplot(232)
plt.title('HSV')
plt.imshow(img_hsv)

# Plotting the HSL image
plt.subplot(233)
plt.title('HLS')
plt.imshow(img_hls)

# Plotting the GRAY image
plt.subplot(234)
plt.title('GRAY')
plt.imshow(img_gray, cmap='gray')

# Plotting the YCrCb image
plt.subplot(235)
plt.title('YCrCb')
plt.imshow(img_YCrCb)

# Plotting the LAB image
plt.subplot(236)
plt.title('LAB')
plt.imshow(img_LAB)

colour space converted

Significance of color space conversion in OpenCV

Color spaces are the representation of the color channels of the image that gives the image that particular hue. By default, the image is read in OpenCV as BGR mode and the color space is RGB.

There are three use cases for color spaces and conversion to different color spaces in data visualizations:

  1. Use of color to distinguish data
  2. Use of color for representation of data values
  3. Using color to highlight information

Conclusion

In this article, we learned about color spaces in image processing. We learned about the color spaces available in OpenCV and understood their significance. Furthermore, we learned about color space conversion and implemented it in OpenCV. We also understood the importance of color space conversion in image processing.

Did you like this article? If Yes, please give PythonGeeks 5 Stars on Google | Facebook


Leave a Reply

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