Before diving into the fancy Convolutional Neural Networks, which is widely used in image classification, let’s start first with how to handle image datasets in Python.
One of the widely used libraries for image datasets is OpenCV. You can find a variety of resources via the OpenCV official website, which is linked just below.
Let’s start by reading images with code lines.
First of all, we will import some libraries.
import numpy as npimport cv2
> OpenCV is imported as the name ‘cv2’ in the last line.
image = cv2.imread(‘./image_file_path.jpg’)
> using the mpimg.imread function, we are going to read an image.
image2 = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)gray_image = cv2.cvtColor(image2, cv2.COLOR_RGB2GRAY)
> The thing is, cv2, OpenCV library read images as BGR, which is unlike the usual way, RGB. (BGR: Blue-Green-Red, RGB: Red-Green-Blue)
Therefore, we have to change the order of the color channels from BGR to RGB by using one of the key features of the library ‘COLOR_BGR2RGB’.
Also, in the last line, with the function ‘COLOR_RGB2GRAY’ we have converted the RGB channel color image into a grayscale image.
We can also split an RGB color image into three different color channeled ones; Red channel, Green channel, and Blue channel image.
import matplotlib.image as mpimgrgb_image = mpimg.imread(‘./image_file_path.jpg’)
> We will use mpimg to read an image as an RGB.
r = rgb_image[:, :, 0]g = rgb_image[:, :, 1]b = rgb_image[:, :, 2]
> The very first line indicates the red channel, with the third dimension value of zero. For the green channel, the third dimension value is 1, and for the blue, the value is 2.
And now, let’s see the individual color channel’s outcome.
#visualize individual color channel
import matplotlib.pyplot as plt
f, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize = (20,10))ax1.set_title(‘R channel’)ax1.imshow(r, cmap = ‘gray’)ax2.set_title(‘G channel’)ax2.imshow(g, cmap = ‘gray’)ax3.set_title(‘B channel’)ax3.imshow(b, cmap = ‘gray’)
> We imported matplotlib.pyplot for the image representation. This library is widely used in graphic visualizations.
Using the subplot function, we will show three different images which have been filtered out from the red, green, and blue channel.
> I used a yawning tiger image for the practice. From the left side, we can see the different color channels filtered out an image in three different ways.
Credits to Udacity Compuser Vision Nanodegree
Any comment or feedback is always welcome and will come back with another interesting story!