Color Spaces and Channels in OpenCV

Color Spaces

Color Spaces are the organization of colors, they display the colors that make the array of color pixels in the images. The different Color Spaces are

  • BGR
  • HSV
  • LAB
  • RGB
  • GRAY

BGR Color Space

BGR stands for Blue(255, 0, 0), Green(0, 255, 0), Red(0, 0, 255). OpenCV uses BGR color as a default color space to display images, when we open a image in openCV using cv2.imread() it display the image in BGR format. And it provides color-changing methods using cv2.cvtColor() for transforming a BGR image into other Color spaces.

HSV Color Space

HSV stands for Hue, saturation, Value. In OpenCV Hue represents the image, Saturation represents greyness, Value represents the brightness. HSV color space solves many problems in OpenCV because it provides better performance when compared to other color spaces.


We can convert a BGR color image into HSV format using cv2.cvtColor(src, cv2.COLOR_BGR2HSV).

import cv2
image = cv2.imread('C:\images\green.jpg')
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
cv2.imshow('image', image)
cv2.imshow('hsv', hsv)
Image 38

LAB Color Space

LAB color space is often referred to as L multiplies with the product of A & B ( L*A*B ). L stands for Luminance dimensions( Intensity ) which a & b are color component dimensions where ‘a’ represents colors from green to magenta, ‘b’ represents colors from blue to yellow.

We can use cv2.cvtColor(src, COLOR_BGR2LAB) to convert image into LAB color space.

If you try to convert an HSV image to LAB Color space or vice-versa it does not convert, for this conversion, you need to convert the image into BGR and then transform it into the required color space.

lab = cv2.cvtColor(image, cv2.COLOR_BGR2LAB)
cv2.imshow('image', image)
cv2.imshow('LAB', lab)
Image 39

RGB Color Space

RGB color space is the opposite of BGR color space, where the objects in the BGR image that are in Blue appear to be Red, and objects in Red color appear to be in Blue and Green Color remain the same.

Opening a BGR image in MatplotLib display the image in RGB format since RGB color space is the default color for matplotlb.

We can notice the observation using a small example.

rgb = cv2.cvtColor(car, cv2.COLOR_BGR2RGB)
cv2.imshow('image', car)
cv2.imshow('RGB', rgb)
Image 40

Gray Color Space

Gray color space is very useful in object detection, we can convert a BGR to GRAY color using cv2.cvtColor(src, COLOR_BGR2GRAY)

imgray = cv2.cvtColor(car, cv2.COLOR_BGR2GRAY)
cv2.imshow('image', car)
cv2.imshow('GRAY', imgray)
Image 41

Color Channels

Color channels have three colors Blue, Green, Red. Any image or any color that we see in our real world is a combination of Different intensities of Blue, Green, Red colors.

To view an image in red, green, blue colors with their respective intensities we can use cv2.split() function to split the colors in the image, and it returns the blue, green, red. color intensity distribution in the form of an image.

import cv2

#split the color channels in the image and store them into three variable
b, g, r = cv2.split(img)
cv2.imshow('img', img)
cv2.imshow('blue', b)
cv2.imshow('green', g)
cv2.imshow('red', r)

Output: If we check the images clearly all of them are in GRAY image format because you can differentiate easily between 2 basic colors black and white, and when there is a high intensity of a particular color its represented in white color, and if there is zero intensity of that particular color over the area, then its completely blacked out

Image 43
Image 42

We can also combine all the blue, green, red images to form a BGR image which is our original image, using cv2.merge()

import cv2
b, g, r = cv2.split(img)

# merge image in blue, green, red into actual image
merged = cv2.merge([b, g, r])
cv2.imshow("merged", merged)
Image 44