Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

UNIVERSITY OF ENGINEERING AND

TECHNOLOGY, TAXILA

ELECTRICAL ENGINEERING
DEPARTMENT

DIP LAB MANUAL 07

SUBMITTED BY:
Rabeah Farrakh

20-EE-02

SUBMITTED TO:

Sir Junaid Mir


Lab-07: Spatial Filtering (Part-I)

OBJECTIVE:
To implement and understand the use of spatial filters for image processing.
LEARNING TASKS:
Download chapter 3 images of the Gonzalez DIP 3rd edition book from the link given below.
https://www.imageprocessingplace.com/DIP-3E/dip3e_book_images_downloads.htm

1. Read and view a greyscale image of your choice in MATLAB.


a. A 3 × 3 filter 𝑤 is defined below. What will happen, when you convolve your
image with this filter? Comprehend the operation of 2D convolution to answer
this question.
0 0 0
𝑤 = [0 1 0]
0 0 0

b. Filter the image with the filter 𝑤 defined above using the imfilter command.
View the filtered image and verify your response to part-a. While using
imfilter command, try to understand the boundary options available with this
command.

c. A 3 × 3 filter 𝑤 defined below shifts the image towards right one pixel unit when
applied on the image. Try this kernel and explain why this happens? Write down
a filter kernel which would shift the image towards left by 3-pixel units.

0 0 0
𝑤 = [0 0 1]
0 0 0

d. Write a code in MATLAB based on filters to shift the image 𝑛 −pixel units
towards left or right based on the user inputs. AVOID hardcoding.

MATLAB CODE:
img=imread('kidney.tif');
inp = input('enter value: ');
z = zeros((n*2)+1);
z(round(length(z)/2),length(z))=1;
f=imfilter(img,z);
imshow(f)
title('Output Image')
suptitle('20-EE-02')
2. Read and view Fig0333(a)(test_pattern_blurring_orig).tif in MATLAB.

1 1 1 1 1 1 2 1
𝑤1 = × [1 1 1], 𝑤2 = × [2 4 2]
9 16
1 1 1 1 2 1
a. Filter kernel 𝑤1 is a 3 × 3 smoothing/averaging box (square) filter. Why is it
called a smoothing or averaging filter? Think, how the filtered output image will
be different visually when the input image is convolved with this filter?

b. Create the 𝑤1 kernel and apply the filter using the imfilter command. View
the filtered image. Is the smoothing filter a LPF or HPF?

c. Write down the kernel for a 5 × 5 averaging box filter. Verify your answer by
creating a 5 × 5 averaging kernel using the fspecial command.

d. Create a 3 × 2 subplot grid to show the original image and the filtered images
with averaging box filter of sizes 5, 9, 15, 35 and 51. Use the fspecial
command to create filters of different sizes. What happens to the smoothing
operation when the filter kernel size is increased?
e. Filter kernel 𝑤2 is one representation of a 3 × 3 weighted smoothing/averaging
filter also called the Gaussian smoothing filter. Which pixel value is given the
highest weight in this kernel? Repeat part-d using the Gaussian filter. How the
standard deviation parameter in Gaussian filter impact the weights of the kernel?
f. Create a 1 × 3 subplot grid to show the original image and the filtered images
with averaging box filter and the Gaussian filter of size 15. Compare the output
images.

3. Read and view Fig0334(a)(hubble-original).tif in MATLAB, which is an image from the


Hubble telescope in orbit around the Earth. An important application of spatial averaging
is to blur an image for the purpose of getting a gross representation of objects of interest,
such that the intensity of smaller objects blends with the background and larger objects
become “bloblike” and easy to detect. The size of the mask establishes the relative size of
the objects that will be blended with the background.
a. Apply a 15 × 15 averaging box filter to the image and view the filtered image.
What happens to the small stars in an image?

b. Binarize the filtered image. Let’s set the threshold value equal to the 25% of the
highest intensity value in the filtered image. View the resulting mask image.
c. Multiply the mask image with the original image and view the resulting image.
Record your observations.

4. Read and view Fig0335(a)(ckt_board_saltpep_prob_pt05).tif in MATLAB.


a. Can you identify the type of noise in the image? Is this a linear or non-linear
noise? Add the same noise in an image of your choice using the imnoise
command.
b. Try to remove the noise using spatial averaging or gaussian smoothing filter. Use
different kernel sizes. Can you remove the noise?

c. Use the order-statistic (non-linear) filters to remove the noise. Use max, min and
median filters using the ordfilt2 command. View the filtered images from these
three filters in a 1 × 3 subplot grid.

d. Which filter is most suitable for removing the noise. Justify your observations.

You might also like