In [1]:
from __future__ import division, print_function
%matplotlib inline

Introduction: images are numpy arrays

A grayscale image is just a 2D array:


In [ ]:
import numpy as np
r = np.random.rand(500, 500)
from matplotlib import pyplot as plt, cm
plt.imshow(r, cmap=cm.gray, interpolation='nearest')

Trying that with a real picture:


In [ ]:
from skimage import data
coins = data.coins()
print(type(coins))
print(coins.dtype)
print(coins.shape)
plt.imshow(coins, cmap=cm.gray, interpolation='nearest')

A color image is a 3D array, where the last dimension has size 3 and represents the red, green, and blue channels:


In [ ]:
lena = data.lena()
print(lena.shape)
plt.imshow(lena, interpolation='nearest')

These are just numpy arrays. Making a red square is easy using just array slicing and manipulation:


In [ ]:
lena[100:200, 100:200, :] = [255, 0, 0] # [red, green, blue]
plt.imshow(lena)

As we will see, this opens up many lines of analysis for free.

Exercise: draw an H

Define a function that takes as input an RGB image and a pair of coordinates (row, column), and returns the image (optionally a copy) with green letter H overlaid at those coordinates. The coordinates should point to the top-left corner of the H.

The arms and strut of the H should have a width of 3 pixels, and the H itself should have a height of 24 pixels and width of 20 pixels.


In [ ]:
def draw_h(image, coords, in_place=True):
    pass # code goes here

Test your function like so:


In [ ]:
lena_h = draw_h(lena, (50, -50), in_place=False)
plt.imshow(lena_h)

Bonus points: RGB intensity plot

Plot the intensity of each channel of the image along some row.


In [ ]:
def plot_intensity(image, row):
    pass # code goes here

Test your function here:


In [ ]:
plot_intensity(coins, 50)
plot_intensity(lena, 250)