Python for Computer Vision
Ahmed Fawzy Gad
ahmed.fawzy@ci.menofia.edu.eg
MENOUFIA UNIVERSITY
FACULTY OF COMPUTERS AND INFORMATION
INFORMATION TECHNOLOGY
COMPUTER VISION
‫المنوفية‬ ‫جامعة‬
‫والمعلومات‬ ‫الحاسبات‬ ‫كلية‬
‫المعلومات‬ ‫تكنولوجيا‬
‫بالحاسب‬ ‫الرؤية‬
‫المنوفية‬ ‫جامعة‬
Tuesday 26 September 2017
Index
• Traditional Data Storage in Python
• NumPy Arrays
• Matplotlib
• SciPy
Goals of Computer Vision
• Computer vision aims to enable computer to see, identify objects,
and analyze such images to understand them like or better than
humans.
• To do this, computer needs to store and process images to get useful
information.
CAT
Storing Data in Python
List
Tuple
Data
Structures
Which one is suitable for
storing images?
Let`s See.
List Vs. Tuple
• Image is MD. Which one supports MD storage?
• Which one supports updating its elements?
Both
List
Tuples are immutable.
Python List for Image Storage
• List is mutable and thus
we can edit the image
pixels easily and apply
operations.
• So, lets start storing an
image into a Python list.
The following code reads
an image in the img_list
variable.
Very time consuming for simple operations
Very time consuming for simple operations.
• Why not applying arithmetic operations rather than looping?
img_list = img_list + 50
List is complex. What is the alternative?
• List adds more complexity in making operations over the images. One
drawback was seen previously is that list operations are 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?
Python Arrays
• Lists are already available in Python. To use Python arrays, additional
libraries must be used.
• 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 like Anaconda.
Installing NumPy
• 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
• But it is by default available in Anaconda.
Importing a Python Module
• After installing NumPy, we can import it in our programs and scripts.
NumPy Array for MD Data
Matplotlib: Displaying the Image
Array Data Type
• 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.
• Change array type to uint8.
How to make the conversion to uint8?
Controlling Array dtype
• When creating the array, set the dtype argument of numpy.array to
the desired data type.
• dtype argument can be set to multiple types.
Creating Array with dtype of uint8
Controlling Array dtype
• 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.
Array Operations
• Arithemetic Operations
• Operations between arrays
More Array Creation Methods
• Array Creation
• arange
• linspace
arange vs. linspace
Array Indexing & Slicing
• 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 Backward
Indexing & Slicing Examples – 1D Array
• 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
Indexing & Slicing Examples – 2D Array
• 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]
Iterating Through Arrays
For
While
Matplotlib: Plotting Data
Scientific Python (SciPy)
• 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 elementwise
functions, etc.
• SciPy supports everything in NumPy but 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
SciPy
• SciPy contains 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 subpackages.
SciPy for Image Processing
• SciPy provides modules for working with images from reading,
processing, and saving an image.
• This example applies the Sobel edge detector to an image using SciPy.
References
• SciPy
• https://docs.scipy.org/doc/scipy/reference
• NumPy
• https://docs.scipy.org/doc/numpy/reference
• Matplotlib
• https://matplotlib.org/contents.html

Python for Computer Vision - Revision

  • 1.
    Python for ComputerVision Ahmed Fawzy Gad [email protected] MENOUFIA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATION INFORMATION TECHNOLOGY COMPUTER VISION ‫المنوفية‬ ‫جامعة‬ ‫والمعلومات‬ ‫الحاسبات‬ ‫كلية‬ ‫المعلومات‬ ‫تكنولوجيا‬ ‫بالحاسب‬ ‫الرؤية‬ ‫المنوفية‬ ‫جامعة‬ Tuesday 26 September 2017
  • 2.
    Index • Traditional DataStorage in Python • NumPy Arrays • Matplotlib • SciPy
  • 3.
    Goals of ComputerVision • Computer vision aims to enable computer to see, identify objects, and analyze such images to understand them like or better than humans. • To do this, computer needs to store and process images to get useful information. CAT
  • 4.
    Storing Data inPython List Tuple Data Structures Which one is suitable for storing images? Let`s See.
  • 5.
    List Vs. Tuple •Image is MD. Which one supports MD storage? • Which one supports updating its elements? Both List Tuples are immutable.
  • 6.
    Python List forImage Storage • List is mutable and thus we can edit the image pixels easily and apply operations. • So, lets start storing an image into a Python list. The following code reads an image in the img_list variable.
  • 8.
    Very time consumingfor simple operations
  • 9.
    Very time consumingfor simple operations. • Why not applying arithmetic operations rather than looping? img_list = img_list + 50
  • 10.
    List is complex.What is the alternative? • List adds more complexity in making operations over the images. One drawback was seen previously is that list operations are 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?
  • 11.
    Python Arrays • Listsare already available in Python. To use Python arrays, additional libraries must be used. • 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 like Anaconda.
  • 12.
    Installing NumPy • Basedon 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 • But it is by default available in Anaconda.
  • 13.
    Importing a PythonModule • After installing NumPy, we can import it in our programs and scripts.
  • 14.
  • 15.
  • 16.
    Array Data Type •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. • Change array type to uint8. How to make the conversion to uint8?
  • 17.
    Controlling Array dtype •When creating the array, set the dtype argument of numpy.array to the desired data type. • dtype argument can be set to multiple types.
  • 18.
    Creating Array withdtype of uint8
  • 19.
    Controlling Array dtype •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.
  • 20.
    Array Operations • ArithemeticOperations • Operations between arrays
  • 21.
    More Array CreationMethods • Array Creation • arange • linspace arange vs. linspace
  • 22.
    Array Indexing &Slicing • 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 Backward
  • 23.
    Indexing & SlicingExamples – 1D Array • 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
  • 24.
    Indexing & SlicingExamples – 2D Array • 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]
  • 25.
  • 26.
  • 27.
    Scientific Python (SciPy) •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 elementwise functions, etc. • SciPy supports everything in NumPy but 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
  • 28.
    SciPy • SciPy containsa 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 subpackages.
  • 29.
    SciPy for ImageProcessing • SciPy provides modules for working with images from reading, processing, and saving an image. • This example applies the Sobel edge detector to an image using SciPy.
  • 30.
    References • SciPy • https://docs.scipy.org/doc/scipy/reference •NumPy • https://docs.scipy.org/doc/numpy/reference • Matplotlib • https://matplotlib.org/contents.html