Professional Documents
Culture Documents
Concept of Channels in An Image
Concept of Channels in An Image
Concept of
Channels in
an Image
M
MAAD
DHHU
UBB
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,
M
MAAD
DHHU
UBB
Grayscale
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("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).
M
MAAD
DHHU
UBB
RGB images
Now, let us print an RGB image and observe the results. We use the below code
for that:
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.
M
MAAD
DHHU
UBB
Visualization of each channel
separately
import cv2 as cv
import numpy as np#read the image
image = cv.imread("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("channel_red.jpg", R)
cv.imwrite("channel_green.jpg", G)
cv.imwrite("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:
M
MAAD
DHHU
UBB
I know that there are a couple of questions messing up
your thought process now. So let me explain.
M
MAAD
DHHU
UBB
If you found this resource helpful and you'd
like to see more content like this, consider
following me on:
Ninad Madhab
Ninad Madhab