0% found this document useful (0 votes)
266 views

Revision Python For Computer Vision

The document discusses an introduction to computer vision and image processing in Python. It covers traditional data storage methods in Python like lists and introduces NumPy arrays as a better method. It also discusses libraries like Matplotlib, SciPy, and scikit-learn that are useful for computer vision tasks. The document is presented by Ahmed F. Gad from Menoufia University in Egypt.

Uploaded by

jwpaprk1
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
266 views

Revision Python For Computer Vision

The document discusses an introduction to computer vision and image processing in Python. It covers traditional data storage methods in Python like lists and introduces NumPy arrays as a better method. It also discusses libraries like Matplotlib, SciPy, and scikit-learn that are useful for computer vision tasks. The document is presented by Ahmed F. Gad from Menoufia University in Egypt.

Uploaded by

jwpaprk1
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

‫جامعة المنوفية‬

‫كلية الحاسبات والمعلومات‬


‫تكنولوجيا المعلومات‬
‫جامعة المنوفية‬
‫الرؤية بالحاسب‬

‫‪[email protected]‬‬
‫‪September 2018‬‬
• Overview about Computer Vision
• Traditional Data Storage in Python
• NumPy Arrays
• Matplotlib
• SciPy
• scikit-learn
• Other Libraries
• Further Reading

2 Ahmed F. Gad 9/21/2018


• Overview about Computer Vision
• Traditional Data Storage in Python
• NumPy Arrays
• Matplotlib
• SciPy
• scikit-learn
• Other Libraries
• Further Reading

3 Ahmed F. Gad 9/21/2018


• 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

4 Ahmed F. Gad 9/21/2018


• Preprocessing Noise

Filter

5 Ahmed F. Gad 9/21/2018


• Preprocessing Size in MBytes Size in KBytes
• Feature Extraction 800.625/1,024= MByte 819,840/1,024=
0.782 MByte 800.625 KByte

KByte
640x427x3
Size in Bytes
819,840 Values 8 bits
819,840
6 Ahmed F. Gad 9/21/2018
• Preprocessing
Dataset: 2,000 Images
• Feature Extraction
Dataset Size
0.782x2,000 = 1,564 MByte

GByte
1,564/1,024=
1.527 GByte
7 Ahmed F. Gad 9/21/2018
• Preprocessing
• Feature Extraction
Dataset: 50,000 Size 38.18 GByte
Intensive in its Memory and
Processing Requirements
Reduce Amounts of Data
How to Represent Image Feature
using Less Amount of Data? Extraction
8 Ahmed F. Gad 9/21/2018
Advantage of using Features rather Image Pixels
• Preprocessing
Values is that Features are Robust to Variations
• Feature Extraction such as illumination, scale, and rotation.

Summarizes Images
For example, use 2,000 values rather than 819,840

Element 0 Element 0 … Element 1,999

Examples Histogram of Oriented Gradients Scale-Invariant Feature Transform


(HOG) (SIFT)
9 Ahmed F. Gad 9/21/2018
• Preprocessing
• Feature Extraction
• Application (e.g. Classification)

10 Ahmed F. Gad 9/21/2018


• Overview about Computer Vision
• Traditional Data Storage in Python
• NumPy Arrays
• Matplotlib
• SciPy
• scikit-learn
• Other Libraries
• Further Reading

11 Ahmed F. Gad 9/21/2018


List
Data Tuple
Structures Dict
Set
Which one is suitable for
storing images?
12 Ahmed F. Gad Let`s See. 9/21/2018
• Image is MD. Which one supports MD storage?

Both

• Which one supports updating its elements?

List
Tuples are immutable.
13 9/21/2018 Ahmed F. Gad
• Let`s use the PIL (Python Image Library) for reading an image in a list.
• So, lets start storing an
image into a Python list.
The following code reads
an image in the img_list
variable.

14 Ahmed F. Gad 9/21/2018


Original Result

15 Ahmed F. Gad 9/21/2018


Core i7 Machine

16 Ahmed F. Gad 9/21/2018


• Why not applying arithmetic operations rather than looping?
img_list = img_list + 50

17 Ahmed F. Gad 9/21/2018


• 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?

18 Ahmed F. Gad 9/21/2018


• Overview about Computer Vision
• Traditional Data Storage in Python
• NumPy Arrays
• Matplotlib
• SciPy
• scikit-learn
• Other Libraries
• Further Reading

