Sie sind auf Seite 1von 10

Bo co X

l nh v m
thanh
ti 19: Image transform

GVHD: V TRUNG DNG


Thnh vin nhm :
H tn:

MSSV:

Nguyn Cng Bnh

41000222

Nguyn Vit Cng

41000374

Nguyn Thnh Nguyn 41002..


T L Hong Sn

4100

Nguyn Vn Thin

4100

ti 19:
Image Transform:
- read an original image named lena512.tif
- find the Fourier transform of the image. Hint: use fftshift and fft2
- plot the log magnitude of the 2D DFT of the grayscale image, with center
shifted.
- design and apply the truncation windows discussed in the class to keep 25%,
12.5%, and 6.25% of the DFT coefficients.
- apply the 2D inverse DFT to reconstruct the image for each of the truncated
spectra.
- plot the reconstructed images.
- compute the PSNR value for each of the reconstructed images.

Code cho ton bi:


function baitaplon_xu_ly_anh
%%%%%%%% nhap anh va FFT
I = imread('C:\Users\WIN8\Desktop\lena512.tif');
imshow(I);
figure;
FF = fft2(I);
imagesc(100*log(1+abs(FF)));
colormap(gray);
figure;
imagesc(angle(FF));
colormap(gray);
figure;
%%%%%%%%%%%%%%%%%%%%%%%%%%
imagesc(100*log(1+abs(fftshift(FF))));
colormap(gray);
figure;
imagesc(angle(FF));
colormap(gray);
figure;
%%%%%%%%%%%%%%%%%%tao bo loc thong thap
cutoff = 0.159;
cutoff = 0.159 ly 25% h s DFT
%%%%%%%%%%%%%
0.079577 ly 12.5% h s DFT
%%%%%%%%%%%%%% 0.039788 ly 6.25% h s DFT
n = 1000000;
if cutoff < 0 | cutoff > 0.5
error('cutoff frequency must be between 0 and 0.5');
end
if rem(n,1) ~= 0 | n < 1
error('n must be an integer >= 1');
end
rows = 512;
cols = 512;
x = (ones(rows,1) * [1:cols] - (fix(cols/2)+1))/cols;
y = ([1:rows]' * ones(1,cols) - (fix(rows/2)+1))/rows;
radius = sqrt(x.^2 + y.^2);
f = 1 ./ (1.0 + (radius ./ cutoff).^(2*n));
%%%%%%%%%%%%%%%%%%%%%%%%%%%% nhan trong mien tan so
FFcen = fftshift(FF);
FFf = FFcen .* f;
%%%%%%%%%%%%%%%%%%%%%%%% ve lai pho sau khi loc
imagesc(100*log(1+abs(FFf)));
colormap(gray);

figure;
imagesc(angle(FFf));
colormap(gray);
figure;
%%%%%%%%%%%%%%%%%%%%% khoi phuc anh
reFFf = fftshift(FFf);
J = ifft2(reFFf);
J = uint8(J);
imshow(J)
%%%%%%%%%tinh PSNR
S = 0;
for i = drange(1:512)
for j = drange(1:512)
S = S+ (I(i,j)-J(i,j))^2;
end;
end;
S = double(S);
MSE = S/(512*512);
MAXi = 245;
A = (MAXi*MAXi)/MSE;
PSNR = 10*log(A)

read an original image named lena512.tif

code :
I = imread('C:\Users\WIN8\Desktop\lena512.tif');
imshow(I);

find the Fourier transform of the image.


Code :
FF = fft2(I);
imagesc(100*log(1+abs(FF)));
colormap(gray);
figure;
imagesc(angle(FF));
colormap(gray);

plot the log magnitude of the 2D DFT of the grayscale image, with center
shifted.
Code:
imagesc(100*log(1+abs(fftshift(FF))));
colormap(gray);
figure;
imagesc(angle(FF));
colormap(gray);
figure;

design and apply the truncation windows discussed in the class to keep 25%,
12.5%, and 6.25% of the DFT coefficients.
Code :
cutoff = 0.039788;%%%%%%%%% cutoff = 0.159 ly 25% h s DFT
%%%%%%%%%%%%%
0.079577 ly 12.5% h s DFT
%%%%%%%%%%%%%% 0.039788 ly 6.25% h s DFT
n = 1000000;
if cutoff < 0 | cutoff > 0.5
error('cutoff frequency must be between 0 and 0.5');
end
if rem(n,1) ~= 0 | n < 1
error('n must be an integer >= 1');
end
rows = 512;
cols = 512;
x = (ones(rows,1) * [1:cols] - (fix(cols/2)+1))/cols;
y = ([1:rows]' * ones(1,cols) - (fix(rows/2)+1))/rows;
radius = sqrt(x.^2 + y.^2);
f = 1 ./ (1.0 + (radius ./ cutoff).^(2*n));
imshow(f)

apply the 2D inverse DFT to reconstruct the image for each of the truncated
spectra.
plot the reconstructed images.
compute the PSNR value for each of the reconstructed images.
Code:
%%%%%%%%%%%%%%%%%%%%%%%%%%%% nhan trong mien tan so
FFcen = fftshift(FF);
FFf = FFcen .* f;
%%%%%%%%%%%%%%%%%%%%%%%% ve lai pho sau khi loc
imagesc(100*log(1+abs(FFf)));
colormap(gray);
figure;
imagesc(angle(FFf));
colormap(gray);
figure;
%%%%%%%%%%%%%%%%%%%%% khoi phuc anh
reFFf = fftshift(FFf);
J = ifft2(reFFf);
J = uint8(J);
imshow(J)
%%%%%%%%%tinh PSNR
S = 0;
for i = drange(1:512)
for j = drange(1:512)
S = S+ (I(i,j)-J(i,j))^2;
end;
end;
S = double(S);
MSE = S/(512*512);
MAXi = 245;
A = (MAXi*MAXi)/MSE;
PSNR = 10*log(A)

PSNR = 179.3790

PSNR = 179.3790

PSNR = 179.3790

Das könnte Ihnen auch gefallen