11 KiB
# You must run this cell, but you can ignore its contents.
import hashlib
def ads_hash(ty):
"""Return a unique string for input"""
ty_str = str(ty).encode()
m = hashlib.sha256()
m.update(ty_str)
return m.hexdigest()[:10]
Numpy array for images¶
import numpy as np
import matplotlib.pyplot as plt
Images are saved in computers as arrays of numbers. In Python, the library called numpy
(often abbreviated np
for short). is the most common way to manipulate arrays of numbers. Here we will create an 8x8 pixel image and put it in the variable check
.
# Create a variable called `check` which will contain an 8x8 array of numbers.
check = np.zeros((8, 8))
check[::2, 1::2] = 1
check[1::2, ::2] = 1
check
# Now lets view our 8x8 pixel image:
plt.imshow(check, cmap='gray');
Questions Part A - Image representation¶
Use numpy slicing to set the values in an array:
- Use
plt.imshow
to show a new 8x8 pixel image in which the top half is white and the bottom half is black. - Use
plt.imshow
to show a new 8x8 pixel image in which the left half is white and the right half is black.
# YOUR CODE HERE
raise NotImplementedError()
# YOUR CODE HERE
raise NotImplementedError()
Questions Part B - More image representation¶
Write the numerical value corresponding with each pixel. In other words, what number corresponds with the black color and which number corresponds with the white color?
# YOUR CODE HERE
raise NotImplementedError()
# If this runs without error, it means the answer in your previous cell was correct.
assert ads_hash(float(black))=='8aed642bf5'
assert ads_hash(float(white))=='d0ff5974b6'
# If import skimage fails, you can install it like this in Jupyter:
# !pip install scikit-image
# or like this at the command line:
# python -m pip install scikit-image
from skimage import data
# Load the sample image data into a variable called `camera`.
camera = data.camera()
plt.figure(figsize=(4, 4))
plt.imshow(camera, cmap='gray', interpolation='nearest', vmin=0, vmax=255)
plt.axis('off')
plt.tight_layout()
plt.show()
So, let's checkout some things about the image. How many pixels are here? Put your answer in the num_pixels
variable.
# YOUR CODE HERE
raise NotImplementedError()
# If this runs without error, it means the answer in your previous cell was correct.
assert ads_hash(num_pixels)=='54faea9b3e'
What is the distribution of luminance values in the image?
plt.hist(camera.flat,bins=256);
plt.xlabel('luminance');
plt.ylabel('number of occurances');
We can see that there are two main peaks in the intensity histogram. We can also see that the intensities go from 0 to 255. This happens to be the range of values that fit in an 8 bit byte. This is the unit of memory size on modern computers. Is it correct to think that each pixel in this image is stored as a byte?
camera.dtype
The dtype
of a numpy array is the "data type" - the type for each individual element of the array. Above we see the answer is uint8
which means "unsigned integer, 8 bits". So, yes, each pixel is stored here as a byte.
If you look at the histogram, you can see that there is some part of the luminance space which has very few occurances, namely above a luminance value of about 220. We can probably make better use of the 0-255 dynamic range available.
In the cell below, enter a scale factor which fills the histogram of possible luminances more completely but does not cause too much clipping of the image values. The tower in the background should still be visible, for example. Look at the figures below to judge the effect of changing the scale factor. Put this in the variable scale_factor
.
# YOUR CODE HERE
raise NotImplementedError()
# assert that scale_factor is a number
assert scale_factor - 0.0 == scale_factor
Let's look at the histogram of rescaled luminances.
rescaled = np.clip(scale_factor*camera.astype(np.float32),0,255).astype(np.uint8)
plt.hist(rescaled.flat,bins=256);
plt.xlabel('luminance');
plt.ylabel('number of occurances');
plt.figure(figsize=(4, 4))
plt.imshow(rescaled, cmap='gray', interpolation='nearest', vmin=0, vmax=255)
plt.axis('off')
plt.tight_layout()
plt.show()