For the purposes of this tutorial we are going to use a part of matplotlib called pyplot. We import it by doing:

%matplotlib inline

import numpy as np
from matplotlib import pyplot as plt

Images are 2-dimensional arrays containing pixels. We can use 2-dimensional arrays to represent image data and visualise with matplotlib:

arr = np.arange(100).reshape(10,10)

print(arr)

plt.imshow(arr)

Exercise 2.1: Can you create a similar image but with shape (50,50)?

from check_answer import check_answer

answ = np.arange(?).reshape(?)

plt.imshow(answ)

check_answer("2.1", answ.sum())

If you remember from the last tutorial we were able to address regions of a numpy array using the [ ] index notation. For multidimensional arrays we can user , to designate the different axis. For example we can update the values on the left part of this array to be equal to 1.

arr = np.arange(100).reshape(10,10)
arr[:, :5] = 1

plt.imshow(arr)

Note: We have selected all the values : in the first axis (vertical) and the first 5 values :5 on the second axis (horizontal)

Exercise 2.2: Can you do something similar updating all the values in the top-left half corner to 1

answ = np.arange(100).reshape(10,10)
answ[?] = 1

plt.imshow(answ)

check_answer("2.2", answ.sum())

Exercise 2.3: Remember how we used boolean arrays in the previous tutorial to select parts of an array? How would you create a boolean array indicating the values that are greater than 49?

Tip: Your new array should be a boolean array with the same shape as the initial array.

answ = np.arange(100).reshape(10,10)
answ = ?

plt.imshow(answ)

check_answer("2.3", answ.sum())

We can use the Pyplot library to load an image using the matplotlib function imread. The image that we get is a 3-dimensional numpy array. By convention the first dimension corresponds to the vertical axis, the second to the horizontal axis and the third are the Red, Green and Blue channels of the image.

im = np.copy(plt.imread('data/black_mountain_fire.jpg'))

im.shape

Exercise 2.4: Can you find the data type of the previous im array? Remeber the suffix we used in the previous tutorial to find out the type of an array

answ = im.?

check_answer("2.4", answ)

Let's display this image using the imshow function.

plt.imshow(im)

This is a photo of Black Mountain (ACT) taken during some prescribed burns in 2014. A colour image is normally composed of three layers containing the values of the red, green and blue pixels. When we display an image we see all three colours combined.

Exersise 2.5: Let's use the indexing functionality of numpy to select a portion of this image. Would you be able to select the top-right corner of this image with shape (800,800)? Remember there are three axes in this image.

answ = im[?]

plt.imshow(answ)

check_answer("2.5", answ.mean())

Exercise 2.6: Let's get some more practice to improve your indexing skills! Can you create a cropped image around Black Mountain's tower? Remember: first dimension is the vertical coordinates, second dimension is the horizontal coordinates and the third are the RGB channels of the image.

answ = im[?]

plt.imshow(answ)

Let's have a look at one of the pixels in this image. We choose the top-left corner with position (0,0) and show the values of its RGB channels.

im[0,0]

The first value corresponds to the red component, the second to the green and the third to the blue. uint8 can contain values in the range [0-255] so the pixel is mostly blue, which makes sense for a pixel representing the sky.

Now let's modify the image. What happens if we set all the values representing the green channel to the maximum value?

# We first make a copy to avoid modifying the original image.
im2 = np.copy(im)

im2[:,:,1] = 255

plt.imshow(im2)

Exercise 2.7: Could you create an artistic representation of the previous image saturating different parts of the image with different colours? We want the top-left quadrant with the red channel saturated, the top-right with the green channel satured, the bottom-left with the blue channel saturated and the bottom-right as it is.

im2 = np.copy(im)

im2[?] = 255
im2[?] = 255
im2[?] = 255

plt.imshow(im2)