Exercise 1
This exersice is only partial, as it includes only the tasks with matlab code in the original exersice set, found here.
It assumes that you followed the install instructions in the python introduction, such that relevant packages are installed.
First, let us import some packages.
import cv2
import numpy as np
import matplotlib.pyplot as plt
Task 1
This python script reads a graylevel image (which can be found here),
and displays it. Notice that the value of the variable filename
must reflect where your image
mona.png
is located. It can be an absolute path like /absolute/path/to/lena.png
, or a relative
path ../../path/relative/to/your/current/location/lena.png
(the path style is assuming linux
or mac system).
filename = 'mona.png'
image = cv2.imread(filename, cv2.IMREAD_GRAYSCALE)
plt.imshow(image, cmap='gray')
The following snippet traverses every pixel in the image, and halves the grayscale values.
M, N = image.shape
half_image = np.zeros((M, N)) # Default type is 'float64'
for i in range(M): # Implicit from 0 (inclusive) to M (exclusive)
for j in range(N):
half_image[i, j] = 0.5*image[i, j]
Write a python script that reads the image ‘mona.png’ (found here, and computes the difference between values in neighbouring pixels. That is, if is the input image and is the result image, compute
for all feasible values $(x, y)$.
The image will be of type uint8
meaning that the data type of the value in each pixel is an
8-bit unsigned integer. This data type occupies 8 bit (1 byte), and ranges from 0 to 255
( values). The above computation could result in negative values, thus giving us type
overflow warnings (languages like python or matlab may silence these warnings and run anyway, but
the general advise is to pay attention to your types). Therefore, try to add a bias (e.g. 128) to
each pixel value in the resulting image.
Now, try to scale the difference image with a scalar value greater than 1. What happens to the contrast?
Task 4
The python script below loads and displays an image with in given amount of bits. Experiment by
changing the number of bits (controlled by the bit
variable).
image = cv2.imread('lena.png', cv2.IMREAD_GRAYSCALE)
plt.imshow(image, cmap='gray')
bit = 7 # Between 1 and 8
requantized_image = (image / (2**(8 - bit))).astype('uint8')
plt.imshow(requantized_image, cmap='gray')
How many quantization levels can you have before the visual quality is severely damaged?