© Ashwin Pajankar 2017

Ashwin Pajankar, Raspberry Pi Supercomputing and Scientific Programming, 10.1007/978-1-4842-2878-4_14

14. Matplotlib

Ashwin Pajankar

(1)Nashik, Maharashtra, India

In the last chapter, we studied digital image processing with SciPy. We studied a few of the major classes of functions offered by SciPy, consolidated under scipy.misc, for digital image processing. In this chapter, we will study a few more image processing and data representation techniques with matplotlib. We have already used matplotlib in earlier chapters for plotting and displaying images. As mentioned in earlier chapters, matplotlib is a MATLAB-style data visualization library. Data processing and mining is a vast topic and outside the scope of this book; however, we can use images as a convenient data source to demonstrate some of the data processing capabilities of matplotlib. So let's get started with that.

Reading an Image

Create a directory chapter 14 for the code samples. The following code (Listing 14-1) demonstrates how to read and display an image:

Listing 14-1. prog01.py
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
img = mpimg.imread('/home/pi/book/Dataset/Sample01.jpg')
plt.imshow(img)
plt.show()

The output (Figure 14-1) is as follows:

A447085_1_En_14_Fig1_HTML.jpg
Figure 14-1. Reading and displaying an image

Colormaps

Colormaps are used to apply colors to a dataset. Grayscale images are automatically applied with the default colormaps. We can even set the colormap for the image. To display grayscale images properly, we need to set the colormap to the value gray as we have done in earlier chapters. In the example below (Listing 14-2), we are going to display one of the channels of an image with the default colormap. Then we will apply another colormap to the channel.

Listing 14-2. prog02.py
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np


img = mpimg.imread('/home/pi/book/Dataset/4.2.01.tiff')
img_ch = img[:, :, 0]


plt.subplot(1, 2, 1)
plt.imshow(img_ch)
plt.title('Default Colormap')
plt.xticks([]), plt.yticks([])


plt.subplot(1, 2, 2)
plt.imshow(img_ch, cmap='hot')
plt.title('Hot Colormap')
plt.xticks([]), plt.yticks([])


plt.show()

The output (Figure 14-2) is as follows:

A447085_1_En_14_Fig2_HTML.jpg
Figure 14-2. Colormaps

Colorbar

We can also display the colorbar to let the viewers know the relative intensity values in an image. The following code (Listing 14-3) demonstrates that:

Listing 14-3. prog03.py
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np


img = mpimg.imread('/home/pi/book/Dataset/4.2.01.tiff')
img_ch = img[:, :, 0]


plt.imshow(img_ch, cmap='nipy_spectral')
plt.title('Colorbar Demo')
plt.colorbar()
plt.xticks([]), plt.yticks([])


plt.show()

The output (Figure 14-3) is as follows:

A447085_1_En_14_Fig3_HTML.jpg
Figure 14-3. Colorbar demo

Matplotlib for Image Processing

A histogram is the graphical representation of frequency tables in statistics. It is a graph of the number of occurrences for every value in the dataset. We can also use the plt.hist() method in matplotlib for plotting the histogram of a single channel or a grayscale image. The following (Listing 14-4) is an example:

Listing 14-4. prog04.py
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np


img = mpimg.imread('/home/pi/book/Dataset/Sample03.jpg')
img_ch = img[:, :, 0]


plt.hist(img_ch.ravel(), 256, [0, 256])
plt.title('Histogram Demo')
plt.xticks([]), plt.yticks([])


plt.show()

The output (Figure 14-4) is as follows:

A447085_1_En_14_Fig4_HTML.jpg
Figure 14-4. Histogram demo

Interpolation Methods

There are many interpolation types in plt.imshow(). The interpolation type decides how the image is to be displayed. The best way to understand how they work is to use them against a gradient image. The following (Listing 14-5) code example demonstrates this very well:

Listing 14-5. prog05.py
import matplotlib.pyplot as plt
import numpy as np


methods = [None, 'none', 'nearest', 'bilinear', 'bicubic', 'spline16',
           'spline36', 'hanning', 'hamming', 'hermite', 'kaiser', 'quadric',
           'catrom', 'gaussian', 'bessel', 'mitchell', 'sinc', 'lanczos']


grid = np.arange(16).reshape(4, 4)

fig, axes = plt.subplots(3, 6, figsize=(12, 6),
subplot_kw={'xticks': [], 'yticks': []})


fig.subplots_adjust(hspace=0.3, wspace=0.05)

for ax, interp_method in zip(axes.flat, methods):
    ax.imshow(grid, interpolation=interp_method)
    ax.set_title(interp_method)


plt.show()

The output (Figure 14-5) is as follows:

A447085_1_En_14_Fig5_HTML.jpg
Figure 14-5. Interpolation demo

Conclusion

In this chapter, we learned how to represent data with matplotlib. We studied colormaps, colorbars, and histograms. We also studied the concept of interpolation. We can use matplotlib this way for representing data, images, and signals.

Summary of the Book

In this book, we got started with the very fundamentals of Raspberry Pi and single board computers. We learned how to set up the Raspberry Pi and connect it to the Internet. We also learned to access it with a network.

Then we moved on to the basics of supercomputing and parallel programming. We prepared the nodes of our Pis and joined them together over a network for them to act as a cluster. We also exploited the power of the cluster with MPI4PY.

Then we studied symbolic computing with Python. We also studied the NumPy library for numerical computation. We then explored the scientific computing library SciPy and its applications in signal and image processing.

Finally, we studied how to represent image data and calculate histograms with the matplotlib library.

This is not the end, though. This is merely the beginning of your journey in the amazing world of scientific computing. You can further explore matplotlib, OpenCV, and Scientific Kit (SciKit) libraries. For folks who want to try OS programming, they can explore pthread and POSIX libraries in C on Raspberry Pi. Possibilities are endless with the Raspberry Pi. I wish you all the best in getting started on this amazing journey of exploration.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.80.15