Understanding the concept of Channels in an Image

Maximinusjoshus
featurepreneur
Published in
5 min readApr 27, 2021
Photo by Jerin J on Unsplash

There are a handful of colour spaces to represent an image such as RGB, BGR, HSV, CMYK etc. But there is something they all have in common. They are the channels, which these colour spaces use, to collectively form an image. Let us look at some definitions of channels. Wikipedia says that,

Color digital images are made of pixels, and pixels are made of combinations of primary colors represented by a series of code. A channel in this context is the grayscale image of the same size as a color image, made of just one of these primary colors.

If this sounds confusing, hear me out. This definition says that, every image is made up of pixels and each pixel is made up of combinations of colours, to be more precise, primary colours. A channel is the grayscale image of a coloured image, which is made up of only one of the primary colours that form the coloured image. If this still doesn’t make any sense, please stick around here for a couple of minutes.

Grayscale images:

Photo by Hulki Okan Tabak on Unsplash

Grayscale images are single-channeled images in which each pixel carries only information about the intensity of light. These images are exclusively made up of shades of gray.

Grayscale images should not be confused with black and white images (binary images) which contain only black and white pixels. In binary images, either a pixel is black or it is white. They have no colours in between. But Greyscale images have a wide range of shades of grey in their pixels.

Now let us see what we get when we print a grayscale image array. For that we use the code below:

import cv2 as cv#read the image
image = cv.imread("D://medium_blogs//architecture.jpg")
#convert the image to grayscale
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
#print the image array
print(gray)
print("")
#print the shape of the array which is of the form (height, width, number of channels)
print(gray.shape)

The output is:

As you can see, the printed array is a two dimensional array, in which each number represents a pixel and the value of the number is the intensity of light in that pixel. As each number in the above image array represents a pixel, it is called a single channeled image. The light intensity of each pixel in computer vision is measured from 0 to 255 and is known as the pixel value. When the pixel value is 0 it is black and when the pixel value is 255 it is white. Since we used OpenCV to read the image array, the dimensions of the above array are of the form height x width. Here the image has 6016 pixels along it’s y-axis(height) and 4016 pixels along its x-axis(width).

RGB images:

Unlike grayscale images, RGB images are three channeled. Each pixel is made up of three channels, with each channel representing a colour. Now, let us print an RGB image and observe the results. We use the below code for that:

import cv2 as cv#read the image
image = cv.imread("D://medium_blogs//architecture.jpg")
#convert the image to RGB (images are read in BGR in OpenCV)
RGB = cv.cvtColor(image, cv.COLOR_BGR2RGB)
#print the image array
print(RGB)
print("")
#print the shape of the image array
print(RGB.shape)

The output is:

The output is a three dimensional array this time! In this image each pixel has three channels. Each array in the second dimension, represents a pixel. The 0th index has the intensity of red light, the 1st index the intensity of green light and the 2nd index, the intensity of blue light.

When you print the shape of this image, it prints a tuple containing the height, width and the number of channels. When you multiply these three values you get the total number of values inside the image array.

Visualization of each channel separately:

Now as we know what a channel is, let us see how each channel looks like separately. Observe the image below:

Photo by Christian Walker on Unsplash

Now let us split the channels of the above image using the following code:

import cv2 as cv
import numpy as np
#read the image
image = cv.imread("D://medium_blogs//colours.jpg")
#convert the image to RGB (images are read in BGR in OpenCV)
image = cv.cvtColor(image, cv.COLOR_BGR2RGB)
#split the image into its three channels
(R,G,B) = cv.split(image)
#create named windows for each of the images we are going to display
cv.namedWindow("Blue", cv.WINDOW_NORMAL)
cv.namedWindow("Green", cv.WINDOW_NORMAL)
cv.namedWindow("Red", cv.WINDOW_NORMAL)
#display the images
cv.imshow("Blue",B)
cv.imshow("Green", G)
cv.imshow("Red", R)
#write the images to disk
cv.imwrite("D://medium_blogs//channel_red.jpg", R)
cv.imwrite("D://medium_blogs//channel_green.jpg", G)
cv.imwrite("D://medium_blogs//channel_blue.jpg", B)
if cv.waitKey(0):
cv.destroyAllWindows()

We use OpenCV’s split function to split the channels. The outputs of the code are:

Red channel(left), Green channel(middle) and Blue channel(right)

I know that there are a couple of questions messing up your thought process now. So let me explain.

Why are they in grayscale?

That’s because, as we split up the channels, the pixels in each of these images have only one channel now. So they are in greyscale.

How can I differentiate between these images?

Well, just compare each image with the original image. Let us take the red channel image. You can see that the regions containing red colour in the original image are lighter in the red channel image. This simply means that, regions which contribute more to the red colour of the original image are lighter in the grayscale image of the red channel. And the regions which contribute less or do not contribute are dark. This applies for all the three channels.

Why is yellow colour lighter in green and red channels?

That’s because yellow is a mixture of red and green. So green and red make significant contributions to yellow.

I hope this has explained all the questions you had related to image channels.

Thank You.

--

--