Reader small image

You're reading from  Mastering OpenCV 4 with Python

Product typeBook
Published inMar 2019
Reading LevelExpert
PublisherPackt
ISBN-139781789344912
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Alberto Fernández Villán
Alberto Fernández Villán
author image
Alberto Fernández Villán

Alberto Fernndez Villn is a software engineer with more than 12 years of experience in developing innovative solutions. In the last couple of years, he has been working in various projects related to monitoring systems for industrial plants, applying both Internet of Things (IoT) and big data technologies. He has a Ph.D. in computer vision (2017), a deep learning certification (2018), and several publications in connection with computer vision and machine learning in journals such as Machine Vision and Applications, IEEE Transactions on Industrial Informatics, Sensors, IEEE Transactions on Industry Applications, IEEE Latin America Transactions, and more. As of 2013, he is a registered and active user (albertofernandez) on the Q&A OpenCV forum.
Read more about Alberto Fernández Villán

Right arrow

Machine Learning with OpenCV

Machine learning is an application of artificial intelligence that provides computers (and other systems with the certain capability of computation) with the capability to automatically make predictions or decisions from experience without being explicitly programmed to carry out the task. The concept of machine learning has been around for a long time, but it has been gaining momentum over the last several years mainly due to three key factors:

  • There is enormously increased data.
  • There are significantly improved algorithms.
  • There is substantially more powerful computer hardware. Virtual personal assistants (for example, smart speakers or mobile apps), predictions while commuting (traffic predictions or navigation services), video systems (surveillance camera systems or license plate recognition systems), and e-commerce applications (recommendation...

Technical requirements

The technical requirements are listed as follows:

  • Python and OpenCV
  • A Python-specific IDE
  • The NumPy and Matplotlib packages
  • The Git client

Further details about how to install these requirements are mentioned in Chapter 1, Setting Up OpenCV. The GitHub repository for Mastering OpenCV 4 with Python, which contains all the supporting project files necessary to work through this book from the first chapter to the last, can be accessed at https://github.com/PacktPublishing/Mastering-OpenCV-4-with-Python.

An introduction to machine learning

In Chapter 1, Setting Up OpenCV, we introduced the concepts of computer vision, artificial intelligence, machine learning, neural networks, and deep learning, which can be structured in a hierarchical way, as shown here:

As can be seen, the Artificial Intelligence topic includes all of the other topics. In this chapter, we are going to focus on Machine Learning.

See Chapter 1, Setting Up OpenCV, if you want a refresher on these concepts.

Machine Learning is the process of programming computers to learn from historical data to make predictions on new data. Machine Learning is a sub-discipline of artificial intelligence and refers to statistical techniques, by which machines perform on the basis of learned interrelationships. On the basis of gathered or collected data, algorithms are independently learned by computers.

In the context of Machine...

k-means clustering

OpenCV provides the cv2.kmeans() function, which implements a k-means clustering algorithm, which finds centers of clusters and groups input samples around the clusters.

The objective of the k-means clustering algorithm is to partition (or cluster) n samples into K clusters where each sample will belong to the cluster with the nearest mean. The signature of the cv2.kmeans() function is as follows:

retval, bestLabels, centers=cv.kmeans(data, K, bestLabels, criteria, attempts, flags[, centers])

data represents the input data for clustering. It should be of np.float32 data type, and each feature should be placed in a single column. K specifies the number of clusters required at the end. The algorithm-termination criteria are specified with the criteria parameter, which sets the maximum number of iterations and/or the desired accuracy. When these criteria are...

k-nearest neighbor

k-nearest neighbours (kNN) is considered one of the simplest algorithms in the category of supervised learning. kNN can be used for both classification and regression problems. In the training phase, kNN stores both the feature vectors and class labels of all of the training samples. In the classification phase, an unlabeled vector (a query or test vector in the same multidimensional feature space as the training examples) is classified as the class label that is most frequent among the k training samples nearest to the unlabeled vector to be classified, where k is a user-defined constant.

This can be seen graphically in the next diagram:

In the previous diagram, if k = 3, the green circle (the unlabeled test sample) will be classified as a triangle because there are two triangles and only one square inside the inner circle. If k = 5, the green circle will...

Support vector machine

A Support Vector Machine (SVM) is a supervised learning technique that constructs a hyperplane or a set of hyperplanes in a high-dimensional space by best separating the training examples according to its assigned class.

This can be seen in the next diagram, where the green line is the representation of the hyperplane that best separates the two classes because the distance to the nearest element of each of the two classes is the largest:

In the first case, the decision boundary is a line while, in the second case, the decision boundary is a circumference. The dashed lines and the dashed circumference represent other decision boundaries, but they do not best separate both classes.

SVM implementation in OpenCV is based on LIBSVM: A library for support vector machines (2011) (https://www.csie.ntu.edu.tw/~cjlin/libsvm/). To create an empty model, the cv2...

Summary

In this chapter, we covered a complete introduction to machine learning.

In the first section, we contextualized the concept of machine learning and how it is related to other hot topics, such as artificial intelligence, neural networks, and deep learning. Additionally, we summarized the three main approaches in machine learning and discussed the three most common techniques to solve classification, regression, and clustering problems.Then, we applied the most common machine learning techniques to solve some real-world problems. More specifically, we looked at the k-means clustering algorithm, the k-nearest neighbor classifier, and SVM.

In the next chapter, we will explore how to create face-processing projects using state-of-the-art algorithms in connection with face detection, tracking, and recognition.

Questions

  1. What are the three main approaches in the context of machine learning?
  2. What is the difference between a classification and a regression problem?
  3. What function does OpenCV provide to implement a k-means clustering algorithm?
  4. What function does OpenCV provide to create a kNN classifier?
  5. What function does OpenCV provide to find the nearest neighbors?
  6. What function does OpenCV provide to create an SVM classifier?
  7. What is a reasonable first choice for an SVM kernel?

Further reading

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Mastering OpenCV 4 with Python
Published in: Mar 2019Publisher: PacktISBN-13: 9781789344912
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Alberto Fernández Villán

Alberto Fernndez Villn is a software engineer with more than 12 years of experience in developing innovative solutions. In the last couple of years, he has been working in various projects related to monitoring systems for industrial plants, applying both Internet of Things (IoT) and big data technologies. He has a Ph.D. in computer vision (2017), a deep learning certification (2018), and several publications in connection with computer vision and machine learning in journals such as Machine Vision and Applications, IEEE Transactions on Industrial Informatics, Sensors, IEEE Transactions on Industry Applications, IEEE Latin America Transactions, and more. As of 2013, he is a registered and active user (albertofernandez) on the Q&A OpenCV forum.
Read more about Alberto Fernández Villán