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 librariesimportcv2importnumpyasnpfrommatplotlibimportpyplotasplt# Load image as grayscaleimage=cv2.imread("images/plane.jpg",cv2.IMREAD_GRAYSCALE)
If we want to view the image, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access