Loading and displaying images

In order to manipulate images, we will use a package called mahotas. You can obtain mahotas through anaconda and read its manual at https://mahotas.readthedocs.io. Mahotas is an open source package (it has an MIT license, so it can be used in any project) and was developed by one of the authors of this book. It is based on NumPy. Thus, the NumPy knowledge you have acquired so far can be used for image processing. There are other image packages, such as scikit-image (skimage), the n-dimensional image (ndimage) module in SciPy, and the Python bindings for OpenCV. All of these work natively with NumPy arrays, so you can even mix and match functionality from different packages to build a combined pipeline.

We start by importing mahotas with the mh abbreviation, which we will use throughout this chapter, as follows:

import mahotas as mh 

Now, we can load an image file using imread as follows:

image = mh.imread('scene00.jpg') 

The scene00.jpg file (this file is contained in the dataset available in this book's companion code repository) is a color image of height h and width w; the image will be an array of shape (h, w, 3). The first dimension is the height, the second is the width, and the third is red/green/blue. Other systems put the width in the first dimension, but this is the convention that is used by all NumPy-based packages. The type of array will typically be np.uint8 (an unsigned 8-bit integer). These are the images that your camera takes and that your monitor can fully display.

Some specialized equipment, used in scientific and technical applications, can take images with a higher bit resolution (that is, with more sensitivity to small variations in brightness). 12 or 16 bits are common in this type of equipment. Mahotas can deal with all these types, including floating point images. In many computations, even if the original data is composed of unsigned integers, it is advantageous to convert to floating point numbers in order to simplify the handling of rounding and overflow issues.

Mahotas can use a variety of different input/output backends. Unfortunately, none of them can load all image formats that exist (there are hundreds, with several variations of each). However, the loading of PNG and JPEG images is supported by all of them. We will focus on these common formats and refer you to the mahotas documentation on how to read uncommon formats.

We can display the image on screen using matplotlib, and the plotting library we have already used several times, as follows:

from matplotlib import pyplot as plt 
fig,ax = plt.subplots() 
ax.imshow(image) 

As shown in the following screenshot, this code shows the image using the convention that the first dimension is the height and the second the width. It correctly handles color images as well. When using Python for numerical computation, we benefit from the whole ecosystem working well together: mahotas works with NumPy arrays, which can be displayed with matplotlib. Later, we will compute features from images to use with scikit-learn:

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

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