Explain basic image operations of Matplotlib.

10 b] Explain basic image operations of Matplotlib.

Loading Images

If you encounter image formats that are not supported by Matplotlib, we recommend using the Pillow library to load the image. In Matplotlib, loading images is part of the image submodule. We use the alias mpimg for the submodule, as follows:

import matplotlib.image as mpimg

The mpimg.imread(fname) reads an image and returns it as a numpy.array object. For grayscale images, the returned array has a shape (height, width), for RGB images (height, width, 3), and for RGBA images (height, width, 4). The array values range from 0 to 255.

img_filenames = os.listdir('../../Datasets/images')
imgs = [mpimg.imread(os.path.join('../../Datasets/images', img_filename)) 
for img_filename in img_filenames]

The os.listdir() method in Python is used to get the list of all files and directories in the specified directory and then the os.path.join() function is used to join one or more path components intelligently.

Saving Images

The mpimg.imsave(fname, array) saves a numpy.array object as an image file. If the format parameter is not given, the format is deduced from the filename extension. With the optional parameters vmin and vmax, the color limits can be set manually. For a grayscale image, the default for the optional parameter, cmap, is 'viridis'; you might’ want to change it to ‘gray’.

Plotting a Single Image

The plt.imshow(img) displays an image and returns an AxesImage object. For grayscale images with shape (height, width), the image array is visualized using a colormap. The default colormap is ‘viridis‘, as illustrated in Figure 3.41. To actually visualize a grayscale image, the colormap has to be set to ‘gray’ (that is, plt.imshow(img, cmap='gray'), which is illustrated in Figure 3.42. Values for grayscale, RGB, and RGBA images can be either float or uint8, and range from [0…1] or [0…255], respectively. To manually define the value range, the parameters vmin and vmax must be specified. A visualization of an RGB image is shown in the following figures:

grayscale image with a default viridis colormap

Sometimes, it might be helpful to get an insight into the color values. We can simply add a color bar to the image plot. It is recommended to use a colormap with high contrast—for example, jet:

plt.imshow(img, cmap='jet')
plt.colorbar()
image with a jet colormap and color bar

Another way to get insight into the image values is to plot a histogram, as shown in the following diagram. To plot the histogram for an image array, the array has to be flattened using numpy.ravel:

plt.hist(img.ravel(), bins=256, range=(0, 1))
histogram of image values
Plotting Multiple Images in a Grid

To plot multiple images in a grid, we can simply use plt.subplots and plot an image per Axes:

fig, axes = plt.subplots(1, 2)
for i in range(2):
    axes[i].imshow(imgs[i])

The result of the preceding code is shown in the following diagram:

multiple images within a grid

Leave a Reply

Your email address will not be published. Required fields are marked *