image restoration matlab code
276 views (last 30 days)
Show older comments
aziz alfares
on 2 Dec 2021
Commented: vaishnavi padhar
on 13 Apr 2022
hello i am trying to implement this code to get the result in figure 5.26(b) but still something wrong could you help me in that
thanks,
refernce: digital image processing gonzalez third edition
here is my code
c = im2double(imread('Fig0526(a)(original_DIP).tif'));
figure,imshow(c)
a = 0.1;
b = 0.1;
T=1;
[M, N] = size(c);
h = zeros(M,N);
for u = 1:M
for v = 1:N
h(u,v) = (T/(pi*(u*a + v*b)))*...
sin(pi*(u*a + v*b))*...
exp(-1i*pi*(u*a + v*b));
end
end
cf=(fft2(c));
c1 = cf.*h;
c1a=(ifft2(c1));
figure,imshow(abs(log(c1a)+1), []);
0 Comments
Accepted Answer
Image Analyst
on 2 Dec 2021
The origin was not at the right place. Try this:
clc; % Clear the command window.
fprintf('Beginning to run %s.m ...\n', mfilename);
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 = 15;
grayImage = im2double(imread('logo.tif'));
% Make sure it's 688 like the book says.
grayImage = imresize(grayImage, [688, 688]);
subplot(2, 3, 1);
imshow(grayImage, [])
impixelinfo
axis('on', 'image')
title('Original Image', 'FontSize', fontSize)
% Make a filter for the spectral domain.
a = 0.1;
b = a;
T=1;
[rows, columns] = size(grayImage);
h = zeros(rows, columns);
for u = 1 : rows
for v = 1 : columns
rx = v - columns/2;
ry = u - rows/2;
h(u,v) = (T/(pi*(ry*a + rx*b))) *...
sin(pi*(ry*a + rx*b))*...
exp(-1i*pi*(ry*a + rx*b));
end
end
% Print the max value of h.
absh = real(h);
fprintf('Max h = %f.\n', max(absh(:)))
subplot(2, 3, 2);
imshow(log(absh), []);
impixelinfo;
axis('on', 'image')
title('h Frequency Domain Filter', 'FontSize', fontSize)
% Compute fft of the image.
fftOrig = fftshift(fft2(grayImage));
subplot(2, 3, 3);
imshow(log(abs(fftOrig)), []);
impixelinfo;
axis('on', 'image')
title('FFT of original image', 'FontSize', fontSize)
% Multiply it by the filter. fftOrig and h have DC position at center of image.
c1 = fftOrig .* h;
% There are some nan's which mess up the inverse transform. Set nan's to zero.
rc = real(c1);
ic = imag(c1);
rc(isnan(rc)) = 0;
ic(isnan(ic)) = 0;
c1 = rc + 1i * ic;
subplot(2, 3, 4);
imshow(abs(log(c1)+1), []);
title('Filtered Spectrum', 'FontSize', fontSize)
% Inverse transform from frequency domain back to space domain.
% After multiplication, shift back.
c1Spatial = ifft2(ifftshift(c1));
subplot(2, 3, 5);
imshow(real(c1Spatial), []);
title('Filtered Image', 'FontSize', fontSize)
g = gcf;
g.WindowState = 'maximized'
2 Comments
More Answers (2)
See Also
Categories
Find more on Digital Filtering in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!