In this section, we will use a powerful image-processing library called OpenCV and use it to convert an image to grayscale. We will take a color image of a road, as shown in the following screenshot:
Fig 4.10: Sample image
In the following steps, we will convert the color image into grayscale using the OpenCV library:
- First, import the matplotlib (mpimg and pyplot), numpy, and openCV libraries:
In[1]: import matplotlib.image as mpimg
In[2]: import matplotlib.pyplot as plt
In[3]: import numpy as np
In[4]: import cv2
- Next, import the image for the operation:
In[5]: image_color = mpimg.imread('image.jpg')
In[6]: plt.imshow(image_color)
Let's see what our image looks like:
Fig 4.11: Reading an image using matplotlib
- In the preceding screenshot, the image has three channels because it is in an RGB format. Let's check the size of the image. We can see that the value is (515, 763, 3):
In [7]: image_color...