19 Ahmed F. Gad 9/21/2018


• 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.

20 Ahmed F. Gad 9/21/2018


• 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.

21 Ahmed F. Gad 9/21/2018


• After installing NumPy, we can import it in our programs and scripts.

22 Ahmed F. Gad 9/21/2018


23 Ahmed F. Gad 9/21/2018
• Overview about Computer Vision
• Traditional Data Storage in Python
• NumPy Arrays
• Matplotlib
• SciPy
• scikit-learn
• Other Libraries
• Further Reading

24 Ahmed F. Gad 9/21/2018


25 9/21/2018
Ahmed F. Gad
• 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?

26 Ahmed F. Gad 9/21/2018


• When creating the array, set the dtype argument of numpy.array to
the desired data type.

• dtype argument can be set to multiple types.

27 Ahmed F. Gad 9/21/2018


28 Ahmed F. Gad 9/21/2018
• 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.

29 Ahmed F. Gad 9/21/2018


• Arithemetic Operations

• Operations between arrays

30 Ahmed F. Gad 9/21/2018


• Array Creation

• arange

• linspace

31
arange vs. linspace
Ahmed F. Gad 9/21/2018
• 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.

Forward Start End Backward End Start


32 0 2 Ahmed F. Gad 9/21/2018 -3 -1
• Forward: my_array[start=0:stop=6:step=2]

• Backward: my_array[start=-1:stop=-6:step=-2]

• Get all elements starting from index 3

33 Ahmed F. Gad 9/21/2018


• 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]

34 Ahmed F. Gad 9/21/2018


For

While

35 Ahmed F. Gad 9/21/2018


36 Ahmed F. Gad
9/21/2018
• Overview about Computer Vision
• Traditional Data Storage in Python
• NumPy Arrays
• Matplotlib
• SciPy
• scikit-learn
• Other Libraries
• Further Reading

37 Ahmed F. Gad 9/21/2018


• 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 NumPy
functions, etc.
• SciPy supports everything in NumPy and also adds
SciPy
new features not existing in NumPy. We can
imagine that NumPy is a subset of SciPy.
• Let`s explore what is in SciPy.

38 Ahmed F. Gad 9/21/2018


• 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.

39 Ahmed F. Gad 9/21/2018


• 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.

Ahmed F. Gad 9/21/2018


40
• In addition to Scipy, there are other Python libraries for working
with images.
• Examples:
• Python Image Library (PIL)

• OpenCV

• scikit-image

41 Ahmed F. Gad 9/21/2018


• Apply erosion morphology operation using skimage.morphology
sub-module.
Binary Result

Ahmed F. Gad 9/21/2018


42
• Overview about Computer Vision
• Traditional Data Storage in Python
• NumPy Arrays
• Matplotlib
• SciPy
• scikit-learn
• Other Libraries
• Further Reading

43 Ahmed F. Gad 9/21/2018


• 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.
44 Ahmed F. Gad 9/21/2018
45 Ahmed F. Gad 9/21/2018
• Overview about Computer Vision
• Traditional Data Storage in Python
• NumPy Arrays
• Matplotlib
• SciPy
• scikit-learn
• Other Libraries
• Further Reading

46 Ahmed F. Gad 9/21/2018


• Sometimes the problem is complex and needs deep
learning. Some of the libraries used for building deep
learning models include:
• TensorFlow

• Keras

• Theano

• PyTorch

47 Ahmed F. Gad 9/21/2018


• Overview about Computer Vision
• Traditional Data Storage in Python
• NumPy Arrays
• Matplotlib
• SciPy
• scikit-learn
• Other Libraries
• Further Reading

48 Ahmed F. Gad 9/21/2018


• 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
49 9/21/2018
Ahmed F. Gad
• SciPy • TensorFlow
• https://docs.scipy.org/doc/scipy/reference • https://tensorflow.org
• NumPy • Keras
• https://docs.scipy.org/doc/numpy/reference • https://keras.io
• Matplotlib • Theano
• https://matplotlib.org/contents.html • http://deeplearning.net/software/theano
• Anaconda • PyTorch
• https://www.anaconda.com • https://pytorch.org
• scikit-image
• http://scikit-image.org
• scikit-learn
• http://scikit-learn.org
50 Ahmed F. Gad 9/21/2018

You might also like