Lab 3 - Instructions

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

5CCYB050: Signal and image processing

Lab 3: Image Filtering – Spatial Domain

1. 2D convolution. Let’s consider an image 𝑓(𝑥, 𝑦) with dimensions 𝑚 × 𝑛 and


convolution kernel 𝑤(𝑥, 𝑦) with dimensions (2𝑎 + 1) × (2𝑏 + 1). Then convolution
𝑤(𝑥, 𝑦) ∗ 𝑓(𝑥, 𝑦) results in a new image 𝑔(𝑥, 𝑦) defined by equation
𝑎 𝑏

𝑔(𝑥, 𝑦) = ∑ ∑ 𝑤(𝑠, 𝑡)𝑓(𝑥 − 𝑠, 𝑦 − 𝑡)


𝑠=−𝑎 𝑡=−𝑏
The most common type of convolution is Gaussian, also called Gaussian blurring. In this
exercise we implement Gaussian convolution using MATLAB commands.
a. Create image 𝑓(𝑥, 𝑦) using f=checkerboard(50) and display. What are the
dimensions 𝑚 and 𝑛?
b. Create Gaussian kernel 𝑤(𝑥, 𝑦) with size 41 × 41 and standard deviation 5. Use
command fspecial and check help to find how to input the parameters. Display
the kernel w. What do the values 𝑎 and 𝑏 from the definition correspond to?
c. Now blur the image f using command
g = imfilter(f,w,'full')
and display. Can you see the blurring effect? What is the size of the blurred image
g? Why is that? Carefully check the edges – can you see the intensity drop-off
caused by the zero-padding?
d. If no parameter is set, imfilter will assume that image is zero-padded. Now set
the fourth parameter of imfilter to 'circular'. Can you see the periodic
behaviour of convolution?
Note: The above approach is better for the purpose of this tutorial, but now it is
recommended to use newer command imgaussfilt instead. Command imfilter
with no additional parameters will produce output of the same size as the original
image.

2. Low pass filters and image denoising. Load image “brain_pet.tif” into variable brain
and display. You can observe that the image is noisy. In this exercise we will compare
different denoising filters.
a. Use command fspecial to create three different denoising filters. Check help to
create these filters with size 3 × 3:
• arithmetic averaging filter
• weighted averaging filter – Gaussian, stdev=1
• weighted averaging filter – Gaussian, stdev=0.5
Display these filters in one figure using subplot, setting intensity range to
[0,0.5]. Can you predict which filter will be most effective in reducing noise and
which will result in most blurring?
b. Blur the image brain with the kernels from the previous exercise using
command imfilter. Additionally, also apply median filter using Matlab function
medfilt2. Which filter was most effective in reducing noise and preserving
edges?

3. High pass filters, gradients and image sharpening.


a. Calculate first partial derivatives (gradients 𝑔𝑥 and 𝑔𝑦 ) of the image brain from
question 2 using Sobel filter. Create Sobel filter kernel sy using function
fspecial and sx as transpose of sy. Filter the image brain using imfilter to
produce gradients in 𝑥 and 𝑦. Display the filter kernels and the gradient images
in one figure using subplot.

b. Calculate gradient magnitude given by equation 𝑀 = √𝑔𝑥2 + 𝑔𝑦2 and display. Does
it highlight well the edges in the image?
c. Create the Laplacian filter using command
l = fspecial('laplacian');
To check which type of the Laplacian filter Matlab created, write on command
line l*6. Is the central value of the filter positive or negative?
d. Filter the brain using l. To produce sharpening effect, enhance the image by
taking away the result of Laplacian filtering from the original image (remember
from the lecture that if the central point of the Laplacian filter is negative, we
need to take away the result of Laplacian filtering, rather than add). Display the
original image, the result of Laplacian filtering and the enhanced image in one
figure. Set the intensity range for the enhanced image to [0,255] (this is because
there are negative values). What can you say about this enhancement?
e. Now instead of enhancing the original image, enhance the image blurred by
Gaussian with standard deviation 1, that you created in question (2b). Display
the original image, the blurred image, and the sharpened blurred image. Did we
manage to reduce blurring while preserving the denoising effect of Gaussian
filter? Note: Laplacian of Gaussian is a commonly used filter for estimation of
Laplacian while avoiding noise enhancement.
f. Another type of image sharpening method is called unsharp masking. It is
performed in three steps:
• blur the image 𝑓 to obtain 𝑓 ̅
• subtract the blurred image from original to obtain the mask
𝑔mask = 𝑓 − 𝑓 ̅
• add the mask to the original image to create the enhanced image
𝑔 = 𝑓 + 𝑔mask
Compare unsharp masking to enhancing using Laplacian operator. Which one
has more noise enhancing effect?

You might also like