Building Computer Vision Applications Using Artificial Neural Networks, 2nd Edition Shamshad Ansari pdf download
Building Computer Vision Applications Using Artificial Neural Networks, 2nd Edition Shamshad Ansari pdf download
or textbooks at https://ebookmass.com
https://ebookmass.com/product/building-computer-vision-
applications-using-artificial-neural-networks-2nd-edition-
shamshad-ansari/
https://ebookmass.com/product/analysis-and-visualization-of-discrete-
data-using-neural-networks-koji-koyamada/
https://ebookmass.com/product/3-d-computer-vision-principles-
algorithms-and-applications-1st-edition-yu-jin-zhang/
https://ebookmass.com/product/accelerators-for-convolutional-neural-
networks-arslan-munir/
https://ebookmass.com/product/computer-vision-theory-algorithms-
practicalities-fifth-edition-davies/
AI Applications to Communications and Information
Technologies: The Role of Ultra Deep Neural Networks
Daniel Minoli
https://ebookmass.com/product/ai-applications-to-communications-and-
information-technologies-the-role-of-ultra-deep-neural-networks-
daniel-minoli/
https://ebookmass.com/product/computer-networks-global-edition-andrew-
s-tanenbaum/
https://ebookmass.com/product/probabilistic-graphical-models-for-
computer-vision-ji-q/
https://ebookmass.com/product/computer-vision-technology-for-food-
quality-evaluation-second-edition-sun/
https://ebookmass.com/product/computer-networks-and-internets-6th-
edition-ebook-pdf/
Building Computer Vision
Applications Using
Artificial Neural Networks
With Examples in OpenCV
and TensorFlow with Python
Second Edition
Shamshad Ansari
Building Computer Vision Applications Using Artificial Neural Networks: With
Examples in OpenCV and TensorFlow with Python, Second Edition
Shamshad Ansari
Centreville, VA, USA
Acknowledgments��������������������������������������������������������������������������������������������������xix
Introduction������������������������������������������������������������������������������������������������������������xxi
v
Table of Contents
Additional Libraries��������������������������������������������������������������������������������������������������������������������� 12
Installing SciPy���������������������������������������������������������������������������������������������������������������������� 12
Installing Matplotlib��������������������������������������������������������������������������������������������������������������� 12
Summary������������������������������������������������������������������������������������������������������������������������������� 12
vi
Table of Contents
vii
Table of Contents
GLCM����������������������������������������������������������������������������������������������������������������������������������� 140
HOGs������������������������������������������������������������������������������������������������������������������������������������ 145
LBP�������������������������������������������������������������������������������������������������������������������������������������� 152
Feature Selection���������������������������������������������������������������������������������������������������������������������� 159
Filter Method����������������������������������������������������������������������������������������������������������������������� 160
Wrapper Method������������������������������������������������������������������������������������������������������������������ 160
Embedded Method�������������������������������������������������������������������������������������������������������������� 161
Model Training��������������������������������������������������������������������������������������������������������������������������� 162
How to Do Machine Learning���������������������������������������������������������������������������������������������� 162
Supervised Learning������������������������������������������������������������������������������������������������������������ 163
Unsupervised Learning�������������������������������������������������������������������������������������������������������� 164
Model Deployment�������������������������������������������������������������������������������������������������������������������� 165
Summary���������������������������������������������������������������������������������������������������������������������������������� 167
viii
Table of Contents
Our First Computer Vision Model with Deep Learning: Classification of Handwritten Digits���� 207
Model Overview������������������������������������������������������������������������������������������������������������������������������������������������ 207
Model Implementation��������������������������������������������������������������������������������������������������������� 208
Model Evaluation����������������������������������������������������������������������������������������������������������������������� 217
Overfitting���������������������������������������������������������������������������������������������������������������������������� 217
Underfitting�������������������������������������������������������������������������������������������������������������������������� 219
Hyperparameters���������������������������������������������������������������������������������������������������������������������� 223
TensorBoard������������������������������������������������������������������������������������������������������������������������ 224
Experiments for Hyperparameter Tuning����������������������������������������������������������������������������� 225
Saving and Restoring Model����������������������������������������������������������������������������������������������������� 229
Save Model Checkpoints During Training���������������������������������������������������������������������������� 230
Manually Save Weights������������������������������������������������������������������������������������������������������� 233
Load the Saved Weights and Retrain the Model������������������������������������������������������������������ 233
Saving the Entire Model������������������������������������������������������������������������������������������������������ 234
Retraining the Existing Model���������������������������������������������������������������������������������������������� 234
Using a Trained Model in Applications��������������������������������������������������������������������������������� 235
Convolutional Neural Network�������������������������������������������������������������������������������������������������� 236
Architecture of CNN������������������������������������������������������������������������������������������������������������� 236
How a CNN Works���������������������������������������������������������������������������������������������������������������� 238
Summary of CNN Concepts������������������������������������������������������������������������������������������������� 243
Training a CNN Model: Pneumonia Detection from Chest X-rays���������������������������������������� 244
Examples of Popular CNNs�������������������������������������������������������������������������������������������������� 255
Summary����������������������������������������������������������������������������������������������������������������������������� 260
ix
Table of Contents
x
Table of Contents
xi
Table of Contents
Dataset�������������������������������������������������������������������������������������������������������������������������������� 410
Downloading VGGFace2 Data���������������������������������������������������������������������������������������������� 412
Data Preparation������������������������������������������������������������������������������������������������������������������ 414
Model Training��������������������������������������������������������������������������������������������������������������������� 419
Evaluation���������������������������������������������������������������������������������������������������������������������������� 420
Developing a Real-Time Face Recognition System������������������������������������������������������������������� 421
Face Detection Model���������������������������������������������������������������������������������������������������������� 421
Classifier for Face Recognition�������������������������������������������������������������������������������������������� 422
Summary���������������������������������������������������������������������������������������������������������������������������������� 428
xii
Table of Contents
Index��������������������������������������������������������������������������������������������������������������������� 513
xiii
About the Author
Shamshad Ansari is an author, inventor, and thought
leader in the fields of computer vision, machine learning,
artificial intelligence, and cognitive science. He has
extensive experience in high-scale, distributed, and parallel
computing. Sam currently serves as an adjunct professor at
George Mason University, teaching graduate-level programs
within the Data Analytics Engineering department of the
Volgenau School of Engineering. His areas of instruction
encompass machine learning, natural language processing,
and computer vision, where he imparts his knowledge and
expertise to aspiring professionals.
Having authored multiple publications on topics such as machine learning, RFID,
and high-scale enterprise computing, Sam’s contributions extend beyond academia. He
holds four US patents related to healthcare AI, showcasing his innovative mindset and
practical application of technology.
Throughout his extensive 20+ years of experience in enterprise software
development, Sam has been involved with several tech startups and early-stage
companies. He has played pivotal roles in building and expanding tech teams from
the ground up, contributing to their eventual acquisition by larger organizations.
At the beginning of his career, he worked with esteemed institutions such as the US
Department of Defense (DOD) and IBM, honing his skills and knowledge in the
industry.
Currently, Sam serves as the president and CEO of Accure, Inc., an AI company
that he founded. He is the creator and architect of, and continues to make significant
contributions to, Momentum AI, a no-code platform that encompasses data
engineering, machine learning, AI, MLOps, data warehousing, and business intelligence.
Throughout his career, Sam has made notable contributions in various domains
including healthcare, retail, supply chain, banking and finance, and manufacturing.
xv
About the Author
xvi
About the Technical Reviewers
Bharath Kumar Bolla has over 12 years of experience
and is a senior data scientist at Salesforce, Hyderabad.
Bharath obtained a master’s degree in data science from
the University of Arizona and a master’s degree in life
sciences from Mississippi State University. Bharath worked
as a research scientist for seven years at the University of
Georgia, Emory University, and Eurofins LLC. At Verizon,
Bharath led a team to build a “Smart Pricing” solution, and
at Happiest Minds, he worked on AI-based digital marketing
products. Along with his day-to-day responsibilities, Bharath is a mentor and an active
researcher with more than 20 publications in conferences and journals. Bharath received
the “40 Under 40 Data Scientists 2021” award from Analytics India Magazine for his
accomplishments.
xvii
Acknowledgments
I wrote this book with two primary objectives in mind. First, I aimed to comprehensively
construct the foundations of computer vision concepts, starting from the basics and
progressing to an advanced level. Secondly, I aspired to furnish a practical guide for
the application of these concepts in real-world computer vision systems by including
practical use cases and code examples.
Achieving these objectives required meticulous organization of topics, integration
of content with meaningful and practical use cases, and thorough testing of the code,
all of which demanded my undivided attention throughout the writing process. This
accomplishment wouldn’t have been possible without the unwavering support of my
family. My gratitude is immeasurable towards my wife, Shazia, who managed the care of
our two daughters (Erum and Dua), ensuring they remained engaged while I dedicated
myself to the writing of this book. She ingeniously transformed this experience into a
positive experience for both them and myself. Remarkably, my children began tracking
my progress, celebrating each completion of a section, subsection, or chapter. Their
enthusiasm became a wellspring of energy and motivation that significantly enriched my
writing experience. I still do not know what magic my wife used to do this.
My life is indebted to Anumati Bhagi and Ashok Bhagi, who are no less than parents
to me; their love and support always motivate me.
This book is a collection of my lifetime experiences that I gained by working with
some of the greatest engineers, data scientists, and business professionals. I would
like to thank all my colleagues at Accure and all the past companies I have worked at. I
sincerely thank all my teachers, professors, and mentors who enlightened me with their
knowledge and wisdom.
Working with the Apress editorial team, which includes remarkable individuals like
Mark Powers and Laura Berendson, has been an incredible experience. I want to extend
a special thank you to Shonmirin PA, the project coordinator, who has been instrumental
in coordinating with the editorial team and technical reviewers and providing prompt
and helpful responses to my questions along the way.
xix
Acknowledgments
xx
Introduction
For over 20 years, I have had the privilege of working with distinguished data scientists
and computer vision experts, some of the finest in their field. Throughout this journey,
I’ve gained invaluable insights, particularly in the realm of building scalable computer
vision systems following the best practices. This book encapsulates the wisdom acquired
from my work experience, as well as the collective knowledge amassed from the
exceptional individuals with whom I’ve had the fortune to work or collaborate. This book
also presents knowledge I’ve gained from the works of some of the greatest contributors
and thought leaders of computer vision. I have provided references to their work at
appropriate places throughout the book.
When I hire new engineers and scientists, one of my main challenges has been giving
them effective training so that they can quickly start contributing to the development
of computer vision systems. With numerous online resources and books available on
various computer vision topics, it’s easy to feel overwhelmed by the sheer amount of
information. This is especially true since the field of computer vision is extensive and
intricate.
In this book, my aim is to offer a structured and methodical approach to learning
computer visions systems. You will learn essential concepts first and then build on
those concepts by working through practical code examples that pertain to real-world
computer vision systems. This approach will help you connect the dots as you progress
through the chapters. I’ve structured this book to be as hands-on and practical as
possible to assist you effectively.
This book begins by explaining the core concepts of computer vision. It presents
code examples to help you understand these ideas better. In the beginning, the examples
mainly use OpenCV with the Python language.
This book also covers the basic concepts of machine learning and then gradually
moves into more complex concepts like artificial neural networks and deep learning.
Every concept is reinforced with one or more practical code examples that show how
the concept is applied in practice. Machine learning–related concepts are illustrated via
code examples in TensorFlow with Python.
xxi
Introduction
This book presents 11 examples, including working code, that demonstrate how
computer vision is used in real life. These examples are from different domains like
healthcare, surveillance, security, and manufacturing. To help you understand the code,
I explain each line step by step.
Chapters 7, 8, and 9 are dedicated to building practical computer vision–based
systems. These chapters show you all stages of how to create vision systems, from
obtaining pictures or videos, to building a data pipeline, to training a model, and finally
deploying the model for real situations.
Training state-of-the-art computer vision models requires a lot of hardware
resources. It is desirable and economically beneficial to train computer vision models on
a cloud infrastructure to leverage the latest hardware resources, such as GPUs, and pay-
as-you-go cost models. The final chapter, Chapter 10, provides step-by-step instructions
for building machine learning–based computer vision applications on three popular
cloud infrastructures: Google Cloud Platform, Amazon AWS, and Microsoft Azure.
Even though this book starts from explaining a single pixel of an image and goes up
to teaching how to use cloud computers for neural networks training, there are certain
prerequisites to better understand the concepts presented in this book. You should
already know how to use the Python programming language.
The purpose of this book is to assist working professionals, programmers, data
scientists, and even students (undergraduate and graduate) acquire hands-on skills for
building computer vision applications using artificial neural networks.
xxii
CHAPTER 1
Prerequisites and
Software Installation
This book is a practical guide that explores the process of building computer vision
applications using the Python programming language. Throughout this book, you will
gain a comprehensive understanding of leveraging OpenCV for image manipulation and
harnessing the power of TensorFlow to build machine learning models.
OpenCV, originally developed by Intel and written in C++, is an open source
computer vision and machine learning library consisting of more than 2,500 optimized
algorithms for working with images and videos. TensorFlow is an open source
framework for high-performance numerical computation and large-scale machine
learning. It is written in C++ and provides native support for both CPUs and GPUs.
Python is the most widely used programming language for developing machine learning
applications. Both TensorFlow and OpenCV provide Python interfaces to access their
low-level functionality. While both TensorFlow and OpenCV also offer interfaces for
other programming languages like Java, C++, and MATLAB, we will focus on the use of
Python in this book. Python’s vast community support and intuitive syntax make it an
accessible language for learners and practitioners alike.
The prerequisites for this book are practical knowledge of Python and familiarity
with NumPy and pandas. The book assumes that you are familiar with built-in data
containers in Python, such as dictionaries, lists, sets, and tuples. If you need to brush up
on these prerequisites, the following resources can be helpful:
• Python: https://www.w3schools.com/python/
• pandas: https://pandas.pydata.org/docs/getting_started/
index.html
• NumPy: https://numpy.org/devdocs/user/quickstart.html
1
© Shamshad Ansari 2023
S. Ansari, Building Computer Vision Applications Using Artificial Neural Networks,
https://doi.org/10.1007/978-1-4842-9866-4_1
Chapter 1 Prerequisites and Software Installation
The first order of business is to prepare your working environment for the exercises
presented throughout this book. You will begin by downloading and installing the
necessary software libraries and packages.
$ python3 --version
The output of this command should be something like this: Python 3.8.10.
To check the version number of PIP, execute the following command on your
terminal:
$ pip3 --version
This command should show a version number of PIP 3, for example, PIP 20.0.2.
2
Chapter 1 Prerequisites and Software Installation
brew update
brew install python
Make sure long paths are enabled on Windows. Here are the instructions to
do that: https://www.thewindowsclub.com/how-toenable-or-disable-win
32-long-paths-in-windows-11-10.
Install the 64-bit Python 3 release for Windows from https://www.python.org/
downloads/windows/ (select PIP as an optional feature).
If these installation instructions do not work in your situation, refer to the official
Python documentation at https://www.python.org/.
3
Chapter 1 Prerequisites and Software Installation
virtualenv
virtualenv is a tool designed to create isolated Python environments. When you use
virtualenv, it generates a directory that includes all the necessary executables for utilizing
the packages required by a Python project. virtualenv provides the following advantages:
Next, we will install virtualenv and set up the virtual environment with all the
necessary software. The rest of the book assumes that the dependencies for our
reference program will be contained in this virtualenv.
After the installation is complete, create a directory of your choice where you intend
to set up the virtualenv. For purposes of following the examples in this book, name this
directory cv (short for “computer vision”):
$ mkdir cv
4
Chapter 1 Prerequisites and Software Installation
The following is a sample output from running this command (on my MacBook):
When virtualenv is active, your shell prompt is prefixed with (cv). Here’s an example:
Installing packages within a virtual environment does not affect the host system
setup. Start by upgrading PIP as follows. Please note that you should not execute any
command as root or sudo while inside the virtual environment.
When you have finished your programming activities and you want to exit from
virtualenv, run the following:
$ deactivate
TensorFlow
TensorFlow is a widely used open source library for numerical computation and
large-scale machine learning. In the upcoming chapters, you will delve deeper into
TensorFlow and explore its capabilities. However, before we proceed, let’s begin by
installing TensorFlow and preparing it for our deep learning exercises. If you have a
GPU in your computer and you want to utilize it for deep learning tasks, you typically
don’t need to install both CPU and GPU versions of TensorFlow. You can install the GPU
version, which includes support for both GPU and CPU computation. TensorFlow with
GPU support automatically falls back to CPU execution when GPU resources are not
5
Chapter 1 Prerequisites and Software Installation
available or when the operations are not suitable for GPU acceleration. So, installing
TensorFlow with GPU support (tensorflow-gpu) should be sufficient. It will work on both
GPU and CPU, making it a versatile choice for most deep learning tasks.
$ sh Downloads/Miniconda3-latest-MacOSX-arm64.sh
$ conda activate cv
$ conda deactivate
7. Activate the virtual environment by running the command conda
activate cv, and then install TensorFlow by executing the
following commands:
6
Chapter 1 Prerequisites and Software Installation
10. If you encounter any errors, resolve incompatibility issues by, for
example, upgrading the Mac OS.
If TensorFlow is successfully installed, the output should not show any errors.
1. Ensure that you have the appropriate NVIDIA GPU and drivers
installed on your system. You can check the TensorFlow
documentation for the specific GPU requirements and compatible
driver versions.
7
Chapter 1 Prerequisites and Software Installation
PyCharm IDE
For consistency, the integrated development environment (IDE) used throughout this
book for writing and managing Python code is the community version of PyCharm, a
popular Python IDE. You can choose to use your preferred IDE, but if you do so, you’ll
need to adapt relevant examples and exercises to your IDE.
Installing PyCharm
To acquire PyCharm from its official website, follow these steps:
1. Visit the PyCharm website at https://www.jetbrains.com/
pycharm/download/.
Here are the direct download links for different operating systems:
• Linux: https://www.jetbrains.com/pycharm/download/download-
thanks.html?platform=linux&code=PCC
• Mac: https://www.jetbrains.com/pycharm/download/download-
thanks.html?platform=mac&code=PCC
• Windows: https://www.jetbrains.com/pycharm/download/
download-thanks.html?platform=windows&code=PCC
Please note that these links may change over time, so it’s recommended to visit the
official PyCharm website to ensure you access the most up-to-date download links.
9
Chapter 1 Prerequisites and Software Installation
Optionally, you can select the checkbox “Make available to all projects” if you want
this interpreter to be accessible for all your projects in PyCharm.
By following these steps, you will configure PyCharm to use the virtual environment
cv as the Python interpreter for your project.
OpenCV
OpenCV is one of the most popular and widely used libraries for image processing. All
code examples in this book are based on OpenCV 4. Therefore, the installation steps
presented in this section are for version 4 of OpenCV.
10
Chapter 1 Prerequisites and Software Installation
$ source cv/bin/activate
Once the installation is complete, you can verify it by importing OpenCV in your
Python command:
If there are no errors, it indicates that OpenCV has been successfully installed.
11
Chapter 1 Prerequisites and Software Installation
Note Package names for OpenCV may vary slightly based on your specific
operating system or Python version. If you encounter any issues during the
installation, refer to the official OpenCV documentation or the PyPI page for the
most accurate and up-to-date installation instructions.
Additional Libraries
SciPy and Matplotlib are two additional libraries that we will need as we work on some
of the examples. Let’s install and keep them in our virtualenv.
Installing SciPy
Install SciPy with the following:
Installing Matplotlib
Install Matplotlib with the following:
Summary
The following table summarizes the installation commands for the required libraries
within your virtualenv. You can copy and paste these commands into your terminal to
install the libraries.
12
Chapter 1 Prerequisites and Software Installation
Ensure that your virtualenv is activated before running these commands. Once
you’ve installed these libraries, you’ll have all the necessary dependencies within your
virtualenv to work with the examples in the book.
13
CHAPTER 2
• To learn how pixels are organized in an image and how to access and
manipulate them
Image Processing
Image processing is a method used to manipulate a digital image to obtain an improved
image or extract valuable information from it. It involves taking an image as input
and producing as output either another image or specific characteristics and features
associated with that image. Videos, being composed of a series of images or frames, can
also be processed using image processing techniques. In this chapter, we will delve into
the fundamental concepts of digital image processing. Additionally, you will acquire the
basic skills to work with images and write code to manipulate them.
15
© Shamshad Ansari 2023
S. Ansari, Building Computer Vision Applications Using Artificial Neural Networks,
https://doi.org/10.1007/978-1-4842-9866-4_2
Chapter 2 Core Concepts of Image and Video Processing
Image Basics
A digital image is an electronic representation of an object/scene or scanned document.
The digitalization of an image means converting it into a series of numbers and storing
these numbers in a computer storage system. Understanding how these numbers are
arranged and how to manipulate them is the primary objective of this chapter. This
chapter explains the fundamental components that make up an image and guides you
through the process of manipulating images using OpenCV and Python.
Pixels
An image can be visualized as a collection of dots arranged in rows and columns, where
each dot represents a pixel with a specific color. These pixels are assigned numerical
values, which determine their respective colors. You can imagine an image as a grid
of square cells, where each cell corresponds to a single pixel with a specific color. For
instance, if we have a 300×400-pixel image, it implies that the image is structured as a
grid with 300 rows and 400 columns, resulting in a total of 120,000 pixels.
Pixel Color
A pixel can be represented in two different ways: grayscale and color.
Grayscale
In a grayscale image, each pixel takes a value between 0 and 255. The value 0 represents
black, and 255 represents white. The values in between are varying shades of gray. The
values close to 0 are darker shades of gray, and values closer to 255 are brighter shades
of gray.
Color
The RGB (which stands for Red, Blue, and Green) color model is one of the most popular
color representations of a pixel. Other color models are available, but this book primarily
focuses on the RGB color model.
16
Chapter 2 Core Concepts of Image and Video Processing
In the RGB model, each pixel is represented as a tuple of three values, generally
represented as follows: (value for red component, value for green component, value for
blue component). Each of the three colors is represented by integers ranging from 0 to
255. Here are some examples:
The W3Schools website is a great place to play with different combinations of RGB
tuples to explore more patterns (https://www.w3schools.com/colors/colors_rgb.asp).
Use the RGB Calculator to explore what color is represented by each of the following tuples:
(0,0,255)
(255,255,255)
(0,0,128)
(128,0,128)
(128,128,0)
Now try to make the color yellow. Here is a clue: red and green make yellow. That
means a pure red (255), a pure green (255), and no blue (0) will make yellow. So, the RGB
tuple for yellow is (255,255,0).
Now that you have a good understanding of pixels and their color, let’s explore how
pixels are arranged in an image and how to access them. The following section discusses
the concept of coordinate systems in image processing.
Coordinate Systems
As previously mentioned, pixels in an image are arranged in the form of a grid that is
made of rows and columns. Imagine a square grid of eight rows and eight columns. This
forms an 8×8 or 64-pixel image. Think of it as a 2D coordinate system in which the origin
(0,0) is the top-left corner. Figure 2-1 shows our example 8×8-pixel image.
17
Chapter 2 Core Concepts of Image and Video Processing
With the top-left corner as the start or origin of the image coordinate system, the
pixel at the top-right corner is represented by (0,7), the bottom-left pixel is (7,0), and the
bottom-right pixel is (7,7). This may be generalized as (x,y), where x is the position of the
cell from the left edge of the image and y is the vertical position down from the top edge
of the image. In Figure 2-1, the red pixel is in the fifth position from the left and fourth
from the top. Since the coordinate system begins at 0, the coordinate of the red pixel
shown in Figure 2-1 is (4,3).
To make this concept a little clearer, consider Figure 2-2, an image that is 8×8 pixels,
with the letter H written on it. Also, for simplicity, assume this is a grayscale image, with
the letter H written in black and the rest of the area of the image in white.
18
Chapter 2 Core Concepts of Image and Video Processing
So, what’s the value of the pixel at position (1,4)? And at position (2,2)?
Now, you should have a clear understanding of how images are represented as
numbers arranged in a grid. These numerical values are serialized and stored in a
computer’s storage system. When the image is displayed on the screen, these numbers
are interpreted and rendered as an actual image.
At this point, you have learned how to access individual pixels using the coordinate
system, which allows you to pinpoint specific locations within the image grid.
Additionally, you have learned how to assign colors to these pixels by manipulating their
numerical values. By grasping these concepts, you now understand how images are
structured and how to interact with them programmatically.
With a solid foundation and understanding of image representation, you are now
ready to gain practical experience through Python and OpenCV coding. In the following
section, I will guide you step-by-step through the process of writing code that loads
images from the computer’s disk, accesses individual pixels, performs manipulations,
and saves the modified images back to the disk.
Let’s delve into the exciting world of image processing and begin our hands-on journey!
19
Chapter 2 Core Concepts of Image and Video Processing
• For a grayscale image, the returned value from NumPy will be a single
value between 0 and 255.
• For a color image, the returned value from NumPy will be a tuple for
red, green, and blue. Note that OpenCV maintains the RGB sequence
in the reverse order. In other words, OpenCV stores the colors in BGR
sequence, not in RGB sequence. Remember this important feature of
OpenCV to avoid any confusion while working with OpenCV.
Before we write any code, let’s make sure we always use our virtual environment, in
the ~/cv directory, that we already set up with PyCharm in Chapter 1.
Launch your PyCharm IDE and make a new project (I named my project cviz, short
for “computer vision”). Refer to Figure 2-4 and ensure that you have selected Existing
Interpreter and have our virtualenv Python 3.8 (cv) selected.
20
Chapter 2 Core Concepts of Image and Video Processing
Figure 2-4. PyCharm IDE, showing the setup of the project with virtualenv
Filename: Listing_2_1.py
1 import cv2
2
3 # image path
4 image_path = "images/marsrover.png"
5
21
Chapter 2 Core Concepts of Image and Video Processing
22
Chapter 2 Core Concepts of Image and Video Processing
Note The function waitKey() waits for a key event infinitely or for a certain
delay in milliseconds. Since the OS has a minimum time between switching
threads, the waitKey() function will not wait, after a key is pressed, for exactly
the delay time passed as an argument to the waitKey() function. The actual wait
time depends on other programs that your computer might be running at the time
when a key is pressed and waitKey() function is called.
The NumPy array representing the image has three dimensions: height × width ×
channel. The first element of the array corresponds to the height, indicating the number of
rows in the image pixel grid. Similarly, the second element represents the width, indicating
the number of columns in the grid. The three channels represent the color components in
the order of blue, green, and red (BGR), rather than the conventional RGB. The overall size
of the array is 400×640×3 = 768,000. This means that the image contains a total of 400×640
= 256,000 pixels, with each pixel consisting of three color values.
23
Chapter 2 Core Concepts of Image and Video Processing
As shown in Figure 2-6, the modified image has a 100×100-pixel square at the top-left
corner in aqua, represented by (255,255,0) of the BGR scheme.
Drawing
OpenCV provides convenient methods to draw various shapes on an image. In the
following sections, we will explore how to draw a line, a rectangle, and a circle using the
corresponding methods provided by OpenCV:
• Line: line()
• Rectangle: rectangle()
• Circle: circle()
These methods allow us to easily add these shapes to an image, enhancing our
ability to annotate and visualize data.
Let’s proceed with the implementation and explore how each step is carried out in
code. Listing 2-3 demonstrates how to draw a line on an image.
Filename: Listing_2_3.py
1 import cv2
2
3 # image path
4 image_path = "images/marsrover.png"
5 # Read or load image from its path
6 image = cv2.imread(image_path)
7
8 # set start and end coordinates
9 start = (0, 0)
10 end = (image.shape[1], image.shape[0])
11 # set the color in BGR
12 color = (255,0,0)
13 # set thickness in pixel
14 thickness = 4
15 cv2.line(image, start, end, color, thickness)
16
17 # display the modified image
18 cv2.imshow("Modified Image", image)
19 cv2.waitKey(0)
• Line 9: We set the starting coordinates of the point from where the
line will be drawn. Recall that the location (0,0) is the top-left corner
of the image.
• Line 10: We set the coordinates of the endpoint of the image. Notice
that the expression (image.shape[1], image.shape[0]) represents
the coordinates of the bottom-right corner of the image.
26
Other documents randomly have
different content
She little knew why Jerry valued them, and what a quantity of
wealth had been concealed in the soiled garments.
With trembling fingers, and features working with agitation, Jerry
took the clothes, and began to feel for his treasures. Alas for the old
man! His worst fears were realized. The bank books and certificate
of stock had been removed. Not a trace of them was to be found.
The poor man, for he was to be pitied, uttered a sharp cry of
anguish. The clothes dropped from his nerveless hands, and he fell
back on the bed as if stricken with a mortal wound.
“Help! Help! Police!” he ejaculated. “I’ve been robbed.”
“Robbed is it?” inquired Mrs. Hogan, puzzled. “And of what have you
been robbed, ould man?”
“There were two savings bank books in the pockets. You’ve taken
them!”
“Well, well, if I ever heard the loike!” exclaimed Mrs. Hogan,
indignantly. “So you call me a thafe, do you?”
“Give them back to me!” said the old man, imploringly. “I—I am so
poor. It will kill me if I lose my money.”
“Two savings bank books, indade!” said Mrs. Hogan. “It’s my belafe
you’re an ould humbug, you that have always called yourself so
poor. And how much money was there in them?” she asked.
“I—I—no matter. Give me the books, or I’ll send for the police.”
“Go and welcome, this minute, if you please. You ought to know
better than to call an honest woman a thafe.”
“Somebody has taken the books,” wailed Jerry.
“Very likely Paul’s taken care of them for you. He was here alone
with the clothes.”
“Where is Paul?” demanded Jerry, with peevish eagerness.
“He’s at his work, but I’m expecting him back every minute. If he
has taken the books, they are all right. Paul’s an honest boy, and a
fine boy.”
“Do you think he would rob me, Mrs. Hogan?” asked Jerry, piteously.
“It’s a shame to be askin’ such a question,” said Mrs. Hogan. “Shure
you know Paul wouldn’t demane himself by such an act. But here he
is to answer for himself.”
She heard Paul’s step on the stairs, as he came up whistling. Directly
afterwards he entered the room.
“What’s the matter?” he asked, looking from one to the other.
“The ould man’s in a great taking about bein’ robbed, Paul,”
answered Mrs. Hogan. “He says some savings bank books have been
taken from his clothes.”
“Where are they, Paul?” wailed the old man.
“They are safe, Jerry. I took them from your pockets, and the
railroad shares, too, and have left them with a Safe Deposit
Company, for safe keeping.”
“Are you sure they are safe, Paul?”
“Yes, much safer than they would be here. Of course Mrs. Hogan is
honest, but other persons might come into the room.”
“Bring them back to me, Paul. I want to see them.”
“I can if you insist upon it, Jerry. But you are too sick to look after
them. Besides, suppose your son should come in some day. He could
rob you easily, and you would never see your property again. Shall I
show you the receipt for them?”
“Yes.”
Paul drew from his pocket the receipt given him by the Safe Deposit
Company, and displayed it to the old man.
“Let me keep this?” said Jerry.
“Yes, if you like.”
This seemed to satisfy him, and he clutched the paper with a deep
sigh of relief.
“Shure, and you’ve calmed him down, Paul,” said Mrs. Hogan. “Was
it really true about the books?”
“It was really true, Mrs. Hogan. He has a good deal of money,
though he doesn’t look it.”
“Thin it’s a fool he is to live as he does, whin he might live like a
gintleman.”
“He will never live differently. When he dies it will go to his son, who
will get through with it in short order.”
Then Mrs. Hogan told Paul about the visit of Ellen Barclay, and her
discovering a relation in old Jerry.
“I wish she might have some of the money. Shure, she nades it with
her young children.”
“I would much rather she would have it than his son, who is a
ruffian and a burglar.”
“And you’ll help her to it, if you can, Paul?”
“Yes, I will. I don’t want Jerry’s money myself. I am young, and my
prospects are good. All I want is that it should go into the right
hands.”
CHAPTER XXVIII.
JERRY FINDS A NEW RELATION.
“There,” said Mrs. Holbrook, after reading the letter to Paul, “I think
this will secure you an entrance into my aunt’s house, and should it
be read by the housekeeper, as is likely to be the case, it will tell her
nothing. Now we will go out and see about getting you a supply of
clothes.”
CHAPTER XXX.
PAUL’S RECEPTION AT ROCKVILLE.
The housekeeper held the door half way open, and evidently meant
to prevent Paul from entering. If he had been less determined, or
more easily abashed, he would have given up his purpose. But
Number 91 had a will of his own.
“I don’t accept a dismissal from you, madam,” he said, eying Mrs.
Mercer with a steady glance. “I demand to see Mrs. Granville.”
“Hoity toity! Who are you, I’d like to know?” demanded the
housekeeper, amazed and exasperated.
“A visitor to Mrs. Granville,” answered Paul; “you, I suppose, are a
servant.”
“Do you dare to call me a servant, you impudent boy?” answered
Mrs. Mercer, raising her voice.
“What are you, then?” asked Paul, calmly.
“I am Mrs. Mercer, the housekeeper, I’d have you to know.”
“So I suppose, and Mrs. Granville is your employer. By what right do
you dare to send away her visitors?”
Mrs. Mercer was quite unused to being defied so boldly, and she
could hardly express her indignation.
“Well, I never heard of such impudence!” she ejaculated.
In her anger she would have closed the door, but just at that
moment a voice was heard from the floor above—the voice of Mrs.
Granville, who had been attracted by the housekeeper’s loud tones.
“What is all this, Mrs. Mercer?” she asked, in a tone of authority.
“It’s a boy who wants to force his way in, ma’am,” said Mrs. Mercer,
in a sulky tone.
“What boy is it?” asked her mistress.
Paul answered this question himself.
“I come from New York with a letter from your niece, Mrs. Holbrook,”
he said.
“Then come in at once,” said the old lady, in an imperative tone.
“Why did you not admit him at once?”
“I didn’t know,” answered the housekeeper, confused.
“I beg your pardon,” said Paul, “I told you this, and you said Mrs.
Granville had a headache, and would not see me.”
“What do you mean by this misrepresentation, Mrs. Mercer?”
demanded the old lady, sternly.
“I offered to take the letter up to you,” said Mrs. Mercer, a little
alarmed at the evident anger of her mistress.
“When Mrs. Granville reads the letter she will understand why I
preferred to hand it to her myself.”
“Why did you say I had a headache? Why do you presume to send
away my visitors, Mrs. Mercer?” demanded the old lady, thoroughly
aroused.
“I meant it for the best,” said the woman, sulkily.
“Never presume so far again. Now, young man, come up and let me
see you.”
Paul passed the discomfited housekeeper, and, going upstairs,
followed the old lady into a pleasant sitting room.
“I am sorry to have been the means of disturbing you,” said Paul,
politely. “Mrs. Holbrook sends you this letter, and wishes me to give
you her love.”
“You are a nice looking boy,” said the old lady, letting her glance rest
approvingly on Number 91. “I hope Louisa is well.”
“Very well, thank you.”
She put on her glasses, and read the letter.
“So you are Paul Parton,” she said, as she folded up the letter.
“Yes, madam.”
“A New York boy?”
“Yes, madam.”
“I am afraid you will find it dull here.”
“O no, Mrs. Granville, I am sure I shall like the country, for a while at
least.”
“Do you know how to drive?”
“Oh, yes; I am used to horses.”
“On the whole, I am glad Louisa sent you down here. Frost Mercer,
who usually drives me, has been getting lazy of late, and makes
excuses when I want to go out.”
“I won’t do that, Mrs. Granville. I shall be only too glad to go.”
“You are sure you won’t upset me?”
“I don’t believe I shall,” answered Paul, smiling. “I don’t care about
being upset myself.”
“I think, on the whole, I shall like to have a boy about,” said the old
lady, thoughtfully.
“I am afraid Mrs. Mercer might object to me.”
“I am the mistress of the house. Mrs. Mercer is only the
housekeeper,” said the old lady, with an unwonted assumption of
dignity. “Please ring the bell, Paul.”
Paul did so.
Presently the housekeeper entered in answer to the summons. She
looked askance at Paul.
“Mrs. Mercer,” said her mistress, “you will prepare a room for this
young gentleman. He will remain here, for the present.”
Mrs. Mercer looked disagreeably surprised.
“Perhaps you had better read this letter from my niece, Mrs.
Holbrook, as it will save explanations.”
The housekeeper read the letter, and her thin lips tightened with
displeasure.
“There is no need of your getting a boy to drive you around, ma’am,”
she said. “My son is perfectly able to do it.”
“Your son is very apt to be engaged when I want to drive out,”
returned the old lady, dryly.
“I will see that it don’t happen again,” said the housekeeper, anxious
to keep Paul out of the house.
“There is no occasion for that. Mind, I don’t blame your son. Paul
here will have nothing else to do, and can drive me as well as not.
Besides, he will read to me, and spare my eyes.”
“Frost would be willing to read to you.”
Mrs. Granville smiled.
“I don’t think reading aloud is one of your son’s accomplishments,”
she said. “His voice is not exactly musical.”
This was certainly true, for Frost Mercer had a voice deep and
croaking, like a frog’s.
“I am sorry you are so prejudiced against my poor boy,” said Mrs.
Mercer, mortified and displeased.
“You are a foolish woman, Mrs. Mercer. I am only going to make it
easier for Frost, and give this young gentleman something to do.
Paul, you may go with Mrs. Mercer, and take possession of your
room. Go where you please, till half past twelve, our dinner hour.
After dinner, I may take a drive.”
“You’d better get your life insured, then, ma’am,” muttered Mrs.
Mercer. “Boys ain’t to be trusted with horses.”
“Is your horse very lively or skittish?” asked Paul.
“No,” answered Mrs. Granville. “He is very gentle and tractable.”
“Then I don’t think there is any need of insuring your life, Mrs.
Granville.”
“I wouldn’t ride out with you,” said the housekeeper, spitefully.
“Perhaps you will think better of me after a while, Mrs. Mercer,” said
Paul, good naturedly.
The housekeeper closed her lips firmly and shook her head.
“I’ve made one enemy, that’s clear,” said Paul to himself—“probably
two, for the housekeeper’s son isn’t likely to be my friend.”
Mrs. Mercer led the way to a small room on the next floor.
“You can go in there,” she said, ungraciously.
“Thank you,” said Paul.
It occurred to him to wonder why so small a room should be
assigned him, while there seemed to be plenty of larger ones. In the
arrangement of the room, however, there was nothing to be desired.
Everything was neat and comfortable. To Paul, accustomed to a
shabby tenement house, it seemed luxurious, and he was disposed
to enjoy it for the time, be it long or short.
CHAPTER XXXII.
FROST MERCER IS CONTRARY.
Paul proved a satisfactory driver, and the old lady’s fears were soon
dissipated.
“You drive better than Frost Mercer,” she said in a tone of
satisfaction.
“I am glad to hear you say so, Mrs. Granville,” said Paul, well
pleased.
“Frost nearly upset me one day. I don’t think he is generally
intemperate, but I suspect he had been drinking something that
day.”
“He doesn’t seem to like me,” Paul ventured to say.
“What makes you think so?”
Hereupon Paul related his reception when he went to the barn to
harness the horses.
Mrs. Granville listened thoughtfully.
“He should not have acted so,” she said; “I presume he didn’t like
the idea of being superseded.”
“Has he been with you a long time?”
“He and his mother have been in my service for a long time. I think
Mrs. Mercer is of a jealous disposition. She never wishes me to have
any one here, but she is very faithful and loyal.”
“I wonder if that is the case,” thought Paul. The housekeeper did not
seem to him like one who would be unselfishly devoted to the
service of any one.
Welcome to our website – the perfect destination for book lovers and
knowledge seekers. We believe that every book holds a new world,
offering opportunities for learning, discovery, and personal growth.
That’s why we are dedicated to bringing you a diverse collection of
books, ranging from classic literature and specialized publications to
self-development guides and children's books.
ebookmasss.com