python for computer vision - revision
TRANSCRIPT
![Page 1: Python for Computer Vision - Revision](https://reader036.vdocuments.pub/reader036/viewer/2022062412/5a677a067f8b9a656a8b560b/html5/thumbnails/1.jpg)
Python for Computer Vision
Ahmed Fawzy Gad
MENOUFIA UNIVERSITYFACULTY OF COMPUTERS AND INFORMATION
INFORMATION TECHNOLOGYCOMPUTER VISION
جامعة المنوفية
كلية الحاسبات والمعلومات
تكنولوجيا المعلومات
الرؤية بالحاسب
جامعة المنوفية
Tuesday 26 September 2017
![Page 2: Python for Computer Vision - Revision](https://reader036.vdocuments.pub/reader036/viewer/2022062412/5a677a067f8b9a656a8b560b/html5/thumbnails/2.jpg)
Index
• Traditional Data Storage in Python
• NumPy Arrays
• Matplotlib
• SciPy
![Page 3: Python for Computer Vision - Revision](https://reader036.vdocuments.pub/reader036/viewer/2022062412/5a677a067f8b9a656a8b560b/html5/thumbnails/3.jpg)
Goals of Computer Vision
• Computer vision aims to enable computer to see, identify objects,and analyze such images to understand them like or better thanhumans.
• To do this, computer needs to store and process images to get usefulinformation.
CAT
![Page 4: Python for Computer Vision - Revision](https://reader036.vdocuments.pub/reader036/viewer/2022062412/5a677a067f8b9a656a8b560b/html5/thumbnails/4.jpg)
Storing Data in Python
ListTuple
Data Structures
Which one is suitable for storing images?
Let`s See.
![Page 5: Python for Computer Vision - Revision](https://reader036.vdocuments.pub/reader036/viewer/2022062412/5a677a067f8b9a656a8b560b/html5/thumbnails/5.jpg)
List Vs. Tuple
• Image is MD. Which one supports MD storage?
• Which one supports updating its elements?
Both
ListTuples are immutable.
![Page 6: Python for Computer Vision - Revision](https://reader036.vdocuments.pub/reader036/viewer/2022062412/5a677a067f8b9a656a8b560b/html5/thumbnails/6.jpg)
Python List for Image Storage
• List is mutable and thuswe can edit the imagepixels easily and applyoperations.
• So, lets start storing animage into a Python list.The following code readsan image in the img_listvariable.
![Page 7: Python for Computer Vision - Revision](https://reader036.vdocuments.pub/reader036/viewer/2022062412/5a677a067f8b9a656a8b560b/html5/thumbnails/7.jpg)
![Page 8: Python for Computer Vision - Revision](https://reader036.vdocuments.pub/reader036/viewer/2022062412/5a677a067f8b9a656a8b560b/html5/thumbnails/8.jpg)
Very time consuming for simple operations
![Page 9: Python for Computer Vision - Revision](https://reader036.vdocuments.pub/reader036/viewer/2022062412/5a677a067f8b9a656a8b560b/html5/thumbnails/9.jpg)
Very time consuming for simple operations.
• Why not applying arithmetic operations rather than looping?
img_list = img_list + 50
![Page 10: Python for Computer Vision - Revision](https://reader036.vdocuments.pub/reader036/viewer/2022062412/5a677a067f8b9a656a8b560b/html5/thumbnails/10.jpg)
List is complex. What is the alternative?
• List adds more complexity in making operations over the images. Onedrawback was seen previously is that list operations are timeconsuming 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 hasmany other advantages. Can you imagine what?
![Page 11: Python for Computer Vision - Revision](https://reader036.vdocuments.pub/reader036/viewer/2022062412/5a677a067f8b9a656a8b560b/html5/thumbnails/11.jpg)
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.
![Page 12: Python for Computer Vision - Revision](https://reader036.vdocuments.pub/reader036/viewer/2022062412/5a677a067f8b9a656a8b560b/html5/thumbnails/12.jpg)
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.
![Page 13: Python for Computer Vision - Revision](https://reader036.vdocuments.pub/reader036/viewer/2022062412/5a677a067f8b9a656a8b560b/html5/thumbnails/13.jpg)
Importing a Python Module
• After installing NumPy, we can import it in our programs and scripts.
![Page 14: Python for Computer Vision - Revision](https://reader036.vdocuments.pub/reader036/viewer/2022062412/5a677a067f8b9a656a8b560b/html5/thumbnails/14.jpg)
NumPy Array for MD Data
![Page 15: Python for Computer Vision - Revision](https://reader036.vdocuments.pub/reader036/viewer/2022062412/5a677a067f8b9a656a8b560b/html5/thumbnails/15.jpg)
Matplotlib: Displaying the Image
![Page 16: Python for Computer Vision - Revision](https://reader036.vdocuments.pub/reader036/viewer/2022062412/5a677a067f8b9a656a8b560b/html5/thumbnails/16.jpg)
Array Data Type
• The problem is expecting uint8 data type but another data type wasused.
• 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?
![Page 17: Python for Computer Vision - Revision](https://reader036.vdocuments.pub/reader036/viewer/2022062412/5a677a067f8b9a656a8b560b/html5/thumbnails/17.jpg)
Controlling Array dtype
• When creating the array, set the dtype argument of numpy.array tothe desired data type.
• dtype argument can be set to multiple types.
![Page 18: Python for Computer Vision - Revision](https://reader036.vdocuments.pub/reader036/viewer/2022062412/5a677a067f8b9a656a8b560b/html5/thumbnails/18.jpg)
Creating Array with dtype of uint8
![Page 19: Python for Computer Vision - Revision](https://reader036.vdocuments.pub/reader036/viewer/2022062412/5a677a067f8b9a656a8b560b/html5/thumbnails/19.jpg)
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 specifiedtype in the dtype argument.
![Page 20: Python for Computer Vision - Revision](https://reader036.vdocuments.pub/reader036/viewer/2022062412/5a677a067f8b9a656a8b560b/html5/thumbnails/20.jpg)
Array Operations
• Arithemetic Operations
• Operations between arrays
![Page 21: Python for Computer Vision - Revision](https://reader036.vdocuments.pub/reader036/viewer/2022062412/5a677a067f8b9a656a8b560b/html5/thumbnails/21.jpg)
More Array Creation Methods
• Array Creation
• arange
• linspace
arange vs. linspace
![Page 22: Python for Computer Vision - Revision](https://reader036.vdocuments.pub/reader036/viewer/2022062412/5a677a067f8b9a656a8b560b/html5/thumbnails/22.jpg)
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 End0 2
End Start-3 -1
Forward Backward
![Page 23: Python for Computer Vision - Revision](https://reader036.vdocuments.pub/reader036/viewer/2022062412/5a677a067f8b9a656a8b560b/html5/thumbnails/23.jpg)
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
![Page 24: Python for Computer Vision - Revision](https://reader036.vdocuments.pub/reader036/viewer/2022062412/5a677a067f8b9a656a8b560b/html5/thumbnails/24.jpg)
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]
![Page 25: Python for Computer Vision - Revision](https://reader036.vdocuments.pub/reader036/viewer/2022062412/5a677a067f8b9a656a8b560b/html5/thumbnails/25.jpg)
Iterating Through Arrays
For
While
![Page 26: Python for Computer Vision - Revision](https://reader036.vdocuments.pub/reader036/viewer/2022062412/5a677a067f8b9a656a8b560b/html5/thumbnails/26.jpg)
Matplotlib: Plotting Data
![Page 27: Python for Computer Vision - Revision](https://reader036.vdocuments.pub/reader036/viewer/2022062412/5a677a067f8b9a656a8b560b/html5/thumbnails/27.jpg)
Scientific Python (SciPy)
• The main use of NumPy is to support numericalarrays in Python. According to the officialdocumentation, NumPy supports nothing but thearray data type and most basic operations:indexing, sorting, reshaping, basic elementwisefunctions, etc.
• SciPy supports everything in NumPy but also addsnew features not existing in NumPy. We canimagine that NumPy is a subset of SciPy.
• Let`s explore what is in SciPy.
SciPy
NumPy
![Page 28: Python for Computer Vision - Revision](https://reader036.vdocuments.pub/reader036/viewer/2022062412/5a677a067f8b9a656a8b560b/html5/thumbnails/28.jpg)
SciPy
• SciPy contains a collection of algorithms and functions based onNumPy. User can use high-level commands to perform complexoperations.
• SciPy is organized into a number of subpackages.
![Page 29: Python for Computer Vision - Revision](https://reader036.vdocuments.pub/reader036/viewer/2022062412/5a677a067f8b9a656a8b560b/html5/thumbnails/29.jpg)
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.
![Page 30: Python for Computer Vision - Revision](https://reader036.vdocuments.pub/reader036/viewer/2022062412/5a677a067f8b9a656a8b560b/html5/thumbnails/30.jpg)
References
• SciPy• https://docs.scipy.org/doc/scipy/reference
• NumPy• https://docs.scipy.org/doc/numpy/reference
• Matplotlib• https://matplotlib.org/contents.html