Note to instructor:

import matplotlib.pyplot as plt  # For plotting
import numpy as np               # For the linear algebra

%matplotlib inline

from scipy.misc import face

These python notebooks can be used in one of two ways: * If students don’t know Python, you could clear the outputs from all the cells (View->Clear all outputs) and walk through each cell and discuss the outputs with the class. * If student’s know Python, you can delete the appropriate code and ask students to fill in.

Viewing images as matrices

w = 255
b = 0

A = np.array([ [w,w,w,w,w,w,w,w,w,w,w],[w,w,w,w,b,b,b,w,w,w,w],[w,w,w,w,b,b,b,w,w,w,w],[w,w,w,w,w,b,w,w,w,w,w],[w,w,w,b,b,b,b,b,w,w,w],[w,w,w,w,w,b,w,w,w,w,w],[w,w,w,w,w,b,w,w,w,w,w],[w,w,w,w,w,b,w,w,w,w,w], [w,w,w,w,b,w,b,w,w,w,w], [w,w,w,b,w,w,w,b,w,w,w], [w,w,b,w,w,w,w,w,b,w,w] ])
print('The size of the image is ', A.shape)
print('The matrix A has entries \n', A)

Exercise: Based on the entries of the matrix, on a piece of paper, sketch out the hidden object. Can you describe it?

plt.imshow(A, cmap ='gray',vmin=0,vmax=255)

Transpose

The next command will plot the transpose of a matrix.

Exercise: Can you describe the transpose of the matrix in relation to the image?

plt.imshow(A.T, cmap=plt.cm.gray,vmin=0,vmax=255)

Inverting the colors

Inverting the colors means making darker colors light and vice versa. Mathematically this can be accomplished by the formula

\[ 255*I - A ,\]

where \(I\) is the matrix of all ones.

Exercise: Describe the image when we invert the colors.

plt.imshow(255-A,cmap=plt.cm.gray,vmin=0,vmax=255)

Brightening/dimming the colors

In this next example, we divide the entries of \(A\) by \(5.\) Can you guess what happens to the corresponding image?

#@title

plt.imshow(A/5,cmap = 'gray', vmin=0,vmax=255)
plt.colorbar()

Adding and subtracting two images

We first create a simple image which can also be represented as a \(10 \times 10\) matrix and plot it to see how it looks.

 # Second image
B = np.array([ [w,w,w,w,w,w,w,w,w,w,w],[w,w,w,w,w,w,w,w,b,b,w],[w,w,w,w,w,w,w,w,b,b,w],[w,w,w,w,w,w,w,w,w,w,w],[w,w,w,w,w,w,w,w,w,w,w],[w,w,w,w,w,w,w,w,w,w,w],[w,b,b,w,w,w,w,w,w,w,w],[w,b,b,w,w,w,w,w,w,w,w], [w,w,w,w,w,w,w,w,w,w,w], [w,w,w,w,w,w,w,w,w,w,w], [w,w,w,w,w,w,w,w,w,w,w] ])
plt.imshow(B, cmap = 'gray',vmin=0,vmax=255)

Exercise: describe the image obtained by averaging A and B. That is: \(\frac{1}{2}(A+B)\).

plt.imshow(0.5*(A+B), cmap = 'gray', vmin=0,vmax=255)
plt.colorbar()

Reflecting the image

In the lesson we also looked at reflections across the x-axis and reflections across the y-axis. How does this work in the example with our human?

First recall that reflection can be performed using the “reverse” identity matrix. We first compute the \(11 \times 11\) “reverse” identity matrix \(P\).

P = np.eye(11)
P = P[::-1,:] ## Reverses the rows of the permutation matrix
print('The reverse identity matrix is \n ', P)

We learned from our guided notes that it matters which matrix is on the left and which one is on the right. So in this case if you multiplied matrix \(A\) by the “reverse identity matrix” \(P\) from the left \[ R_1 = P A\]

and then repeated it on the right

\[ R_2 = A P,\] what would you expect to happen in each case. Be specific about your hypothesis and then run the Python program to check your guess.

plt.imshow(P @ A, cmap = 'gray', vmin=0,vmax=255)
plt.imshow(A @ P, cmap = 'gray', vmin=0,vmax=255)

Blurring

Finally, we looked at blurring in this lesson as an application of averaging the entries directly around each entry. This is very relevant in the world of photography look at this example again by watching it in Python.

F = (1/9)*np.ones((3,3))
from scipy.signal import convolve2d

B = convolve2d(A,F,mode='same')
plt.imshow(B, cmap='gray', vmin=0,vmax=255)