Question
MATLAB Using MATLAB / Octave, finish the following question. 2.2 Spatial Linear Filtering (15%) Write a function that performs spatial filtering on a gray level
MATLAB
Using MATLAB / Octave, finish the following question.
2.2 Spatial Linear Filtering (15%)
Write a function that performs spatial filtering on a gray level image. The function prototype should be filter_spa(img_input, filter) -> img_result, where filter is the required filter, for example, filter can be [1,1,1;0,0,0;-1,-1,-1]. The function should be able to:
2.2.1 Smooth an image with different sizes of averaging filters, for example, 3 3, 5 5, 11 11 and so on. Please upload your smoothing image with 5 5 averaging filter, named img_ave.png. (5%)
2.2.2 Compute x-gradient, y-gradient of an image with Sobel operator. Please upload your x-gradient image and y-gradient image, named img_dx.png and img_dy.png respectively. (5%)
2.2.3 Sharpen an image with a 3 3 Laplacian filter. As for the Laplacian filters, you can choose any one of the four variants, as shown in the Reference. Please upload your result image, named img_sharpen.png. (5%)
Bonus (10%) The naive approach to convolute an image with a filter mask is to create two loops to retrieve each pixel and do the convolution. Please try to use at most one loop to replace the original two-loop image retrieval. You can take the LBP code (lbp.m) in Canvas as a reference for one-loop image retrieval.
2.3 Spatial Non-linear Filtering (10%)
Write a function that performs median filtering on a gray level image. The function prototype should be medfilt2d(img_input, size) -> img_result, where size is the window size of median filter, for example, size can be 3, 5, and so on. Please use your function to finish the following tasks (you can use the noise generator to generate Gaussian noise or salt-and-pepper noise, the code for noise generator is available in Canvas):
2.3.1 Add Gaussian noise to your input image with mean 0 and standard variance 30. Then try to use your median filter to denoise it. Please upload your noisy image and filtering result, named img_gaussian.png and med_gaussian.png respectively. (5%)
2.3.2 Add salt-and-pepper noise to your input image with the probabilities of the two noise components 0.3. Then try to use your median filter to denoise it. Please upload your noisy image and filtering result, named img_sp.png and med_sp.png respectively. (5%)
2.4 Discrete Fourier Transform (20%)
Write a function that performs 2-D Discrete Fourier Transform (DFT) or Inverse Discrete Fourier Transform (IDFT). The function prototype is dft_2d(img_input, flag) -> img_result, returning the DFT / IDFT result of the given input. flags is a parameter that specifies when the function should perform DFT (flag == DFT) and when it should perform IDFT (flag == IDFT). Please use your function to finish the following sub-tasks:
2.4.1 Perform DFT and compute the Fourier spectrum. Note that you should shift the values such that F(0,0) is at F(M/2, N/2), where M is the height of the input image and N is the width of the input image. Please upload the resulting spectrum image, named dft_spectrum.png. (10%)
2.4.2 Perform IDFT on the DFT result in 2.4.1. Note that the result of IDFT is a complex-value matrix and you need to transform it into a real-value matrix, for example, by computing the absolute values or simply collect the real part. (10%)
1) For better visualization of the Fourier spectrum, we can apply the function Hints: ()=log(+1)onthecomputedspectrum;
2) To shift the coordinate origin, we can multiply the input image (, ) by (1)+. Note: you need to include this part in your own DFT/IDFT function;
3) The formulas for DFT and IDFT are as follows,
2.5 Filtering in the Frequency Domain (10%)
Write a function that performs filtering in the frequency domain. The function prototype is filter2d_freq(img_input, filter) -> img_result, where filter is the required filter. According to Slide 23 in the Lecture 03: image-enhancement-freq, convolution/filtering in the spatial domain is equivalent to element-by-element multiplication in the frequency domain. Thus in this task, you are required to apply DFT to the given image and the given filter, and then multiply them element by element, followed by IDFT to get the filtered result. Hence, it should be easy to implement filter2d_freq based on dft_2d. Use your filter2d_freq to finish the following sub-tasks:
2.5.1 Smooth your input image with 5 5 averaging filter, named img_ave_freq.png. (5%)
2.5.2 Sharpen an image with a 3 3 Laplacian filter. As for the Laplacian filters, you can choose any one of the four variants, as shown in the Reference. Please upload your result image, named img_sharpen_freq.png. (5%)
2.6 HighFrequency Emphasis (35%)
Write a function that performs high-frequency emphasis in frequency domain. The function prototype should be high_freq_emphasis(img_input, a, b, type) -> img_result, where a, b are the parameters in the highfrequency Emphasis formulation and type indicates the filter used in frequency domain whose values are from {butterworth, gaussian}. You need to complete the high-pass Butterworth filter and high-pass Gaussian filter in butterworth.m and gaussian.m respectively. The function prototypes are butterworth(size, cutoff, n) -> f and gaussian(size, cutoff) -> f, where size is a 1 2 matrix indicating the size of the filter. Let = 0.1, which is the normalized cutoff frequency, and = 1. Please upload your result images filtered by Butterworth and Gaussian, named butter_emphasis_a_b.png and gaussian_emphasis_a_b.png respectively, where a and b are the actual values you used.
3. Reference
M-1N-1 MNXX f(x,y)e-j2 x=0 y=0 M-1 N-1 M-1N-1 MNXX f(x,y)e-j2 x=0 y=0 M-1 N-1Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started