%Image filtering example. This image shows the effects of lowpass ideal %and butterworth filtering of an image. This simulation shows how that lowpass %image filtering can be used to blur an image. Also, it shows how the image, %when an ideal filter is used, can introduce "Ringing" into the image. We show %how this problem can be eliminated using butterworth techniques. clear a = .5; %log dampening coefficient n = 1; %filter order D0 = 15; %pixel distance variable fid = fopen('c:/MATLABR11/bin/images/aero.pic','r'); %open the image file for reading aero = fread(fid); %read in the image. aero_matrix = display_image(aero,64,64,1); %convert binary image to a 64 X 64 matrix g_spect = fft2(aero_matrix); %compute 2D FFT g_spect_shft = fftshift(g_spect); %shift DC component to origin. g_spect_shft_log = log10(1 + a*(abs(g_spect_shft))); %compute log of FFT mp = linspace(0,1,5); mp = [mp' mp' mp']; figure(2),image(abs(g_spect_shft_log)),title('image of log fft aero.pic'); colormap(mp); %this section multiplies the ideal filter into the image. This has the effect of zeroing %out any frequency components outside the pixel distance D0. i_filter = zeros(64,64); for i=1:64 for j=1:64 distance = sqrt((i-32)^2 + (j-32)^2); if (distance <= D0) i_filter(i,j) = g_spect_shft(i,j); end end end figure(3),image(abs(i_filter)),title('image of filtered spectrum'); colormap(mp); i_g_spect_shft = ifftshift(i_filter); reconst = ifft2(i_g_spect_shft); %reconstruct the image from the filtered spectrum. mp = linspace(0,1,256); mp = [mp' mp' mp']; figure(4),image(abs(reconst)),title('reconstructed image from ideal filter'); colormap(mp); %This section now builds the butterworth image filter of order n. b_filter = zeros(64,64); for i=1:64 for j=1:64 distance = sqrt((i-32)^2 + (j-32)^2); b_filter(i,j) = 1/(1+((distance/D0)^2*n)); end end mp = linspace(0,1,5); mp = [mp' mp' mp']; figure(5),surf(b_filter),title('image of filter'); colormap(mp); n_image = b_filter .* g_spect_shft; %filter out any frequency components outside %the pixel distance D0 using the butterworth filter. i_g_spect_shft = ifftshift(n_image); reconst = ifft2(i_g_spect_shft); %reconstruct the image using the inverse 2d FFT. mp = linspace(0,1,256); mp = [mp' mp' mp']; figure(6),image(abs(reconst)),title('reconstructed image from butterworth filter'); colormap(mp);