Lab Python Numpy Opencv
Lab Python Numpy Opencv
4
Package managers
9
Installing some stuff
10
Working with Python
You can use Python in two ways:
• Run the program directly through the Python
interpreter
• Use an enhanced command-line REPL
environment (IPython) by either:
– Invoking ipython from a shell $ ipython
– Using a local qt-based console $ jupyter qtconsole
– Using a web-based environment (jupyter lab or jupyter
notebook)
Jupyter is a powerful way to experiment new
algorithms before programming a standalone python
application
11
Jupyter lab
To use Jupyter lab run the command
$ jupyter lab
12
Jupyter lab
Jupyter is a web interface based on a client-server
architecture:
• A «notebook» represent a front-end to the user in which the
python code can be grouped in different cells
• Each notebook is backed by a python process (called
kernel) in which the cells are executed
• A kernel, unless manually restarted, remains active
between different code executions (ie. variables declared in
one cell are visible to the others after execution)
• An interactive console can refer to the same kernel used by
a notebook to evaluate code on-the-fly
• A jupyter notebook can be converted back to a python file
with: $ jupyter nbconvert --to python
13 Notebook.ipynb
Working with data
16
numpy
1 2 3 4 1 4 7 10
5 6 7 8 2 5 8 11
9 10 11 12 3 6 9 12
17
Strides
Row-major and column-major ordering are special
cases of strategies (2D case) for mapping the index
used to address an element, to the offset for the
element in the array’s memory
1 4 7 10 A.shape = (3,4)
2 5 8 11 A.dtype = uint32
3 6 9 12 A.strides = (4,16)
np.array Creates an array for which the elements are given by an array-like
object (ie. python list, a tuple, an iterable sequence, or another ndarray
instance)
np.zeros Creates an array filled with zeros
np.diag Creates a diagonal array with specified values along the diagonal
np.linspace Creates an array with evenly spaced values between specified start and
end values, using a specified number of elements.
np.meshgrid Generates coordinate matrices (and higher-dimensional coordinate
arrays) from one-dimensional coordinate vectors
np.loadtxt Creates an array from a text file (for example a CSV file)
https://docs.scipy.org/doc/numpy/reference/routines.array-creation.html
21
Indexing and slicing
A[-m] Select the mth element from the end of the list.
A[m:] Select elements starting with index m and going up to the last element in
the array.
A[m:n:p] Select elements with index m through n (exclusive), with increment p
np.concatenate Creates a new array by appending arrays after each other, along
a given axis.
np.resize Resizes an array. Creates a new copy of the original array, with
the requested size. If necessary, the original array will be
repeated to fill up the new array.
25
Vectorized expressions and
broadcasting
NumPy implements functions and vectorized
operations corresponding to most fundamental
mathematical functions and operators
https://docs.scipy.org/doc/numpy/reference/routines.math.html
• Most of these functions and operations act on
arrays on an elementwise basis
27
Vectorized expressions and
broadcasting
How broadcasting works:
• If the number of axes of the two arrays is not
equal, the array with fewer axes is padded with
new axes of length 1 from the left until the
numbers of dimensions of the two arrays agree.
• If an input has a dimension size of 1 in its shape,
the first data entry in that dimension will be used
for all calculations along that dimension. In other
words, the stride will be 0 for that dimension and
the numbers are hence repeated.
• Operations are then performed element-wise
28
Broadcasting examples
1 2 3 1 2 5 1 4 15
4 5 6 * 3 2 1 = 12 10 6
7 8 9 7 1 3 49 8 27
29
Broadcasting examples
1 2 3 1
4 5 6 * 3
7 8 9 7
(3,3) (3,1)
1 1 1 1 2 3
3 3 3 = 12 15 18
7 7 7 49 56 63
(3,3)
(3,3)
30
Broadcasting examples
1 2 3
4 5 6 * 1 2 5
7 8 9
1 2 5 1 4 15
1 2 5 = 4 10 30
1 2 5 7 16 45
(3,3)
(3,3)
31
Broadcasting examples
1 2 3
4 5 6 * 2
7 8 9
2 2 2 2 4 6
2 2 2 = 8 10 12
2 2 2 14 16 18
(3,3)
(3,3)
32
Broadcasting examples
1 2 3
7
5
8
6
9
* 1 2 =
?
(3,3)
33
Broadcasting examples
1 2 3
7
5
8
6
9
* 1 2 =
X
(3,3)
34
Matrices
35
Linear algebra with arrays
Function Description
36
OpenCV Basics
Imgcodecs documentation:
http://docs.opencv.org/3.4.5/d4/da8/group__imgcodecs.html
imread function:
http://docs.opencv.org/3.4.5/d4/da8/group__imgcodecs.html#ga288b8b3da089
2bd651fce07b3bbd3a56
imwrite function:
http://docs.opencv.org/3.4.5/d4/da8/group__imgcodecs.html#gabbc7ef1aa2edf
aa87772f1202d67e0ce
Other useful functions
Color-space conversion:
http://docs.opencv.org/3.4.5/d7/d1b/group__imgproc__misc.html#ga397ae87
e1288a81d2363b61574eb8cab
Thresholding:
https://docs.opencv.org/3.4.0/d7/d1b/group__imgproc__misc.html#gae8a4a1
46d1ca78c626a53577199e9c57
Histogram:
https://docs.opencv.org/3.4.0/d6/dc7/group__imgproc__hist.html#ga4b2b5fd7
5503ff9e6844cc4dcdaed35d
Displaying images
highgui documentation:
http://docs.opencv.org/3.4.5/d7/dfc/group__highgui.html
imshow function:
http://docs.opencv.org/3.4.5/d7/dfc/group__highgui.html#ga453d42fe4cb60e57
23281a89973ee563
NOTE: The function should be followed by waitKey function which displays the
image for specified milliseconds. Otherwise, it won't display the image.
Images in Jupyter
General usage:
import matplotlib.pyplot as plt
plt.figure()
plt.imshow( I )
plt.title(“My image”)
43
Interactive plots
Jupyter supports widgets to automatically create user
interface (UI) controls for exploring code and data
interactively.
def plot_func(freq):
x = np.linspace(0,
2*np.pi,int(100*freq))
y = np.sin(x * freq)
plt.plot(x, y)
45