%This script shows how to perform linear filtering directly in the Fourier domain.
%By Li Jiechu 1252853, codes for homework 2 ,programming 1.
%imread an image , use rgb2gray if it is necessary
Image = imread('C:\Users\asus\Desktop\LIHYU作业\homework2\baby.jpg');
%Image = rgb2gray(Image);
[rows,colums] = size(Image);
%Choose test mode:
% default:centered the image and surrounded it by a border of zeros
mode = 1;
% Image padding:the Image locates in the Top,Left Corner with a border of zeros surrounded
% mode = 2;
%Step1: image padding
if(2 == mode)
ImagePadded = padarray(Image,[rows,colums],0,'post'); %put the image on left,top corner
else
ImagePadded = padarray(Image,[rows/2,colums/2],0,'both');%put the image on center
end
figure(1);
subplot(2,2,1),imshow(Image),title('Original Picture');
if(2 == mode)
subplot(2,2,2),imshow(ImagePadded),title('Image Padded in Corner');
else
subplot(2,2,2),imshow(ImagePadded),title('Image Padded in Center');
end
[m,n]=size(ImagePadded);
% Step 2: Multiply fp(x,y) by (-1)^(x+y) to center its transform
% [m,n]=size(ImagePadded);
% for i = 1:m
% for j = 1:n
% ImagePadded(i,j) = ImagePadded(i,j)*((-1)^(i+j));
% end
% end
% Step 3: Discrete Fourier Transform,DFT
J=fftshift(fft2(ImagePadded));
subplot(2,2,3),imshow(log(abs(J)),[]),title('frequency spectrum');
% Step 4: construct a low-pass filter in the Fourier domain
% Let D0 = 50,We can change it here,Ideal Low-Pass filter;
D0=50;
result = zeros(m,n);
rows_center = fix(m/2);
cols_center = fix(n/2);
for i = 1:m
for j = 1:n
d = sqrt((i-rows_center)^2+(j-cols_center)^2);
if(d<=D0 )
D = 1;
else
D = 0;
end
result(i,j) = J(i,j)* D;
end
end
% Step 5: go back to the spatial domain
result = ifftshift(result);
J2 = ifft2(result);
J3 = uint8(real(J2));
% Step 6:
% for i = 1:m
% for j = 1:n
% J3(i,j) = J3(i,j)*((-1)^(i+j));
% end
% end
%extract the final processed result
if(2 == mode)
subplot(2,2,4),imshow(J3(1:rows,1:colums)),title('Final Result');
else
subplot(2,2,4),imshow(J3(rows/2:3*rows/2,colums/2:3*colums/2)),title('Final Result');
end
% -----------------------------------------------------------------------
% The following are some notes I take in this program. |
% -----------------------------------------------------------------------
% -----------------------------------------------------------------------
% 1.About padarray |
% -----------------------------------------------------------------------
% B = padarray(A,PADSIZE,PADVAL,DIRECTION) pads A in the direction;
% String values for DIRECTION
% 'pre' Pads before the first array element along each dimension .
%
% 'post' Pads after the last array element along each dimension.
%
% 'both' Pads before the first array element and after the
% last array element along each dimension.
% -----------------------------------------------------------------------
% 2.My own padarray |
% -----------------------------------------------------------------------
%I also write my personal padarray function and test it after.
%Just see the mypadarray.m and TestMyPadaray.m including in my homework
% -----------------------------------------------------------------------
数字图像处理之作业2
最新推荐文章于 2021-09-20 16:42:31 发布