% freq.m % demonstrating 2D filtering effects % copyright(c) 1997 by Yu Hen Hu % created: 10/14/97 % clear all,clf echo on % % 1. load a gray scale image [xi,ind]=bmpread('p64.bmp'); [nh,nv]=size(xi); % size of the image figure(1),imshow(xi,ind),title('original image') x=ind2gray(xi,ind); % % 2. spatial low pass filtering using different sizes masks ns=input('input low pass filter mask size (odd number): '); % % The mask is: B=ones(ns,ns)/(ns*ns) % Now filter the image using 2d fitlering command "filter2" y=filter2(B,x); figure(2),imagesc(y),colormap('gray'),title('low passed image') % Note that (1) the boundary smears; (2) there are layer edges pause % 3. spatial high pass filtering using default masks (figure 4.24) % % The mask is: B=[-1 -1 -1;-1 8 -1;-1 -1 -1]/9; % Now filter the image using 2d fitlering command "filter2" y=filter2(B,x); figure(2),imagesc(y),colormap('gray'),title('high passed image') % Note that (1) the boundary smears; (2) there are layer edges pause % 4. spatial high boost filtering using selected weigting (figure 4.26) % A=input('input the value of A (>1) :'); % The mask is: B=[-1 -1 -1;-1 9*A-1 -1;-1 -1 -1]/9; % Now filter the image using 2d fitlering command "filter2" y=filter2(B,x); figure(2),imagesc(y),colormap('gray'),title('high passed image') % Note that (1) the boundary smears; (2) there are layer edges pause % 5. derivative filtering (Laplacian) using Prewitt operators % The two masks are: B=[-1 -1 -1;0 0 0 ;1 1 1]; C=[-1 0 1; -1 0 1; -1 0 1]; % Now filter the image using 2d fitlering command "filter2" y=abs(filter2(B,x))+abs(filter2(C,x)); figure(2),imagesc(y),colormap('gray'),title('high passed image') % Note that (1) the boundary smears; (2) there are layer edges pause % 6. Medium filter , Gaussian noise % Let us begin by adding gaussian noise with 0 mean, 0.01 var % y=imnoise(x,'gaussian'); figure(1),imshow(y),colormap('gray'),title('add gaussian noise') % we then use two methods to remove the noise: low pass and medium filter % First use low pass 3x3 mask figure(2),imshow(filter2(ones(3,3)/9,y)),colormap('gray'),title('low pass fitlering') figure(3),imshow(medfilt2(y)),colormap('gray'),title('medium filtered') pause % 7. Medium filter, impusive noise % Let us begin by adding gaussian noise with 0 mean, 0.01 var % y=imnoise(x,'salt & pepper'); figure(1),imshow(y),colormap('gray'),title('add gaussian noise') % we then use two methods to remove the noise: low pass and medium filter % First use low pass 3x3 mask figure(2),imshow(filter2(ones(3,3)/9,y)),colormap('gray'),title('low pass fitlering') figure(3),imshow(medfilt2(y)),colormap('gray'),title('medium filtered')