Python is a powerful tool for computer vision applications. This presentation reviews the essential libraries required for image analysis using Python. These libraries include NumPy, SciPy, Matplotlib, Python Image Library (PIL), scikit-image, and scikit-learn.
2. • Overview about Computer Vision
• Traditional Data Storage in Python
• NumPy Arrays
• Matplotlib
• SciPy
• scikit-learn
• Other Libraries
• Further Reading
9/21/2018Ahmed F. Gad2
3. • Overview about Computer Vision
• Traditional Data Storage in Python
• NumPy Arrays
• Matplotlib
• SciPy
• scikit-learn
• Other Libraries
• Further Reading
9/21/2018Ahmed F. Gad3
4. • Computer vision aims to enable the computer to see the world
same as or better than humans. Computer should analyze the
images to identify objects and recognize them to understand the
image.
• To do this, computer needs to store and process images.
CAT
9/21/2018Ahmed F. Gad4
8. • Preprocessing
• Feature Extraction
Dataset: 50,000 38.18 GByteSize
Intensive in its Memory and
Processing Requirements
Reduce Amounts of Data
How to Represent Image
using Less Amount of Data?
Feature
Extraction
9/21/2018Ahmed F. Gad8
9. • Preprocessing
• Feature Extraction
Summarizes Images
For example, use 2,000 values rather than 819,840
Element 0 Element 0 … Element 1,999
Histogram of Oriented Gradients
(HOG)
Scale-Invariant Feature Transform
(SIFT)
Examples
Advantage of using Features rather Image Pixels
Values is that Features are Robust to Variations
such as illumination, scale, and rotation.
9/21/2018Ahmed F. Gad9
11. • Overview about Computer Vision
• Traditional Data Storage in Python
• NumPy Arrays
• Matplotlib
• SciPy
• scikit-learn
• Other Libraries
• Further Reading
9/21/2018Ahmed F. Gad11
13. • Image is MD. Which one supports MD storage?
• Which one supports updating its elements?
Both
List
Tuples are immutable.
9/21/2018 Ahmed F. Gad13
14. • So, lets start storing an
image into a Python list.
The following code reads
an image in the img_list
variable.
• Let`s use the PIL (Python Image Library) for reading an image in a list.
9/21/2018Ahmed F. Gad14
17. • Why not applying arithmetic operations rather than looping?
img_list = img_list + 50
9/21/2018Ahmed F. Gad17
18. • List makes doing operations over the images more complex and also
time consuming because they require pixel by pixel processing.
• The best way for storing images is using arrays.
• Rather than being time efficient in processing images, arrays has
many other advantages. Can you imagine what?
9/21/2018Ahmed F. Gad18
19. • Overview about Computer Vision
• Traditional Data Storage in Python
• NumPy Arrays
• Matplotlib
• SciPy
• scikit-learn
• Other Libraries
• Further Reading
9/21/2018Ahmed F. Gad19
20. • Lists are already available in Python. To use Python arrays,
additional libraries must be installed.
• The library supporting arrays in Python is called Numerical Python
(NumPy).
• NumPy can be installed using Python command-line.
• It is also available in all-in-one packages such as Anaconda.
9/21/2018Ahmed F. Gad20
21. • Based on your environment, you can install new modules.
• For traditional Python distributions, use the PIP installer.
pip install numpy
• For Anaconda, use the conda installed
conda install numpy
• It is by default available in Anaconda.
9/21/2018Ahmed F. Gad21
22. • After installing NumPy, we can import it in our programs and scripts.
9/21/2018Ahmed F. Gad22
24. • Overview about Computer Vision
• Traditional Data Storage in Python
• NumPy Arrays
• Matplotlib
• SciPy
• scikit-learn
• Other Libraries
• Further Reading
9/21/2018Ahmed F. Gad24
26. • The problem is expecting uint8 data type but another data type was
used.
• To know what is the array data type, use the dtype array property.
How to make the conversion to uint8?
9/21/2018Ahmed F. Gad26
27. • When creating the array, set the dtype argument of numpy.array to
the desired data type.
• dtype argument can be set to multiple types.
9/21/2018Ahmed F. Gad27
29. • After array being created, use the astype method of numpy.ndarray.
• It make a new copy of the array after being casted to the specified
type in the dtype argument.
9/21/2018Ahmed F. Gad29
31. • Array Creation
• arange
• linspace
arange vs. linspace 9/21/2018Ahmed F. Gad31
32. • Indexing can be forward or backward.
• Forward indexing: from start to end.
• Backward indexing: from end to start.
• General form of indexing:
my_array[start:stop:step]
• In backward indexing, the index of the last element is -1.
Start End
0 2
End Start
-3 -1
Forward Backward9/21/2018Ahmed F. Gad32
34. • For MD arrays, indexing can be applied for each individual
dimension. Intersection between the different dimensions will be
returned.
• Forward: my_array[start=0:stop=3:step=2, start=1:stop=4:step=1]
• Forward: my_array[start=-1:stop=-3:step=-1, start=0:stop=3:step=1]
9/21/2018Ahmed F. Gad34
37. • Overview about Computer Vision
• Traditional Data Storage in Python
• NumPy Arrays
• Matplotlib
• SciPy
• scikit-learn
• Other Libraries
• Further Reading
9/21/2018Ahmed F. Gad37
38. • The main use of NumPy is to support numerical
arrays in Python. According to the official
documentation, NumPy supports nothing but the
array data type and most basic operations:
indexing, sorting, reshaping, basic element-wise
functions, etc.
• SciPy supports everything in NumPy and also adds
new features not existing in NumPy. We can
imagine that NumPy is a subset of SciPy.
• Let`s explore what is in SciPy.
SciPy
NumPy
9/21/2018Ahmed F. Gad38
39. • SciPy has a collection of algorithms and functions based on NumPy.
User can use high-level commands to perform complex operations.
• SciPy is organized into a number of sub-packages.
9/21/2018Ahmed F. Gad39
40. • SciPy provides modules for working with images from reading,
processing, and saving an image.
• This example applies Sobel edge detector to an image using SciPy.
9/21/2018Ahmed F. Gad
40
41. • In addition to Scipy, there are other Python libraries for working
with images.
• Examples:
• Python Image Library (PIL)
• OpenCV
• scikit-image
9/21/2018Ahmed F. Gad41
42. • Apply erosion morphology operation using skimage.morphology
sub-module.
Binary Result
9/21/2018Ahmed F. Gad
42
43. • Overview about Computer Vision
• Traditional Data Storage in Python
• NumPy Arrays
• Matplotlib
• SciPy
• scikit-learn
• Other Libraries
• Further Reading
9/21/2018Ahmed F. Gad43
44. • After extracting features, next is to train a machine learning (ML)
model for building applications such as classification.
• One of the most popular Python library for building and training ML
algorithms is scikit-learn.
• We will discuss an example in which the random forest ensemble
technique is trained based on the Sonar Dataset for classifying an
object as either a mine or a rock. It is available at this page
(https://archive.ics.uci.edu/ml/machine-learning-
databases/undocumented/connectionist-bench/sonar/sonar.all-
data).
• The dataset has 208 samples and each sample has 60 numerical
inputs and a single output. The target is M when the object is mine
and R for rocks.
• In our example, half of the samples within each class are used for
training and the other half for testing. In other words, 104 samples
for training and another 104 samples for testing.
9/21/2018Ahmed F. Gad44
46. • Overview about Computer Vision
• Traditional Data Storage in Python
• NumPy Arrays
• Matplotlib
• SciPy
• scikit-learn
• Other Libraries
• Further Reading
9/21/2018Ahmed F. Gad46
47. • Sometimes the problem is complex and needs deep
learning. Some of the libraries used for building deep
learning models include:
• TensorFlow
• Keras
• Theano
• PyTorch
9/21/2018Ahmed F. Gad47
48. • Overview about Computer Vision
• Traditional Data Storage in Python
• NumPy Arrays
• Matplotlib
• SciPy
• scikit-learn
• Other Libraries
• Further Reading
9/21/2018Ahmed F. Gad48
49. • Books
• Ahmed F. Gad 'Practical Computer Vision
Applications Using Deep Learning with CNNs'.
Apress, 2019, 978-1-4842-4167-7.
https://amazon.com/Practical-Computer-Vision-
Applications-Learning/dp/1484241665
• Ahmed F. Gad "TensorFlow: A Guide To Build
Artificial Neural Networks Using Python". LAP
LAMBERT Academic Publishing, 2017, 978-620-2-
07312-7. https://www.amazon.com/TensorFlow-
Artificial-Networks-artificial-
explanation/dp/6202073128
• Tutorials
• https://www.kdnuggets.com/author/ahmed-gad
• http://youtube.com/AhmedGadFCIT
• http://slideshare.com/AhmedGadFCIT
• https://linkedin.com/in/AhmedFGad
9/21/2018
Ahmed F. Gad
49