$35
COMP9517: Computer Vision
T2 Lab 1 Specification
Maximum Marks Achievable: 2.5
This lab is worth 2.5% of the total course marks.
The lab files should be submitted online.
Objective: This lab revisits important concepts covered in the lectures of Weeks 1 and 2 and aims to make you familiar with implementing specific algorithms.
Materials: The sample image to be used in all the questions of this lab is available in WebCMS3. You are required to use OpenCV 3+ with Python 3+. Jupyter notebook files are preferred for submitting your code.
Submission: The last question (Question 4) is assessable after the lab. Submit your code and results in a zip file via WebCMS3 by the above deadline.
Contrast Stretching
Contrast in an image is a measure of the range of intensity values and is the difference between the maximum and minimum pixel values occurring within the image. The full contrast of an 8-bit image is 255 (max) – 0 (min) = 255. Anything less than that means the image has lower contrast than possible. Contrast stretching attempts to improve the contrast of the image by stretching the range of intensity values using linear scaling.
Assume that 𝐼 is the original input image and 𝑂 is the transformed output image. Let 𝑎 and 𝑏 be the minimum and maximum pixel values allowed (for an 8-bit image that means 𝑎 = 0 and 𝑏 = 255). And let 𝑐 and 𝑑 be the minimum and maximum pixel values occurring in 𝐼. Then the contrast stretched image 𝑂 is given by the function:
𝑏 − 𝑎
𝑂 = (𝐼 − 𝑐) ( ) + 𝑎
𝑑 − 𝑐
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.
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
𝑆𝑥 = [−2 −1
0
0
0
1 −1
2] and 𝑆𝑦 = [ 0
1 1
−2
0
2
−1
0]
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.