In this Blog, we will discuss complete information on how color images and black and white (Grayscale) images will work using OpenCV Framework.
Grayscale image Using OpenCV and Matplotlib
The grayscale image generally follows 2 dimensions which means we will be having rows and columns, you can see below image how our computer understands 2D Grayscale images.
It’s a Grayscale image where we are having multiple rows and columns and each slot has a value called a pixel.
What is a Pixel?
A pixel is a numerical number that decides the color in a particular area since we are dealing with grayscale images 0-black and 255-white.
if the image has a shape of (256,256) means 256 rows and 256 columns and the number of values in the entire image was 256 * 256 = 65,536.
Let’s discuss something about 0-black and 255-white and why?
we all know that processor of any type takes the data in 2 possible ways that are 0,1 has binary so in total there are 2 outcomes for each value and if we find out the datatype of an image it will be uint8 which means unsigned integer 8
so keeping datatype 8 in mind and 2 outcomes by the processor 2 power 8
2 ** 8 gives a value of 256 since the index starts from 0, 0–255 total of 256 values so OpenCV takes the minimum value as black and the maximum value as white, You can follow the below code and the outcome as well.
a = np.array([1,2,5,9,11])
a = a.reshape(1,-1)
plt.imshow(a,cmap = 'gray')
when the value is minimum it is in black color and when Ur increases the value slowly from black it is converting to white.
same logic if we apply for 0–255 means a total of 256 values
values = np.arange(0,256) # reading value from 0 to 255
values = values.reshape(1 , -1) # converting 1d values to 2d
plt.imshow(values,cmap = 'gray')
since we have taken only 256 values in one row it’s quite difficult to understand using the above outcome, so we will repeat the row values 100 times to get the proper outcome.
values = np.arange(0,256) # reading value from 0 to 255
values = values.reshape(1 , -1) # converting 1d values to 2d
repeat = np.repeat(values , repeats=100 , axis = 0) # repeat same row 100 times
# axis 0 means column wise according to NumPy library
plt.imshow(repeat)
You can check 0 is black and 255 is white from 0 to 255 and how the color is getting changed at each pixel intensity(value).
Color images Using OpenCV.
color images are quite different for the processor to read when compared to Grayscale images, color images have 3 different channels called RED, GREEN, and BLUE we can also call them RGB channels.
You can see RGB channels where the R channel is a grayscale individually, the G channel is grayscale and even the B channel is grayscale individually.
for example in the above image, u can see the first value 255 in the R channel and 0 in the green channel, and 255 in the blue channel is going to check in the RGB color space
by checking these 3 values in RGB color space you will get a color which you can see in the below image.
Similarly, each value in RGB channels checks in color space and gives a color, the same thing will be applied to the remaining pixel intensities.
If we want to check individual channels separately we can do it using OpenCV and Matplotlib.
plt.subplot(2,2,1)
plt.title('R_channel')
plt.imshow(color_img[: , : , 0] , cmap='gray') # selecting entire rows and columns and 0-R channel
plt.subplot(2,2,2)
plt.title('G_channel')
plt.imshow(color_img[: , : , 1] , cmap='gray')
plt.subplot(2,2,3)
plt.title('B_channel')
plt.imshow(color_img[: , : , 2] , cmap='gray')
plt.subplot(2,2,4)
plt.title('Original_color_image')
plt.imshow(color_img)
since in this, I have done the code using a combination of OpenCV and matplotlib, In my next blog I will show how to read images using OpenCV and Matplotlib completely and the difference between them while reading an image.
But for reading an Image using the OpenCV framework only You can follow my previous blog Reading and showing images using OpenCV.
for the entire code, you can follow my GitHub Repository.