· Hakan Çelik · OpenCV / Machine Learning · 2 dk okuma

Understanding SVM

Learn an intuitive understanding of Support Vector Machines (SVM). We cover decision boundaries, support vectors, margin maximization, and kernel trick for non-linearly separable data.

Understanding SVM

Goal

In this chapter we will see an intuitive understanding of SVM.

Theory

Linearly Separable Data

Consider an image below which has two types of data, red and blue. We find a line, f(x) = ax₁ + bx₂ + c which divides both the data to two regions. When we get a new test_data X, just substitute it in f(X). If f(X) > 0, it belongs to blue group, else it belongs to red group. We can call this line as Decision Boundary.

So what SVM does is to find a straight line (or hyperplane) with largest minimum distance to the training samples. These points closest to the decision boundary are called Support Vectors and the lines passing through them are called Support Planes.

Non-Linearly Separable Data

Consider some data which can’t be divided into two with a straight line. For example, one-dimensional data where ‘X’ is at -3 & +3 and ‘O’ is at -1 & +1. If we map this data set with a function f(x) = x², we get ‘X’ at 9 and ‘O’ at 1 which are linear separable.

In general, it is possible to map points in a d-dimensional space to some D-dimensional space (D > d) to check the possibility of linear separability. The kernel function helps compute the dot product in the high-dimensional (kernel) space by performing computations in the low-dimensional input (feature) space.

SVM in OpenCV

import cv2 as cv
import numpy as np

# Training data
labels = np.array([1, 1, -1, -1])
trainingData = np.matrix([[501, 10], [255, 10], [501, 255], [10, 501]], dtype=np.float32)

# Create SVM and train
svm = cv.ml.SVM_create()
svm.setType(cv.ml.SVM_C_SVC)
svm.setKernel(cv.ml.SVM_LINEAR)
svm.setTermCriteria((cv.TERM_CRITERIA_MAX_ITER, 100, 1e-6))
svm.train(trainingData, cv.ml.ROW_SAMPLE, labels)

SVM icon 1

SVM icon 2


Source: OpenCV Python Tutorials — Original Documentation

Back to Blog

Related Posts

View All Posts »
How OpenCV-Python Bindings Work

How OpenCV-Python Bindings Work

OpenCV · 3 dk

Learn how OpenCV-Python bindings are generated from C++ headers. We cover CV_EXPORTS_W, CV_WRAP, and other macros, plus the gen2.py generator and hdr_parser.py header parser scripts.

Face Detection using Haar Cascades

Face Detection using Haar Cascades

OpenCV · 3 dk

Learn to use Haar Cascade classifiers in OpenCV for face and eye detection. This tutorial covers the theory behind Haar features, integral images, AdaBoost, and cascade classifiers.

High Dynamic Range (HDR) Imaging

High Dynamic Range (HDR) Imaging

OpenCV · 3 dk

Learn how to generate and display HDR images from an exposure sequence in OpenCV. We cover Debevec, Robertson, and Mertens exposure fusion algorithms with camera response function estimation.

Image Inpainting

Image Inpainting

OpenCV · 2 dk

Learn how to remove small noises, strokes, and damage from old photographs using OpenCV's cv.inpaint(). We cover the Telea and Navier-Stokes inpainting algorithms.