Clear Filters
Clear Filters

How to implement Gaussian filter spectrum?

8 views (last 30 days)
I have a problem in the code. I want to plot the spectrum of the Gaussian filter but I got wrong spectrum. Can you please help me to correct the following code :
I=imread('cameraman.tif');
figure(1);
original=imshow(I)
title('original image');
SP = imnoise(I,'salt & pepper',0.1);
figure(2);
imshow(SP)
title('salt & pepper noise');
h=fspecial('gaussian',[256 256] ,10/256);
result = imfilter(SP,h,'replicate');
figure(3)
imshow(result);
title('Result sigma=10 ');
figure(4)
H = abs(fft2(h));
mesh(x,y,(H))

Accepted Answer

Image Analyst
Image Analyst on 1 May 2021
Try this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 14;
% Read in original image.
grayImage=imread('cameraman.tif');
nexttile;
imshow(grayImage);
title('Original image');
drawnow;
% Add noise
SP = imnoise(grayImage,'salt & pepper',0.1);
nexttile;
imshow(SP)
title('With salt & pepper noise');
drawnow;
% Create blurring filter
h = fspecial('gaussian',[256 256] ,10/256);
nexttile;
imshow(h, []);
title('Gaussian Filter h');
% Blur the image.
result = imfilter(SP, h, 'replicate');
nexttile;
imshow(result);
title('Result sigma=10 ');
drawnow;
% Get the spectrum of the Gaussian blurring filter.
H = fftshift(abs(fft2(h)));
[rows, columns] = size(H);
[x, y] = meshgrid(1:columns, 1:rows);
nexttile;
surf(x,y,H, 'EdgeColor', 'none');
title('Spectrum of blurring filter.');
fprintf('Done running %s.m ...\n', mfilename);
  1 Comment
Mahabba Almheiri
Mahabba Almheiri on 3 May 2021
Thank you so much I appreciate your effort. The code is working

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 30 Apr 2021
Did you try imgaussfilt()?
  2 Comments
Mahabba Almheiri
Mahabba Almheiri on 30 Apr 2021
Yes I Im just stuck with plotting the 3D spectrum of the gaussian filter. Could you please help me in this part and thank you
DGM
DGM on 30 Apr 2021
Edited: DGM on 30 Apr 2021
I'm guessing you probably want to center the spectrum with fftshift()
Your x and y are undefined, so you'll either have to define them or just let them be implicit.
figure(4)
H = abs(fftshift(fft2(h)));
mesh(H)
I also don't know why you need [256 256] worth of support for a filter with a sigma of 0.0392

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!