Skip to Content
Programming Computer Vision with Python
book

Programming Computer Vision with Python

by Jan Erik Solem
June 2012
Beginner to intermediate
260 pages
6h 28m
English
O'Reilly Media, Inc.
Content preview from Programming Computer Vision with Python

Chapter 8. Classifying Image Content

This chapter introduces algorithms for classifying images and image content. We look at some simple but effective methods as well as state-of-the-art classifiers and apply them to two-class and multi-class problems. We show examples with applications in gesture recognition and object recognition.

8.1 K-Nearest Neighbors

One of the simplest and most used methods for classification is the k-nearest neighbor classifier (kNN). The algorithm simply compares an object (for example a feature vector) to be classified with all objects in a training set with known class labels and lets the k nearest vote for which class to assign. This method often performs well but has a number of drawbacks. Same as with the k-means clustering algorithm, the number k needs to be chosen and the value will affect performance. Furthermore, the method requires the entire training set to be stored, and if this set is large, it will be slow to search. For large training sets some form of binning is usually used to reduce the number of comparisons needed.[21] On the positive side, there are no restrictions on what distance measure to use; practically anything you can think of will work (which is not the same as saying that it will perform well). The algorithm is also trivially parallelizable.

Implementing kNN in a basic form is pretty straightforward. Given a set of training examples and a list of associated labels, the code below does the job. The training examples and labels ...

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.
Start your free trial

You might also like

Learning OpenCV 4 Computer Vision with Python 3 - Third Edition

Learning OpenCV 4 Computer Vision with Python 3 - Third Edition

Joseph Howse, Joe Minichino

Publisher Resources

ISBN: 9781449341916Errata PageSupplemental Content