Practical Python and
OpenCV: An Introductory,
Example Driven Guide to
Image Processing and
Computer Vision
2nd Edition
Dr. Adrian Rosebrock
C O P Y R I G H T
The contents of this book, unless otherwise indicated, are
Copyrightc2016Adrian Rosebrock, PyImageSearch.com.
All rights reserved.
This version of the book was published on6June2016.
Books like this are made possible by the time invested by
the authors. If you received this book and did not purchase
it, please consider making future books possible by buy-
ing a copy at
python-opencv/
ii
C O N T E N T S
1 introduction
2 python and required packages
2.1NumPy and SciPy 6
2.1.1Windows 7
2.1.2OSX 7
2.1.3Linux 7
2.2Matplotlib 8
2.2.1All Platforms 8
2.3OpenCV 8
2.3.1Linux and OSX 9
2.3.2Windows 10
2.4Mahotas 10
2.4.1All Platforms 11
2.5scikit-learn 11
2.5.1All Platforms 11
2.6scikit-image 12
2.7Skip the Installation 12
3 loading,displaying,and saving
4 image basics
4.1So, What's a Pixel? 19
4.2Overview of the Coordinate System 22
4.3Accessing and Manipulating Pixels 22
5 drawing
5.1Lines and Rectangles 31
5.2Circles 36
6 image processing
6.1Image Transformations 41
6.1.1Translation 42
iii
Contents
6.1.2Rotation 47
6.1.3Resizing 52
6.1.4Flipping 58
6.1.5Cropping 61
6.2Image Arithmetic 63
6.3Bitwise Operations 70
6.4Masking 73
6.5Splitting and Merging Channels 80
6.6Color Spaces 84
7 histograms
7.1Using OpenCV to Compute Histograms 88
7.2Grayscale Histograms 89
7.3Color Histograms 91
7.4Histogram Equalization 97
7.5Histograms and Masks 99
8 smoothing and blurring
8.1Averaging 107
8.2Gaussian 109
8.3Median 110
8.4Bilateral 113
9 thresholding
9.1Simple Thresholding 116
9.2Adaptive Thresholding 120
9.3Otsu and Riddler-Calvard 124
10 gradients and edge detection
10.1Laplacian and Sobel 129
10.2Canny Edge Detector 134
11 contours
11.1Counting Coins 137
12 where to now? 146
iv
P Y T H O N A N D O P E N C V V E R S I O N S
Over a year ago, when I wrote the rst edition ofPrac-
tical Python and OpenCV + Case Studies, the cu
Practical Python and OpenCV_Adrian Rosebrock.pdf