Chapter 8. Handling Images

8.0 Introduction

Image classification is one of the most exciting areas of machine learning. The ability of computers to recognize patterns and objects from images is an incredibly powerful tool in our toolkit. However, before we can apply machine learning to images, we often first need to transform the raw images to features usable by our learning algorithms.

To work with images, we will use the Open Source Computer Vision Library (OpenCV). While there are a number of good libraries out there, OpenCV is the most popular and documented library for handling images. One of the biggest hurdles to using OpenCV is installing it. However, fortunately if we are using Python 3 (at the time of publication OpenCV does not work with Python 3.6+), we can use Anaconda’s package manager tool conda to install OpenCV in a single line of code in our terminal:

conda install --channel opencv3

Afterward, we can check the installation by opening a notebook, importing OpenCV, and checking the version number (3.1.0):

import cv2


If installing OpenCV using conda does not work, there are many guides online.

Finally, throughout this chapter we will use a set of images as examples, which are available to download on GitHub.

8.1 Loading Images


You want to load an image for preprocessing.


Use OpenCV’s imread:

# Load library
import cv2
import numpy as np
from matplotlib import pyplot as plt

# Load image as grayscale ...

Get Machine Learning with Python Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.