CSE 163
Numpy and Images
�Wen Qiu
Numpy Arrays
When using numpy, the primary data structure that we work with is called a numpy.array.
We saw some syntax for creating numpy arrays
2
a = np.array([12, 6]) # [12, 6]
b = np.arange(5) # [0, 1, 2, 3, 4]
c = np.ones(3) # [1, 1, 1]
Reshaping
3
We looked at how numpy arrays have shape
x = np.ones((2, 3))
x: array([[1, 1, 1],
[1, 1, 1]])
Numpy provides a function called reshape that allows us to change the shape of an array without change the data inside.
y = np.arange(9)
y = y.reshape((3, 3))
y: array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
Reshaping
4
We also saw how that numpy arrays have shape
x = np.ones((2, 3))
x: array([[1, 1, 1],
[1, 1, 1]])
Numpy provides a function called reshape that allows us to change the shape of an array without change the data inside.
y = np.arange(9)
y = y.reshape((3, 3))
y: array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
Broadcasting
5
m = np.ones((2, 3))
v = np.arange(3)
m + v
m: [[1, 1, 1],
[1, 1, 1]]
v: [0, 1, 2]
m.shape = (2, 3)
v.shape = (3,)
Numpy lets us perform element-wise operations on arrays that have the same shape (and sometimes with different shape!)
+
Broadcasting
6
m = np.ones((2, 3))
v = np.arange(3)
m + v
m: [[1, 1, 1],
[1, 1, 1]]
v: [0, 1, 2]
m.shape = (2, 3)
v.shape = (3,)
Numpy lets us perform element-wise operations on arrays that have the same shape (and sometimes with different shape!)
+
Broadcasting
7
m: [[1, 1, 1],
[1, 1, 1]]
v: [0, 1, 2]
m.shape = (2, 3)
v.shape = (3,)
+
The Rules of Broadcasting
Broadcasting
8
m: [[1, 1, 1],
[1, 1, 1]]
v: [0, 1, 2]
m.shape = (2, 3)
v.shape = (1, 3)
+
The Rules of Broadcasting
Broadcasting
9
m: [[1, 1, 1],
[1, 1, 1]]
v: [[0, 1, 2]]
m.shape = (2, 3)
v.shape = (1, 3)
+
The Rules of Broadcasting
Broadcasting
10
m: [[1, 1, 1],
[1, 1, 1]]
v: [[0, 1, 2]]
m.shape = (2, 3)
v.shape = (2, 3)
+
The Rules of Broadcasting
Broadcasting
11
m: [[1, 1, 1],
[1, 1, 1]]
v: [[0, 1, 2],
[0, 1, 2]]
m.shape = (2, 3)
v.shape = (2, 3)
+
The Rules of Broadcasting
Images as
Matrices
12
Grey-scale images can be represented as matrices.
Grey-scale: 255
Grey-scale: 0
data = imageio.imread(‘...’)�
data[rows, columns] = #
Color Images
When you overlap each color channel, it creates a picture we are used to seeing.
13
data[rows, columns, channels] = #
14
Practice: Broadcasting
Practice:
Broadcasting
15
x = np.arange(3).reshape((3, 1))
y = np.arange(3)
x + y
x: [[0],
[1],
[2]]
y: [0, 1, 2]
x.shape = (3, 1)
y.shape = (3,)
We started off with the following instructions
+
Practice:
Broadcasting
16
x: [[0],
[1],
[2]]
y: [0, 1, 2]
x.shape = (3, 1)
y.shape = (3,)
The Rules of Broadcasting
+
Practice:
Broadcasting
17
x: [[0],
[1],
[2]]
y: [0, 1, 2]
x.shape = (3, 1)
y.shape = (1, 3)
The Rules of Broadcasting
+
Practice:
Broadcasting
18
x: [[0],
[1],
[2]]
y: [[0, 1, 2]]
x.shape = (3, 1)
y.shape = (1, 3)
The Rules of Broadcasting
+
Practice:
Broadcasting
19
x: [[0],
[1],
[2]]
y: [[0, 1, 2]]
x.shape = (3, 3)
y.shape = (1, 3)
The Rules of Broadcasting
+
Practice:
Broadcasting
20
x: [[0, 0, 0]],
[1, 1, 1],
[2, 2, 2]]
y: [[0, 1, 2]]
x.shape = (3, 3)
y.shape = (1, 3)
The Rules of Broadcasting
+
Practice:
Broadcasting
21
x: [[0, 0, 0]],
[1, 1, 1],
[2, 2, 2]]
y: [[0, 1, 2]]
x.shape = (3, 3)
y.shape = (3, 3)
The Rules of Broadcasting
+
Practice:
Broadcasting
22
x: [[0, 0, 0]],
[1, 1, 1],
[2, 2, 2]]
y: [[0, 1, 2],
[0, 1, 2],
[0, 1, 2]]
x.shape = (3, 3)
y.shape = (3, 3)
The Rules of Broadcasting
+