1 of 22

CSE 163

Numpy and Images

Wen Qiu

💬 Before class: Favorite dessert?

🎵 Music: Hamilton

2 of 22

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]

3 of 22

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

4 of 22

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

5 of 22

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!)

+

6 of 22

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!)

+

7 of 22

Broadcasting

7

m: [[1, 1, 1],

[1, 1, 1]]

v: [0, 1, 2]

m.shape = (2, 3)

v.shape = (3,)

+

The Rules of Broadcasting

  1. If the two arrays differ in their number of dimensions, the shape of the one with fewer dimensions is padded on it’s left side.
  2. If the shape of two arrays does not match on any dimension, the array with shape equal to 1 in that dimension is stretched to match the other shape.
  3. If the sizes of any dimension disagree and neither is equal to 1, an error is raised.

8 of 22

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

  • If the two arrays differ in their number of dimensions, the shape of the one with fewer dimensions is padded on it’s left side.
  • If the shape of two arrays does not match on any dimension, the array with shape equal to 1 in that dimension is stretched to match the other shape.
  • If the sizes of any dimension disagree and neither is equal to 1, an error is raised.

9 of 22

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

  • If the two arrays differ in their number of dimensions, the shape of the one with fewer dimensions is padded on it’s left side.
  • If the shape of two arrays does not match on any dimension, the array with shape equal to 1 in that dimension is stretched to match the other shape.
  • If the sizes of any dimension disagree and neither is equal to 1, an error is raised.

10 of 22

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

  • If the two arrays differ in their number of dimensions, the shape of the one with fewer dimensions is padded on it’s left side.
  • If the shape of two arrays does not match on any dimension, the array with shape equal to 1 in that dimension is stretched to match the other shape.
  • If the sizes of any dimension disagree and neither is equal to 1, an error is raised.

11 of 22

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

  • If the two arrays differ in their number of dimensions, the shape of the one with fewer dimensions is padded on it’s left side.
  • If the shape of two arrays does not match on any dimension, the array with shape equal to 1 in that dimension is stretched to match the other shape.
  • If the sizes of any dimension disagree and neither is equal to 1, an error is raised.

12 of 22

Images as

Matrices

12

Grey-scale images can be represented as matrices.

Grey-scale: 255

Grey-scale: 0

data = imageio.imread(‘...’)�

data[rows, columns] = #

13 of 22

Color Images

When you overlap each color channel, it creates a picture we are used to seeing.

  • Pixels on your monitor let out specified R/G/B light

13

data[rows, columns, channels] = #

14 of 22

14

Practice: Broadcasting

15 of 22

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

+

16 of 22

Practice:

Broadcasting

16

x: [[0],

[1],

[2]]

y: [0, 1, 2]

x.shape = (3, 1)

y.shape = (3,)

The Rules of Broadcasting

  • If the two arrays differ in their number of dimensions, the shape of the one with fewer dimensions is padded on it’s left side.
  • If the shape of two arrays does not match on any dimension, the array with shape equal to 1 in that dimension is stretched to match the other shape.
  • If in any dimension the sizes disagree and neither is equal to 1, an error is raised.

+

17 of 22

Practice:

Broadcasting

17

x: [[0],

[1],

[2]]

y: [0, 1, 2]

x.shape = (3, 1)

y.shape = (1, 3)

The Rules of Broadcasting

  • If the two arrays differ in their number of dimensions, the shape of the one with fewer dimensions is padded on it’s left side.
  • If the shape of two arrays does not match on any dimension, the array with shape equal to 1 in that dimension is stretched to match the other shape.
  • If in any dimension the sizes disagree and neither is equal to 1, an error is raised.

+

18 of 22

Practice:

Broadcasting

18

x: [[0],

[1],

[2]]

y: [[0, 1, 2]]

x.shape = (3, 1)

y.shape = (1, 3)

The Rules of Broadcasting

  • If the two arrays differ in their number of dimensions, the shape of the one with fewer dimensions is padded on it’s left side.
  • If the shape of two arrays does not match on any dimension, the array with shape equal to 1 in that dimension is stretched to match the other shape.
  • If in any dimension the sizes disagree and neither is equal to 1, an error is raised.

+

19 of 22

Practice:

Broadcasting

19

x: [[0],

[1],

[2]]

y: [[0, 1, 2]]

x.shape = (3, 3)

y.shape = (1, 3)

The Rules of Broadcasting

  • If the two arrays differ in their number of dimensions, the shape of the one with fewer dimensions is padded on it’s left side.
  • If the shape of two arrays does not match on any dimension, the array with shape equal to 1 in that dimension is stretched to match the other shape.
  • If in any dimension the sizes disagree and neither is equal to 1, an error is raised.

+

20 of 22

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

  • If the two arrays differ in their number of dimensions, the shape of the one with fewer dimensions is padded on it’s left side.
  • If the shape of two arrays does not match on any dimension, the array with shape equal to 1 in that dimension is stretched to match the other shape.
  • If in any dimension the sizes disagree and neither is equal to 1, an error is raised.

+

21 of 22

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

  • If the two arrays differ in their number of dimensions, the shape of the one with fewer dimensions is padded on it’s left side.
  • If the shape of two arrays does not match on any dimension, the array with shape equal to 1 in that dimension is stretched to match the other shape.
  • If in any dimension the sizes disagree and neither is equal to 1, an error is raised.

+

22 of 22

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

  • If the two arrays differ in their number of dimensions, the shape of the one with fewer dimensions is padded on it’s left side.
  • If the shape of two arrays does not match on any dimension, the array with shape equal to 1 in that dimension is stretched to match the other shape.
  • If in any dimension the sizes disagree and neither is equal to 1, an error is raised.

+