Support Vector Machines for Image Classification and Detection Using OpenCV

In a previous tutorial, we explored using the Support Vector Machine algorithm as one of the most popular supervised machine learning techniques implemented in the OpenCV library.

So far, we have seen how to apply Support Vector Machines to a custom dataset that we have generated, consisting of two-dimensional points gathered into two classes.

In this tutorial, you will learn how to apply OpenCV’s Support Vector Machine algorithm to solve image classification and detection problems. 

After completing this tutorial, you will know:

  • Several of the most important characteristics of Support Vector Machines.
  • How to apply Support Vector Machines to the problems of image classification and detection. 

Kick-start your project with my book Machine Learning in OpenCV. It provides self-study tutorials with working code.


Let’s get started. 

Support Vector Machines for Image Classification and Detection Using OpenCV
Photo by Patrick Ryan, some rights reserved.

Tutorial Overview

This tutorial is divided into three parts; they are:

  • Recap of How Support Vector Machines Work
  • Applying the SVM Algorithm to Image Classification
  • Using the SVM Algorithm for Image Detection

Recap of How Support Vector Machines Work

In a previous tutorial, we were introduced to using the Support Vector Machine (SVM) algorithm in the OpenCV library. So far, we have applied it to a custom dataset that we have generated, consisting of two-dimensional points gathered into two classes.

We have seen that SVMs seek to separate data points into classes by computing a decision boundary that maximizes the margin to the closest data points from each class, called the support vectors. The constraint of maximizing the margin can be relaxed by tuning a parameter called C, which controls the trade-off between maximizing the margin and reducing the misclassifications on the training data. 

The SVM algorithm may use different kernel functions, depending on whether the input data is linearly separable. In the case of non-linearly separable data, a non-linear kernel may be used to transform the data to a higher-dimensional space in which it becomes linearly separable. This is analogous to the SVM finding a non-linear decision boundary in the original input space. 

Applying the SVM Algorithm to Image Classification

We will use the digits dataset in OpenCV for this task, although the code we will develop may also be used with other datasets. 

Want to Get Started With Machine Learning with OpenCV?

Take my free email crash course now (with sample code).

Click to sign-up and also get a free PDF Ebook version of the course.

Our first step is to load the OpenCV digits image, divide it into its many sub-images that feature handwritten digits from 0 to 9, and create their corresponding ground truth labels that will enable us to quantify the accuracy of the trained SVM classifier later. For this particular example, we will allocate 80% of the dataset images to the training set and the remaining 20% of the images to the testing set:

Our next step is to create an SVM in OpenCV that uses an RBF kernel. As we have done in our previous tutorial, we must set several parameter values related to the SVM type and the kernel function. We shall also include the termination criteria to stop the iterative process of the SVM optimization problem:

Rather than training and testing the SVM on the raw image data, we will first convert each image into its HOG descriptors, as explained in this tutorial. The HOG technique aims for a more compact representation of an image by exploiting its local shape and appearance. Training a classifier on HOG descriptors can potentially increase its discriminative power in distinguishing between different classes while at the same time reducing the computational expense of processing the data:

We may finally train the SVM on the HOG descriptors and proceed to predict labels for the testing data, based on which we may compute the classifier’s accuracy:

For this particular example, the values for C and gamma are being set empirically. However, it is suggested that a tuning technique, such as the grid search algorithm, is employed to investigate whether a better combination of hyperparameters can push the classifier’s accuracy even higher. 

The complete code listing is as follows:

Using the SVM Algorithm for Image Detection

It is possible to extend the ideas we have developed above from image classification to image detection, where the latter refers to identifying and localizing objects of interest within an image. 

We can achieve this by repeating the image classification we developed in the previous section at different positions within a larger image (we will refer to this larger image as the test image). 

For this particular example, we will create an image that consists of a collage of randomly selected sub-images from OpenCV’s digits dataset, and we will then attempt to detect any occurrences of a digit of interest.

Let’s start by creating the test image first. We will do so by randomly selecting 25 sub-images equally spaced across the entire dataset, shuffling their order, and joining them together into a $100\times 100$-pixel image:

The resulting test image looks as follows:

Test Image for Image Detection

Next, we shall train a newly created SVM like in the previous section. However, given that we are now addressing a detection problem, the ground truth labels should not correspond to the digits in the images; instead, they should distinguish between the positive and the negative samples in the training set. 

Say, for instance, that we are interested in detecting the two occurrences of the 0 digit in the test image. Hence, the images featuring a 0 in the training portion of the dataset are taken to represent the positive samples and distinguished by a class label of 1. All other images belonging to the remaining digits are taken to represent the negative samples and consequently distinguished by a class label of 0.

Once we have the ground truth labels generated, we may proceed to create and train an SVM on the training dataset:

The final piece of code that we shall be adding to the code listing above performs the following operations:

  1. Traverses the test image by a pre-defined stride.
  2. Crops an image patch of equivalent size to the sub-images that feature the digits (i.e., 20 $\times$ 20 pixels) from the test image at every iteration. 
  3. Extracts the HOG descriptors of every image patch. 
  4. Feeds the HOG descriptors into the trained SVM to obtain a label prediction. 
  5. Stores the image patch coordinates whenever a detection is found.
  6. Draws the bounding box for each detection on the original test image. 

The complete code listing is as follows:

The resulting image shows that we have successfully detected the two occurrences of the 0 digit in the test image:

Detecting the Two Occurrences of the 0 Digit

We have considered a simple example, but the same ideas can be easily adapted to address more challenging real-life problems. If you plan to adapt the code above to more challenging problems:

  • Remember that the object of interest may appear in various sizes inside the image, so you might need to carry out a multi-scale detection task. 
  • Do not run into the class imbalance problem when generating positive and negative samples to train your SVM. The examples we have considered in this tutorial were images of very little variation (we were limited to just 10 digits, featuring no variation in scale, lighting, background, etc.), and any dataset imbalance seems to have had very little effect on the detection result. However, real-life challenges do not tend to be this simple, and an imbalanced distribution between classes can lead to poor performance.

Further Reading

This section provides more resources on the topic if you want to go deeper.

Books

Websites

Summary

In this tutorial, you learned how to apply OpenCV’s Support Vector Machine algorithm to solve image classification and detection problems.

Specifically, you learned:

  • Several of the most important characteristics of Support Vector Machines.
  • How to apply Support Vector Machines to the problems of image classification and detection.

Do you have any questions?

Ask your questions in the comments below, and I will do my best to answer.

Get Started on Machine Learning in OpenCV!

Machine Learning in OpenCV

Learn how to use machine learning techniques in image processing projects

...using OpenCV in advanced ways and work beyond pixels

Discover how in my new Ebook:
Machine Learing in OpenCV

It provides self-study tutorials with all working code in Python to turn you from a novice to expert. It equips you with
logistic regression, random forest, SVM, k-means clustering, neural networks, and much more...all using the machine learning module in OpenCV

Kick-start your deep learning journey with hands-on exercises


See What's Inside

, , , ,

No comments yet.

Leave a Reply