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. As with textual data, there are also many pretrained classifiers available for images that we can use to extract features or objects of interest to use as inputs to our own models.
To work with images, we will primarily use the Open Source Computer Vision Library
(OpenCV). While there are a number of good libraries out there, OpenCV
is the most popular and well-documented library for handling images. It can occasionally be challenging to install, but if you run into issues, there are many guides online. This book in particular was written with opencv-python-headless==4.7.0.68
. You can also run these chapters with the ML in Python Cookbook Runner to ensure all commands are reproducible.
Throughout this chapter, we will use as examples a set of images, which is available to download from GitHub.
8.1 Loading Images
Problem
You want to load an image for preprocessing.
Solution
Use OpenCV’s imread
:
# Load libraries
import
cv2
import
numpy
as
np
from
matplotlib
import
pyplot
as
plt
# Load image as grayscale
image
=
cv2
.
imread
(
"images/plane.jpg"
,
cv2
.
IMREAD_GRAYSCALE
)
If we want to view the image, ...
Get Machine Learning with Python Cookbook, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.