Having trouble getting my gaussian filter to display filtered image.

I believe my issue is with image sizing but I can't seem to solve the issue I am having with outputting the filtered image. Any help would be greatly appreciated.
% Load the image and convert it to grayscale if necessary
img = imread('cameraman.tif');
if size(img1, 3) == 3
img = rgb2gray(img);
end
% Compute the size of the image
[m,n] = size(img);
% Define the standard deviation of the Gaussian filter
sigma = 10;
% Create the Gaussian low pass filter in the frequency domain
H = fspecial('gaussian',[m n],sigma);
% Apply the filter to the image in the frequency domain
Fimg = fft2(img);
Fimg = Fimg .* H;
% Transform the filtered image back to the spatial domain
filtered_img = ifft2(Fimg)
% Display the original and the filtered image
subplot(1, 2, 1);
imshow(img);
title('Original image');
subplot(1, 2, 2);
imshow(real(filtered_img));
title('Filtered image');

2 Comments

Please post your code in text form.
I have edited the post to include code in text format

Sign in to comment.

 Accepted Answer

% Load the image and convert it to grayscale if necessary
img = imread('cameraman.tif');
if size(img, 3) == 3
img = rgb2gray(img);
end
% Compute the size of the image
[m,n] = size(img);
% Define the standard deviation of the Gaussian filter
sigma = 10;
% Create the Gaussian low pass filter in the frequency domain
H = fspecial('gaussian',[m n],sigma);
% Apply the filter to the image in the frequency domain
Fimg = fft2(img);
Fimg = Fimg .* ifftshift(H);
% Transform the filtered image back to the spatial domain
filtered_img = ifft2(Fimg);
% Display the original and the filtered image
subplot(1, 2, 1);
imshow(img);
title('Original image');
subplot(1, 2, 2);
imshow(real(filtered_img),[]);
title('Filtered image');

More Answers (0)

Products

Release

R2021a

Community Treasure Hunt

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

Start Hunting!