Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
How do humans perceive colors?
2.1.
RGB color space
2.2.
CMYK color space
2.3.
HSV color space
3.
Visualizing RGB color channels using OpenCV
4.
Frequently Asked Questions
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Color Spaces

Create a resume that lands you SDE interviews at MAANG
Speaker
Anubhav Sinha
SDE-2 @
12 Jun, 2024 @ 01:30 PM

Introduction

Colors play an important part in our lives. They can express emotions and even affect our mood. We cannot imagine our lives without colors. Computers represent colors by arranging the pixels in arrays, the range of colors that can be represented is called color range. Let us look at some of the popular color spaces. 

Colors are an important aspect of our lives

How do humans perceive colors?

When light travels to the back of the eye, it stimulates the different light-sensitive cells that are located in the retina. These cells send the signals to the brain. The brain processes the information and creates the colors that we are seeing. A small number of colors can be added in different concentrations to create an extensive range of colors. This range is called color space. 

RGB color space

RGB color space is based on the RGB color model. RGB stands for redgreenblue. It is an additive color model. An additive color model is based on light. It starts with black when there is no light. Red, green, and blue lights are added to it to get different colors, and when all the three colors are added equally, white is produced.

Different shades of all three colors are added to produce all the colors on the digital screens of your mobiles, computers, and televisions.

RGB color model

RGB color space occupies a large part of our visible spectrum and can be used to show almost all the colors on our screen. 

White is produced when all the colors are added

NOTE-  RGB color model is an additive color model, and the adding of colors should not be confused with the mixing of colors in real life. Instead, the addition of colors is analogous to the addition of light. Just like adding all the colors of a rainbow produces white light.

CMYK color space

CYMK color space

RGB color model can be successfully used to show colors on our computer screens. But what about the colors that we see on paper. The actual printing of colors on paper cannot use the RGB model because, unlike our screens, light is not emitted from paper.

For this, a different color space is CYMK is used. CYMK includes cyanyellowmagenta, and black. CYMK is a subtractive color model. It starts with white and then by adding different shades of CYMK, we get different colors.

In theory, the result is black when all the colors are added. But in reality, when printers mix all these colors together on paper, the result is muddy brown. To get the black color, true black is added.

link

HSV color space

RGB and CYMK use primary colors to represent all the colors. HSV color model is closer to how we humans perceive colors. HSV stands for hue, saturation, and value.

Hue represents the color of the model. Different softwares use different scales. In OpenCV, Its range is [0-179]. All the shades of a particular color will have the same hue.

Saturation is the amount of gray in color. It’s range is [0,255] in OpenCV. 0 represents gray, and 255 represents full saturation of the color.

Value means the brightness of the color. Its range is also [0,255] in OpenCV. 0 represents pure black.

HSV color model is helpful in color picking and color detection.

HSV cone

Conversion of RGB space into HSV

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Visualizing RGB color channels using OpenCV

OpenCV is an open-source python library. It is used for computer vision and image processing. 

Let us see how we can see the RGB channels separately using OpenCV.

Let us first import the cv2 module:

import cv2

 

We will read the image using the imread function:

img = cv2.imread('path of the image')

 

For splitting the image into the RGB channels, we will use cv2.split()

cv2.split() will split the image into blue, green, and red. We will store it into different variables:

blue, green, red = cv2.split(img)

 

To display the split image, we will use cv2.imshow(), we will also use cv2.waitkey() to make the image stay on the screen until a key is pressed.

To view the original image:

cv2.imshow("RGB", img)
cv2.waitkey(0)

 

Output:

To view the red channel:

cv2.imshow("Red",red)
cv2.waitkey(0)

 

Output:

To view the blue channel:

cv2.imshow("Blue", blue)
cv2.waitkey(0)

 

Output:

 

To view the green channel:

cv2.imshow("Green", green)
cv2.waitkey(0)

 

Output:

Let us look at how we can convert our image into HSV format. To transform our image into HSV:

hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

 

To view the image:

cv2.imshow('HSV', hsv)

 

Output:

Also read, Sampling and Quantization

Frequently Asked Questions

  1. Are there more color spaces?
    Yes, there are more color spaces such as HSB, YUB, and YCbCr.
     
  2. What are some practical uses of CYMK color space?
    CYMK is used in newspaper printing.
     
  3. Which color space contains the biggest color range?
    L*a*b* color space is the biggest. It represents all the colors that a human can see.

Conclusion

This article talks about all the basic aspects of color spaces. We learned what color spaces are and different color spaces such as RGB, CYMK, and HSV. We also saw how we can view different channels using OpenCV. To know more, check out our industry-level courses on coding ninjasRefer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, Machine learning, etc.

Previous article
Color Filtering
Next article
How To Crop An Image In Opencv Python?
Live masterclass