Learn OpenCV with Python by Examples
By James Chen
()
About this ebook
This book is a comprehensive guide to learning the basics of computer vision and machine learning using the powerful OpenCV library and the Python programming language. The book offers a practical, hands-on approach to learning the concepts and techniques of computer vision through practical examples. All codes in this book are available on Gith
Read more from James Chen
Machine Learning and Deep Learning With Python Rating: 0 out of 5 stars0 ratingsEssentials of Technical Analysis for Financial Markets Rating: 0 out of 5 stars0 ratingsI Want That Pencil: Sharpen Your Cashflow, Pencil Your Future. Rating: 0 out of 5 stars0 ratings
Related to Learn OpenCV with Python by Examples
Related ebooks
Machine Learning in Production: Master the art of delivering robust Machine Learning solutions with MLOps (English Edition) Rating: 0 out of 5 stars0 ratingsNeural Networks with Python Rating: 0 out of 5 stars0 ratingsComputer Vision for the Web Rating: 0 out of 5 stars0 ratingsMatplotlib for Python Developers Rating: 3 out of 5 stars3/5Android Application Programming with OpenCV Rating: 3 out of 5 stars3/5Learning OpenCV 3 Computer Vision with Python - Second Edition Rating: 0 out of 5 stars0 ratingsGraph Databases A Complete Guide - 2019 Edition Rating: 0 out of 5 stars0 ratingsNew Learning of Python by Practical Innovation and Technology Rating: 0 out of 5 stars0 ratingsMicrosoft Azure Machine Learning Rating: 4 out of 5 stars4/5Social Media Data Mining and Analytics Rating: 0 out of 5 stars0 ratingsPractical C Programming: Solutions for modern C developers to create efficient and well-structured programs Rating: 0 out of 5 stars0 ratingsCreating Dynamic UIs with Android Fragments - Second Edition Rating: 0 out of 5 stars0 ratingsSimultaneous multithreading A Complete Guide Rating: 0 out of 5 stars0 ratingsData Visualization with D3 4.x Cookbook - Second Edition Rating: 0 out of 5 stars0 ratingsMastering Android NDK Rating: 0 out of 5 stars0 ratingsDeep Learning for Time Series Cookbook: Use PyTorch and Python recipes for forecasting, classification, and anomaly detection Rating: 0 out of 5 stars0 ratingsGetting Started with Greenplum for Big Data Analytics Rating: 0 out of 5 stars0 ratingsLarge Scale Machine Learning with Python Rating: 2 out of 5 stars2/5From Zero to Market with Flutter Rating: 0 out of 5 stars0 ratingsASP.NET 3.5 CMS Development Rating: 0 out of 5 stars0 ratingsOpenCV Computer Vision Application Programming Cookbook Second Edition Rating: 0 out of 5 stars0 ratingsC++ for Beginners: A Complete Course to Master the Fundamentals of C++ Programming Rating: 0 out of 5 stars0 ratingsBeginning DotNetNuke Skinning and Design Rating: 0 out of 5 stars0 ratingsModern Computer Vision with PyTorch: A practical roadmap from deep learning fundamentals to advanced applications and Generative AI Rating: 0 out of 5 stars0 ratingsPractical Machine Learning Cookbook Rating: 0 out of 5 stars0 ratingsLearn LLVM 12: A beginner's guide to learning LLVM compiler tools and core libraries with C++ Rating: 0 out of 5 stars0 ratings
Intelligence (AI) & Semantics For You
Python for Beginners: A Crash Course to Learn Python Programming in 1 Week Rating: 0 out of 5 stars0 ratingsAlgorithms to Live By: The Computer Science of Human Decisions Rating: 4 out of 5 stars4/5Mastering ChatGPT: 21 Prompts Templates for Effortless Writing Rating: 4 out of 5 stars4/5The ChatGPT Revolution: How to Simplify Your Work and Life Admin with AI Rating: 0 out of 5 stars0 ratingsDeep Utopia: Life and Meaning in a Solved World Rating: 0 out of 5 stars0 ratingsCreating Online Courses with ChatGPT | A Step-by-Step Guide with Prompt Templates Rating: 4 out of 5 stars4/5Scary Smart: The Future of Artificial Intelligence and How You Can Save Our World Rating: 4 out of 5 stars4/5Data Science Bookcamp: Five real-world Python projects Rating: 5 out of 5 stars5/5The Secrets of ChatGPT Prompt Engineering for Non-Developers Rating: 5 out of 5 stars5/5Advances in Financial Machine Learning Rating: 5 out of 5 stars5/5AI Literacy Fundamentals Rating: 0 out of 5 stars0 ratingsDancing with Qubits: How quantum computing works and how it can change the world Rating: 5 out of 5 stars5/5Deep Learning with PyTorch Rating: 5 out of 5 stars5/5The Business Case for AI: A Leader's Guide to AI Strategies, Best Practices & Real-World Applications Rating: 0 out of 5 stars0 ratingsThe Alignment Problem: How Can Machines Learn Human Values? Rating: 4 out of 5 stars4/5Artificial Intelligence: A Guide for Thinking Humans Rating: 4 out of 5 stars4/5Hands-On System Design: Learn System Design, Scaling Applications, Software Development Design Patterns with Real Use-Cases Rating: 0 out of 5 stars0 ratingsDeep Learning with Python. Part 1 Rating: 0 out of 5 stars0 ratingsFun Q: A Functional Introduction to Machine Learning in Q Rating: 0 out of 5 stars0 ratingsStill Room for Humans: Career Planning in an AI World Rating: 0 out of 5 stars0 ratingsGrokking Deep Reinforcement Learning Rating: 5 out of 5 stars5/5Generative AI For Dummies Rating: 2 out of 5 stars2/5JAVA for Beginner's Crash Course: Java for Beginners Guide to Program Java, jQuery, & Java Programming Rating: 4 out of 5 stars4/5Regression Analysis: An Intuitive Guide for Using and Interpreting Linear Models Rating: 5 out of 5 stars5/522 Ideas About The Future Rating: 0 out of 5 stars0 ratingsVirtually Human: The Promise—and the Peril—of Digital Immortality Rating: 4 out of 5 stars4/5
Reviews for Learn OpenCV with Python by Examples
0 ratings0 reviews
Book preview
Learn OpenCV with Python by Examples - James Chen
1. Introduction
Computer vision is an interdisciplinary field that focuses on enabling machines to interpret and understand visual data from the world around us. It involves the development of algorithms, techniques, and technologies that can analyze images and videos, recognize patterns, and extract useful information from visual data.
Computer vision has a wide range of applications, including autonomous vehicles, robotics, medical imaging, security and surveillance, and even entertainment. For example, computer vision algorithms can be used to detect objects in real-time video streams, recognize faces in images, and even help doctors diagnose medical conditions from medical imaging data.
To achieve these tasks, computer vision algorithms use a combination of techniques from various fields, such as image processing, machine learning, deep learning and artificial intelligence. Image processing techniques involve operations such as filtering, edge detection, and segmentation and so on, which help to preprocess and enhance visual data. Machine learning techniques, on the other hand, allow computer vision systems to learn patterns from large datasets and make predictions based on those patterns.
Computer vision is a rapidly evolving field that holds great promise for the development of intelligent systems that can perceive and understand the world around us.
1.1.
About OpenCV
OpenCV is a popular open-source computer vision library that provides a vast range of tools and algorithms for image and video processing. It is originally written in C++, but has interfaces for various programming languages, including Python, Java and so on, it’s a cross-platform library, although this book will focus only on Python. OpenCV is designed to be fast and efficient, making it an ideal choice for real-time computer vision applications, and it has become a standard tool for many computer vision projects.
OpenCV is used for image and video processing, object detection, as well as machine learning. The library comes with many built-in mathematical algorithms and is fast enough for real-time video processing. Today it’s widely used for resolving the related problems. Reference the official document for more information at below link,
https://docs.opencv.org/4.7.0/d1/dfb/intro.html
OpenCV's versatility and powerful set of tools make it a popular choice for a wide range of computer vision applications in various industries, including healthcare, automotive, security, entertainment, and more. It has a wide range of applications in today’s world, which include but not limited to:
Object detection and recognition: OpenCV can be used to detect and recognize objects in images and videos, allowing for applications such as security and surveillance systems.
Facial recognition: OpenCV has powerful facial recognition capabilities, which can be used in applications such as biometric authentication and identity verification.
Optical character recognition (OCR): OpenCV can be used to recognize text in images, making it a useful tool for applications such as document scanning and image-to-text conversion.
Video processing: OpenCV can be used for real-time video processing applications, such as video stabilization and object tracking.
Medical imaging: OpenCV can be used to process and analyze medical images, allowing for applications such as diagnosis and treatment planning.
Robotics: OpenCV can be used in robotics applications for tasks such as object detection and tracking, as well as navigation and mapping.
Augmented reality: OpenCV can be used to create augmented reality applications, such as virtual try-on applications for fashion and beauty products.
Python is a high-level programming language that has gained immense popularity in recent years, particularly in the data science and machine learning communities. It has a simple and easy-to-learn syntax, making it an ideal language for beginners, while also being a powerful tool for experienced developers. Python's popularity has led to the development of a vast range of libraries and frameworks, making it an excellent choice for a wide range of applications.
Python and OpenCV together form a powerful combination for computer vision projects. Python provides an easy-to-learn language that is great for prototyping and experimenting with ideas, while OpenCV provides a comprehensive set of tools for image and video processing. Python's integration with OpenCV makes it easy to write computer vision applications in a high-level language, allowing developers to quickly build and test their ideas.
Python and OpenCV are two essential tools for anyone interested in computer vision. Python's ease-of-use and flexibility, combined with OpenCV's powerful set of tools and algorithms, make it a go-to choice for many computer vision projects.
Then what you will learn from this book with OpenCV combined with Python?
Read, show and save images.
Read and show videos or webcam videos with the specific libraries.
User interaction such as keyboard or mouse operations.
Draw texts and shapes such as circles, rectangles, triangles, etc.
Detection of colors and shapes from images, such as circles, rectangles, triangles, etc.
Detection of faces, eyes and human from images or videos.
Text recognition in images.
Modify image quality or colors, e.g. blur, warp transform, blend, resize, adjust colors, etc.
Machine learning methods, including K-Means, K-Nearest Neighbors, Support Vector Machines, Artificial Neural Networks and Convolutional Neural Networks.
The benefits of using OpenCV,
Open source and free, easy and simple to learn.
Fast for processing, especially used for video processing, for example detect objects from videos.
Offers over 2,500 mathematical algorithms, they are efficient enough not only for image but also for video and real-time processing.
The algorithms and functions are designed to take advantage of hardware acceleration and multi-core systems.
1.2.
Target Audients of This Book
This book is written for anyone interested in learning computer vision using the Python programming language. The book is suitable for readers with any level of programming skills, from those with very limited knowledge of computer vision to the experienced ones, as it covers the basics of both Python programming and OpenCV from the ground up.
For beginners, the book begins with the step-by-step instructions for installation and setup of OpenCV, so readers can get started quickly and easily. It introduces Python from simplest statements to object-oriented classes, and begins with a hello world example. It also covers the fundamentals of computer vision, including basic image processing techniques such as pixels, color spaces with BGR and HSV, and conversion between them. It’s a plus if the readers have some basic level of programming skills with any languages, but don’t need to have previous experiences of Python and/or OpenCV.
For more experienced readers, the book covers more advanced topics such as object detection, facial recognition, as well as machine learning and deep learning with neural networks. The book includes numerous examples with practical and hands-on projects to help readers understand the concepts and techniques of OpenCV.
The book is also suitable for researchers, students, and professionals who want to expand their knowledge and skills in computer vision using OpenCV with Python. It provides a comprehensive guide for those who want to master the OpenCV library and use it for real-world applications.
In summary, this book is a valuable resource for anyone interested in computer vision and OpenCV, regardless of their programming skill level. The book covers a wide range of topics, from basic image processing techniques to advanced deep learning applications, making it an excellent choice for anyone looking to learn and apply OpenCV with Python.
1.3.
S
ource Codes for This Book
All source codes used in this book are tested and working as of the release of this edition. The source codes are available from Github at the below link: https://github.com/jchen8000/OpenCV
It’s suggested to use git to clone the source code, alternatively, you can also simply download the zip file from the above URL, and then extract the zip file to your local machine.
If you are not familiar with git, here is the quick get-started guide. Feel free to skip and go to the next section if you are familiar with it.
Download Git for Windows at https://git-scm.com/downloads
It has download links for Windows, macOS and Linux/Unix, simply click the one for your machine, and then download the latest version.
Launch the downloaded installer and follow the on-screen instructions to install it.
Git is a command line tool, open a cmd window, create a folder/directory say OpenCV, and clone the Github repository to the local machine by running the following command,
mkdir opencv
cd opencv
git clone https://github.com/jchen8000/OpenCV.git
The source code will be downloaded to the folder/directory.
Now the source codes are downloaded to the local machine, after the installations of Python and OpenCV are completed as described in Chapter 2, you will be able to use the PyCharm (an integrated development environment for Python) to open the source codes from this folder.
1.4.
Hardware Requirements and Software Versions
Below table shows the versions of OpenCV and Python as well as the libraries used in this book:
The hardware requirements for PyCharm to run Python and OpenCV:
Reference the PyCharm document for the details of system requirements at the below link:
https://www.jetbrains.com/help/pycharm/installation-guide.html#requirements
1.5.
How This Book Is Organized
In order for beginners to get started, this book begins with some basics such as step-by-step installation and setup, if you have experiences and already known how to do it, please feel free to skip the chapters or sections.
Chapter 2 is to go through the installation process for Python and OpenCV, it also has instructions on how to install Python libraries. PyCharm, an integrated development environment, is used throughout this book. In the later chapters, we will need to install some other libraries to perform actions such as machine learning and neural networks.
Chapter 3 is to introduce OpenCV basics such as loading and displaying images, videos and webcams, drawing text and shapes like lines, circles and rectangles, etc. The image fundamentals will also be introduced in this chapter, like pixels and color spaces of BGR and HSV.
Chapter 4 is to explain the user interactions like keyboard and mouse operations.
Chapter 5 is to introduce some common methods for image processing, such as color modification, blur, blend and warp images. The concept of the histogram is also introduced in this chapter, and how to use the histograms in image processing.
Chapter 6 is to introduce object detection, shape (circle, rectangle, etc.) detection, color detection, text recognition, human detection, face and eye detection etc. We also explain two common image processing practices by examples, removing and blurring the backgrounds.
Chapter 7 is to introduce the machine learning features provided by OpenCV, including several widely used machine learning methods like K-Means Clustering, K-Nearest Neighbors, Support Vector Machines, Artificial Neural Networks as well as Convolutional Neural Networks.
There are mathematical algorithms behind the scenes for image processing and machine learning, this book will not focus on those mathematical stuffs, instead we will focus on how to use them via OpenCV with Python, although sometimes we explain some algorithms in very simple terms, but not deep-dive into details.
It’s highly recommended to set up an environment on your PC to practice and execute the example source codes. There are many images throughout this book, which are the results of the execution of the codes. However, the images in this book are for reference only. You will be able to see the color images from the execution of the source codes on the environments of your PC.
All right, enjoy the wonderful world of Computer Vision!
2. Installation
This chapter will prepare for using OpenCV and Python. The first thing is to install Python and an Integrated Development Environment (IDE) if you do not have them already. After the installation is completed, need to install the OpenCV and other required packages on the IDE environment, and then we will create our first projects.
Python version 3.10 is used throughout this book, and PyCharm community edition is used as the Integrated Development Environment (IDE). Section 2.1 explains the details of installing Python and PyCharm.
For users who prefer Linux operating systems, the details of installation on Ubuntu will be explained in section 2.2.
Section 2.3 explains the installation of OpenCV and configuration of PyCharm, and a Hello OpenCV code to verify that the installation is successful.
Enjoy the installation of OpenCV and Python.
2.1.
Install on Windows
2.1.1.
Install Python on Windows 10
This section explains the installation of Python on Windows. Go to the Python website at https://www.python.org/downloads/, there is a list of available Python versions for download, look something like Figure 2.1, this is the available Python versions as of this writing, it could be different in the future when more are released.
Figure 2.1 List of Python Versions for Download
It’s not recommended to install the latest version, because a number of Python packages are used in this book, each package supports up to a specific Python version, some of them are not yet available to support the latest Python version. Here we use Python 3.10.x for this book.
Click Download from the webpage above and go to the next page, there is a list of available download files for different Operation Systems, which looks something similar to Figure 2.2:
Figure 2.2 List of Operation Systems for Python Installer
Download the one for your operation system, save and run the file. If you are using Windows and Mac OS, the installer files can be downloaded from here. If you are using Linux, we will explain how to install it on Ubuntu in the next section.
Run the downloaded installer file, it’s pretty much straightforward although it will take several minutes. After installation is completed successfully, verify the Python on your system. Start a cmd command by clicking on Start icon of Windows and type "cmd" and hit return.
Type the following command in the cmd window:
python –-version
The result should be shown something as below,
C:\>python --version
Python 3.10.5
The displayed is the version just installed, this indicates that the Python is correctly installed.
Please note, as long as the version is 3.10.x, it should be OK, it doesn’t have to match the last number after the second dot.
2.1.2.
PyCharm, the Integrated Development Environment
The next is to install PyCharm -- the Integrated Development Environment (IDE) for Python, which is used to write, edit, run and debug the Python codes. PyCharm is a very popular cross-platform Python IDE, it comes with rich features like integrated unit testing and Python debugger, error highlighting, code analysis and so on.
PyCharm has two versions: Community and Professional, the former is open-source and free for Python developers; the latter is paid edition and comes with more features. The free community version is good for this book.
Go to https://www.jetbrains.com/pycharm/download and click the Download button under Community. After the installer file is downloaded, run it and follow the on-screen instructions to install it.
It’s straightforward to execute the installation process until completion. After the installation is completed, the PyCharm is available on your Windows system.
2.2.
Install Python on Ubuntu
2.2.1.
Install Python on Ubuntu
This section explains the installation of Python on Linux system, specifically Ubuntu 22.04. As a prerequisite to installing Python, a root user or a user with sudo access is required.
Installing Python on Ubuntu with the apt tool is a simple and straightforward process, apt (Advanced Packaging Tool) is a command-line tool used in Ubuntu and other Debian-based Linux to manage packages, which are pre-compiled software packages that can be easily installed, updated, and removed on a Linux system. It provides a simple and easy-to-use interface for searching, installing, and removing packages. It also allows users to update the package list, upgrade the system to the latest, and manage software repositories.
Here is the step-by-step guide:
Start Ubuntu 22.04, login as a root user or a user with sudo privilege, and open a new Terminal window, run the following command to update and upgrade the system to ensure it’s up-to-date:
$ sudo apt update && sudo apt upgrade
Install the prerequisites for Python by executing the following command:
$ sudo apt install software-properties-common
Next, it’s recommended to install the deadsnakes PPA to the sources list, which makes it possible to install multiple Python versions side by side on the Ubuntu system. So instead of only having Python 3.10 on the system, Python 3.9, 3.8, or 3.7 can also be installed on the same system simultaneously. See more details about deadsnakes PPA at https://tooling.bennuttall.com/ deadsnakes/. Execute the following command to install it,
$ sudo add-apt-repository ppa:deadsnakes/ppa
When prompted press Enter to continue:
Press [ENTER] to continue or Ctrl-c to cancel adding it.
When completed, run the following command to update again to refresh the newly installed PPA, and then install Python 3.10:
$ sudo apt update
$ sudo apt install python3.10
After the above execution is completed successfully, Python 3.10 should be installed on the Ubuntu system and ready to use. Now verify it by typing the following command to show the version of installed Python:
$ python3.10 –version
The output is displayed below,
Python 3.10.5
It indicates the Python is successfully installed on the system.
2.2.2.
Install PyCharm on Ubuntu
PyCharm is a cross-platform tool and is also available on Linux. This section describes how to install it on the Ubuntu system.
It’s very simple and straightforward to install it by Ubuntu command line:
$ sudo snap install pycharm-community –classic
All done. After the installation process is completed, the PyCharm can be launched by the following command:
$ pycharm-community
2.3.
Configure PyCharm and Install OpenCV
2.3.1.
Create a New Python Project
Start PyCharm and Create a New Project.

Graphical user interface, text, application, email Description automatically generatedFigure 2.3 PyCharm -- Create a New Project
Explanations:
After the parameters are set correctly, click Create button at the bottom to create the project.
2.3.2.
Install and Upgrade OpenCV and Libraries
All libraries including OpenCV can be installed from within PyCharm after a project is created.
Select File -> Settings, the Settings screen is displayed,

A screenshot of a computer program Description automatically generated with medium confidenceFigure 2.4 PyCharm -- Project Settings
The project name OpenCV is the one we just created in the last step, so in the left-side navigation panel it shows Project: OpenCV as Figure 2.4. If you name the project differently, it will show "Project: [your project name]. Then select
Python Interpreter" under it.
In the right-side panel, the Python Interpreter should show the Python version we have installed in the previous section. If there are multiple Python versions on the system, they will appear in the dropdown list.
In the middle of the right-side panel in Figure 2.4, all packages are listed and available to this environment. If select different interpreters, if any, from dropdown list, a different set of available packages will appear in the package list area. The required packages can be installed from here.
There are +, − and triangle icons are shown at the top of the package list, as highlighted in