Description
Question 1: Write a function that performs contrast stretching. Read the given gray-scale
image and apply your function to test whether it indeed improves the image quality.
The lab files should be submitted online.
Instructions for submission will be posted closer to the deadline.
Intensity Histogram
The histogram of an image shows the frequency of pixel intensity values. It gives statistical
information and removes the location information of the pixels. For a digital image with gray
levels from 0 to πΏ β 1, the histogram is a discrete function β(ππ
) = ππ where ππ β [0, πΏ β 1]
is the kth gray level and ππ is the number of pixels with gray level ππ.
Question 2: Write a function that computes and plots the histogram of the given image.
Image Edges
Edges are an important source of semantic information in images and they occur in human
visual perception at divisions between different areas of intensity, colour, and texture. A grayscale image can be thought of as a 2D landscape with areas of different intensity living at
different heights. A transition between areas of different intensity in an image πΌ means there
must be a steep slope which we formalise as the gradient:
βπΌ = (
ππΌ
ππ₯ ,
ππΌ
ππ¦)
Since the image πΌ is discrete, we need to approximate the continuous derivatives ππΌ/ππ₯ and
ππΌ/ππ¦ by finite differences. Simple examples of convolution kernels that perform finite
differencing are the Sobel filters defined as follows:
ππ₯ = [
β1 0 1
β2 0 2
β1 0 1
] and ππ¦ = [
β1 β2 β1
0 0 0
1 2 1
]
Question 3: Write a function that computes the two gradient images ππΌ/ππ₯ β πΌ β ππ₯ and
ππΌ/ππ¦ β πΌ β ππ¦ from the given image.
Notice that the OpenCV built-in Sobel functions can also be applied to achieve this result. But
the challenge is to implement your own functions. You can verify the result of your own
functions by comparing with the result of the built-in functions.
Image Sharpening
Sometimes images are not as sharp as they could be, and fine details look a bit blurred. An
image can be sharpened by using specific filters that enhance the high-frequency content in
the image. One possible technique to accomplish this is called unsharp masking.
The figure below gives a schematic overview of this technique. In words, it takes the input
image πΌ and convolves it with a Gaussian kernel πΊ with standard deviation or scale π,
resulting in a slightly blurred image πΏ. Next, the blurred image πΏ is subtracted from the input
image πΌ, resulting in image π», in which the high frequencies are enhanced. Then, each pixel
in π» is multiplied by a constant factor π, and the resulting image is added pixel-wise to the
input image πΌ, resulting in output image π.
Question 4 (2.5 marks): Write a function that implements the above technique. Apply it to
the given image using, for example, π = 1.0 pixels and π = 1.25. The differences between πΌ
and π are subtle but the latter should be visibly sharper.
Notice that this technique may produce output pixel values outside the range [0,255]. Thus,
make sure you use the right data types for the computations. Also, you can apply contrast
stretching (which you implemented in answer to Question 1) to force the contrast in πΌ and π
to be in the same range [0,255], so you can properly compare the two images visually.

