A very common input to machine learning models are images, and images are typically represented as a matrix. Every cell in this matrix represents a particular pixel in the image, and each pixel holds some value based on the kind of image this is.
For a color image, every pixel is represented using three separate values. These are RGB values where each value is a number between zero and 255. For example, the color red will be represented by 255, 0, 0, the color green by 0, 255, 0, and the color blue by 0, 0, 255. Color images are called three channel images because each pixel requires three values to represent its information.
Grayscale image, on the other hand, require just one value to represent the information in one pixel. This value represents the intensity of that pixel, and is typically a number between zero and one. It can be a number between 0 and 255 as well. We typically divide it by 255 to get a number between zero and one.
OpenCV == Open Source Computer Vision Library
!pip install opencv-python
import cv2
# Image with dimensions 173x130
imagePath = '../data/dog.jpg'
image = cv2.imread(imagePath)
%matplotlib inline
import matplotlib
import matplotlib.pyplot as plt
plt.imshow(image)
image.shape
image
image[0][0]
size=(32, 32)
resized_image_feature_vector = cv2.resize(image, size)
plt.imshow(resized_image_feature_vector)
resized_image_feature_vector.shape
resized_image_feature_vector
You can view the three-dimensional matrix. In some cases you might find that when you feed an image into your model, you might want to represent it as a 1D array. This can be done using the flatten operation on your image matrix. The flatten function flattens all of the dimension of the array. There were three dimensions in the original array. The final array has just a single dimension. It is a vector, and the length of that vector is 3072, which is 32 multiplied by 32 multiplied by three
resized_flattened_image_feature_vector = resized_image_feature_vector.flatten()
len(resized_flattened_image_feature_vector)
image_grayscale = cv2.imread(imagePath, cv2.IMREAD_GRAYSCALE )
Go ahead and view this image using matplotlib, and you can see it's the same dog, but this time in grayscale. The shape of the image will be different, though. You will see that it is a 130 by 173 image, but the last dimension is one because this is a single channel image The last dimension isn't explicitly specified here because it's represented as a scaler.
plt.imshow(image_grayscale)
image_grayscale.shape
image_grayscale
import numpy as np
expanded_image_grayscale = np.expand_dims(image_grayscale, axis=2)
expanded_image_grayscale.shape
expanded_image_grayscale
If you display the actual image matrix, you will see that there is a single intensity value for each pixel, and this intensity number is between zero and 255. Here we used the expand_dims function to basically expand the third axis, the axis at index two for our image. This will add an additional dimension to represent the pixel intensity. So now the shape of our image will be 130 by 173 by one