Images are JUST NumPy Arrays
How computes can read the images, How our native python handles the images? Those questions were asked when I started to learn computer vision. I’ve started with python. There are a lot of libraries in python for image processing such as matplotlib, OpenCV, scipy.ndiamge and scikit-image. So whenever an image is read using any of these libraries then that image is actually read as NumPy ndrray. Whenever you are going to perform any operations on these images, essentially, you are manipulating those numbers that are representing these images.
When we read a colour image by OpenCV as follow
import opencv
img = cv2.imread("ImagePath")
print(img)
If we execute the above code, we will get a huge NumPy array. Colour image is a collection of three parallel planes, and every plane represent the colour intensity of a single channel. Most of the colour images are stored in RGB format and here R for red, G for green and B for blue. If you take one single element of an array that represents the intensity of red, green and blue of that pixel.
For a grayscale image, It is different from the colour images since it has only one channel rather than three channels. This single-channel represents the intensity of black or white from 0 to 255.
We can get the shape of the image using the shape method which gives the dimensions of the image
img.shape
ndim, size, dtype methods will provide the dimensions, multiplication of all dimensions and data type of element.
How can we access each element of NumPy array of an image
value_of_the_pixel = img[i,j,k]
Here we have used the above method to get the value of the pixel which is located at i,j location(ith row and jth column )and the colour channel is k in the img image. Here we have used the colour image. If we use a grayscale image, then we will not pass the k argument.
img.min(), img.max(), img.mean() with these functions can be used to get the minimum pixel, maximum pixel and the mean pixel of img image.
More than that, there are some statistical functions of NumPy library such as median, average, mean, std and var to find median, average, standard deviation and variance of the image.
import numpy as np
np.median(img)
np.average(img)
np.mean(img)
np.std(img)
np.var(img)
Above functions are essential functions for image processing.
Colour Spaces of Images
There are many colour spaces which simply represents the colour of the image. RGB, HSV and CMYK are examples of colour spaces.
Red Green Blue Colour Space(RGB colour space)
RGB is an additive colour model that generates colours by combining intensities of blue, green and red.
It is sometimes called as BGR. More than that, OpenCv saves the image in the order of Blue, Green and Red.
Hue, Saturation and Value/Brightness colour space(HSV colourspace)
This colour space attempts to represent colours the way human perceive it. It stores colour information in cylindrical representation of RGB colour points.
Hue — Color Value (0–179)
Saturation — Vibrancy of colour(0–255)
Value — Brightness or intensity (0–255)
It’s useful in computer vision for colour segmentation. In RGB, filtering specific colours is not easy. However, HSV makes it much easier to set colour ranges to filter specific colours as we perceive them. Example if we set the hue 165 to 15 then we filter out the red.
import cv2
import numpy as np
image = cv2.imread(image_path)x=10
y=10
B,G,R = image[x,y]
print(B,G,R)
Above code is written in python. We initialise the blue, green, red colour values of [10,10] Location of the image to B, G, R.
When we change this RGB colour image to Gray image, it will convert into two dimensions array. It doesn’t have three colours, only one colour from 0–255. If 0 then black or if 255 then white.
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
Using the split function, we can split the colour values from images, and we can merge them again with merge function of OpenCV
b,g,r = cv2.split(image)
merged = cv2.merge([b+10,r+100,g])
Above post will give a basic introduction of images. In upcoming posts, I will cover the basic and advance concepts of computer vision
Happy Looking via Computer