OpenCV: Basics of Reading and Visualizing Images

Sasani Perera
5 min readJun 21, 2023

--

In this article, we will discuss the very basics of image processing which is reading and visualizing the images and the theories behind them.

Basics of an Image

Suppose you are having an image. This image could be in any format (PNG, JPEG, GIF etc.). These images are made up of pixels which have their own (x,y) coordinate identity.

This is a painting (canvas) of resolution 800 x 200px which means it has 800 pixels across the x-axis and 200 pixels across the y-axis. You can see a black dot on the green-coloured square, the coordinate of this pixel is given (down-left corner) as 260,90px. Now let’s save this painting into a JPEG file.

Painting canvas
Coordinate of a pixel

Now let’s save this painting into a JPEG file named Test Image. Once it is saved, we can see a slightly darker border around the coloured squares. This is due to the compression of the file.

Test Image.jpg

Step 1: Reading an image

Now let’s read this image with OpenCV. For this, we need to import cv2 library and use the function cv2.imread().

The image should be in the working directory or a full path of the image should be given. (try writing the file path with double backslash after the drive.)

import cv2

#if the image is in the same folder as the python file
im = cv2.imread("TestImage.jpg")
import cv2

#if the image is not in the same folder as the python file
im = cv2.imread("C:\\Users\Public\Pictures\TestImage.jpg")

The output can be seen in the Variable Explorer tab and should be a 3-D array.

Step 2: Visualizing an Image with OpenCV

Now we will visualise the image we read earlier. For this, we use cv2.imshow() to display an image in a window. Once we open a window, we need to close it as well.

#visualising the image
cv2.imshow("Image Window",im)

#closing the window
cv2.waitKey(0) & 0xFF
cv2.destroyAllWindows()

Here (0) indicates to show the window indefinitely. Or, we can put the desired number of milliseconds there.

Non-resizable window

There is a special case where you can already create a window and load the image to it later. In that case, you can specify whether the window is resizable or not.
It is done with the function cv2.namedWindow(). By default, the flag is cv2.WINDOW_AUTOSIZE. But if you specify the flag to be cv2.WINDOW_NORMAL, you can resize the window. To get a resizable window:

cv2.namedWindow("BGR Image", cv2.WINDOW_NORMAL)
cv2.imshow("BGR Image",im)
Resizable window

Step 3: Visualizing an Image with Matplotlib

We can use Matplotlib to plot the images as well.

import cv2
import matplotlib.pyplot as plt

#using opencv to read an image
im = cv2.imread("C:\\Users\Public\Pictures\TestImage.jpg")

#visualising the plot
plt.title("RGB Image")
plt.imshow(im)

We can see the colours displayed by Matplotlib are mixed up. Let us see the reason for this.

RGB channels of a coloured Image

RGB colour model stores individual values for red, green, and blue. With a colour space based on the RGB colour model, the three primaries are added together to create colours from completely white to completely black.

The colour images we see have three channels, R, G and B. Each pixel in the image is consisting of these three colour combinations.

Hence our coloured image can be identified as a 3D array of Rows, Columns, and Colour channels. This is the reason why we got a 3D array as a result in Variable Explorer when TestImage.jpg was read.

Step 3: Separating colour channels

When a coloured image is read using cv2.imread(), the colour channels are read as BGR rather than RGB. Colour images loaded by OpenCV are in BGR mode. But Matplotlib displays in RGB mode. So colour images will not be displayed correctly in Matplotlib if the image is read with OpenCV. This is the reason for the colour mix-up in the above plot. To avoid it, we separate the colour channels using cv2.split() and put them back in the order we like.

This splits the 3D array into 3, 2D arrays, representing the 3 colours. These 2D arrays consist of only Black (000000) and White (FFFFFF). Hence the plots of these split b,g, and r values appear as Black, White and Grey images.

These are the colours that I have used in this image.

In the 4th colour, all 3 colours are combined, hence it appears as neither black nor white and appears as a colour in between black and white. Red (255,0,0) appears as white (255) in the Red layer. Blue (0,0,255) and Green (0,255,0) appear as Black (0) in the Red layer and similarly for the other 2 layers.

import cv2
import matplotlib.pyplot as plt

#read the image
im = cv2.imread("C:\\Users\Public\Pictures\TestImage.jpg",1)

#splitting the color channels
b,g,r = cv2.split(im)

#visualising the color channels
cv2.namedWindow("B channel", cv2.WINDOW_NORMAL)
cv2.namedWindow("G channel", cv2.WINDOW_NORMAL)
cv2.namedWindow("R channel", cv2.WINDOW_NORMAL)
cv2.imshow("B channel",b)
cv2.imshow("G channel",g)
cv2.imshow("R channel",r)

#closing the window
cv2.waitKey(0) & 0xFF
cv2.destroyAllWindows()

Alternative to cv2.split() the following code can be also used.

#read the image
im = cv2.imread("C:\\Users\Public\Pictures\TestImage.jpg",1)

#splitting the color channels
b = im[:,:,0]
g = im[:,:,1]
r = im[:,:,2]

Step 4: Merging the separated colour channels

To merge the colour channels cv2.merge() is used.

#read the BGR image
im = cv2.imread("C:\\Users\Public\Pictures\TestImage.jpg",1)

#splitting the color channels
b = im[:,:,0]
g = im[:,:,1]
r = im[:,:,2]

#merging into RGB image
im2 = cv2.merge([r,g,b]);

Step 5: Write an Image

To save the processed image, function cv2.imwrite() is used.

#merging into RGB image
im2 = cv2.merge([r,g,b]);

#saving the image
cv2.imwrite("ProcessedImage.jpg",im2)

In the next article, we will discuss Image Masking with OpenCV.

Thanks For Reading, Follow For More.

--

--