08.06.2015 Views

Building Machine Learning Systems with Python - Richert, Coelho

Building Machine Learning Systems with Python - Richert, Coelho

Building Machine Learning Systems with Python - Richert, Coelho

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 10<br />

However, some specialized equipment (mostly in scientific fields) can take images<br />

<strong>with</strong> more bit resolution. 12 or 16 bits are common. Mahotas can deal <strong>with</strong> all these<br />

types, including floating point images (not all operations make sense <strong>with</strong><br />

floating point numbers, but when they do, mahotas supports them). In many<br />

computations, even if the original data is composed of unsigned integers, it is<br />

advantageous to convert to floating point numbers in order to simplify handling<br />

of rounding and overflow issues.<br />

Mahotas can use a variety of different input/output backends.<br />

Unfortunately, none of them can load all existing image formats<br />

(there are hundreds, <strong>with</strong> several variations of each). However,<br />

loading PNG and JPEG images is supported by all of them. We<br />

will focus on these common formats and refer you to the mahotas<br />

documentation on how to read uncommon formats.<br />

The return value of mh.imread is a NumPy array. This means that you can use<br />

standard NumPy functionalities to work <strong>with</strong> images. For example, it is often useful<br />

to subtract the mean value of the image from it. This can help to normalize images<br />

taken under different lighting conditions and can be accomplished <strong>with</strong> the standard<br />

mean method:<br />

image = image – image.mean()<br />

We can display the image on screen using maplotlib, the plotting library we have<br />

already used several times:<br />

from matplotlib import pyplot as plt<br />

plt.imshow(image)<br />

plt.show()<br />

This shows the image using the convention that the first dimension is the height<br />

and the second the width. It correctly handles color images as well. When using<br />

<strong>Python</strong> for numerical computation, we benefit from the whole ecosystem working<br />

well together.<br />

Basic image processing<br />

We will start <strong>with</strong> a small dataset that was collected especially for this book. It has<br />

three classes: buildings, natural scenes (landscapes), and pictures of texts. There are<br />

30 images in each category, and they were all taken using a cell phone camera <strong>with</strong><br />

minimal composition, so the images are similar to those that would be uploaded<br />

to a modern website. This dataset is available from the book's website. Later in the<br />

chapter, we will look at a harder dataset <strong>with</strong> more images and more categories.<br />

[ 201 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!