Book Image

Machine Learning for OpenCV 4 - Second Edition

By : Aditya Sharma, Vishwesh Ravi Shrimali, Michael Beyeler
Book Image

Machine Learning for OpenCV 4 - Second Edition

By: Aditya Sharma, Vishwesh Ravi Shrimali, Michael Beyeler

Overview of this book

OpenCV is an opensource library for building computer vision apps. The latest release, OpenCV 4, offers a plethora of features and platform improvements that are covered comprehensively in this up-to-date second edition. You'll start by understanding the new features and setting up OpenCV 4 to build your computer vision applications. You will explore the fundamentals of machine learning and even learn to design different algorithms that can be used for image processing. Gradually, the book will take you through supervised and unsupervised machine learning. You will gain hands-on experience using scikit-learn in Python for a variety of machine learning applications. Later chapters will focus on different machine learning algorithms, such as a decision tree, support vector machines (SVM), and Bayesian learning, and how they can be used for object detection computer vision operations. You will then delve into deep learning and ensemble learning, and discover their real-world applications, such as handwritten digit classification and gesture recognition. Finally, you’ll get to grips with the latest Intel OpenVINO for building an image processing system. By the end of this book, you will have developed the skills you need to use machine learning for building intelligent computer vision applications with OpenCV 4.
Table of Contents (18 chapters)
Free Chapter
1
Section 1: Fundamentals of Machine Learning and OpenCV
6
Section 2: Operations with OpenCV
11
Section 3: Advanced Machine Learning with OpenCV

Implementing your first perceptron

Perceptrons are easy enough to be implemented from scratch. We can mimic the typical OpenCV or scikit-learn implementation of a classifier by creating a perceptron object. This will allow us to initialize new perceptron objects that can learn from data via a fit method and make predictions via a separate predict method.

When we initialize a new perceptron object, we want to pass a learning rate (lr, or η in the previous section) and the number of iterations after which the algorithm should terminate (n_iter):

In [1]: import numpy as np
In [2]: class Perceptron(object):
... def __init__(self, lr=0.01, n_iter=10):
... self.lr = lr
... self.n_iter = n_iter
...

The fit method is where most of the work is done. This method should take as input some data samples (X) and their associated target labels (y). We will then create an array...