AUTO-FOCUS BANDWIDTH USING PSD ESTIMATION EFFECTIVE By Laurence G. Hassebrook 2-22-2012 Please follow the tutorial and reproduce the figures with your own code. We demonstrate how to use the effective bandwidth of the PSD estimate to determine the image that is most in focus. The first part of this visualization uses control noise generated from a white Gaussian noise image filtered by a Gaussian function to simulate 2-D blurring. Since the noise is stationary we use the rows of the colored noise to estimate a 1-D PSD of the blurred noise image. From this we estimate an effective bandwidth [1] by first removing dc, then taking the peak value of the PSD and forming a rectangle that is equal in area as the PSD estimate. The rectangle height is equal to the peak PSD value and the width yields the effective bandwidth. The image with the maximum effective bandwidth is considered to be most in focus. The second part of the tutorial uses real data that is blurred by repositioning the target under a microscope. The same algorithm is applied except we use a second type of effective bandwidth definition [2] better suited to signals with a fast frequency drop off. In this second case we find the bandwidth that contains 98% of the total area of the estimated PSD. We chose 98% because that is what is commonly used in “Carsons rule” to define the bandwidth of wideband systems having a fast drop off. 1. AUTOFOCUS SIMULATION Form an input noise image from White Gaussian Noise. clear all; % dimensions of image Nx=512; My=Nx; % form the control noise image from Gaussian distribution w=randn(My,Nx); W=fft2(w); % figure(1) imagesc(w) colormap gray; axis image; axis off; title('Input White Noise Pattern') xlabel('Spatial X dim (pixels)'); ylabel('Spatial Y dim (pixels)'); Color Noise Synthesis Page 1 Figure 1: Gaussian white noise input. Estimate the PSD by using each row as a separate “run” of the data. We can do this because the data is stationary in all directions. % estimate PSD along 1 dimension of the input noise pattern by averaging % the PSD of each row Wy=zeros(1,Nx); wy=zeros(1,Nx); for m=1:My wy(1:Nx)=w(m,1:Nx); Wy=Wy+abs(fft(wy)).^2; end; Wy=Wy/My; Then estimate an effective bandwith based on the maximum value and total area of the PSD estimate such that: % Determine equivalent ideal lowpass E=sum(Wy); Amax=max(Wy); B2=floor(E./(2*Amax)); B2=2*B2+1; if B2>Nx B2=Nx; end; Heff=Amax*irect(1,B2,1,Nx); figure(2) k=1:Nx; plot(k,Wy,k,Heff) axis([1,Nx,0,1.1*max(Wy)]); title('Estimated White Noise PSD') xlabel('Discrete Frequency'); Color Noise Synthesis Page 2 Figure 2: Effective Bandwidth and PSD estimate of Fig. 1. Now loop through a range of blurring by varying sigma of a Gaussian function based filter. Each time, estimate the PSD and effective bandwidth. Keep track of the largest bandwidth which is an indicator of which image is the most in focus. % Simulate blurring process nmax=10; deltadev=60 Nindex=floor(2*nmax+1); EffBW=zeros(1,Nindex); sigmaAll=zeros(1,Nindex); EffBWindex=0; EffBWmax=0; istore=0; for n=-nmax:nmax % -nmax <n1< n1=floor(abs(nmax-(abs(n)-1))); sigma=n1*deltadev; sigmaAll(n+nmax+1)=sigma; Hblurr=igauss(sigma,sigma,My,Nx); wcolor=real(ifft2(Hblurr.*W)); % the PSD of each row Wy=zeros(1,Nx); wy=zeros(1,Nx); for m=1:My wy(1:Nx)=wcolor(m,1:Nx); Wy=Wy+abs(fft(wy)).^2; end; Wy=Wy/My; % Determine equivalent ideal lowpass E=sum(Wy); Amax=max(Wy); B2=E./(2*Amax); B2=floor(B2); B2=2*B2+1; Color Noise Synthesis Page 3 if B2>Nx B2=Nx; end; Heff=Amax*irect(1,B2,1,Nx); % store effective bandwidth EffBW(n+nmax+1)=B2; if B2>EffBWmax EffBWmax=B2; EffBWindex=n+nmax+1; istore=1; end; Notice how we store only the best so far of the color filter, blurred image and the PSD by using the istore variable. figure(3) imagesc(Hblurr) colormap gray; title('Blurring Filter') xlabel('DT Frequency (pixels)'); ylabel('DT Frequency (pixels)'); if istore==1 print -djpeg fig3 end; Figure 3: Of the range of sigma used, this transfer function gave the least blurring. % figure(4) imagesc(wcolor) colormap gray; title('Output Colored Noise') xlabel('Spatial X dim (pixels)'); ylabel('Spatial Y dim (pixels)'); if istore==1 Color Noise Synthesis Page 4 print -djpeg fig4 end; Figure 4: Least blurred noise image. Figure 5: Least blurred noise PSD estimate. % figure(5) plot(k,Wy,k,Heff) axis([1,Nx,0,1.1*max(Wy)]); title('Estimated Colored Noise PSD and Eff. BW') xlabel('Discrete Frequency'); ylabel('PSD'); if istore==1 print -djpeg fig5 istore=0; end; end; Color Noise Synthesis Page 5 Figure 6: Sigma and Eff. Bandwidth correspondence. In Fig. 6 we show how the effective bandwidth and sigma varied with image index. As expected, the larger the sigma, the larger the effective bandwidth. Bw=1:Nindex; figure(6); plot(Bw,EffBW,Bw,sigmaAll); title('Effective Bandwidth and Sigma') xlabel('Image Index'); ylabel('Eff. Bandwidth and sigma (pixels)'); legend('Eff. Bandwidth','sigma'); 2. EXPERIMENTAL AUTOFOCUS Download the set of test images. The images were blurred by changing their distance from the camera lens using a Z stage adjustment. This particular type of Z stage also introduced a change in Y position but the technique is relatively invariant to position changes because it uses a PSD estimate. %% REAL DATA Pathname='ee640data' % path or folder name in reference to your default path Filename='autofocusdataB_' % name of files not including the index or suffix Filesuffix='jpg' % suffix or image type % get size of images index=0; Fullname=sprintf('%s%c%s%d%c%s',Pathname,'\',Filename,index,'.',Filesuffix) A_bmp=double(imread(Fullname)); % load pattern#.bmp [My, Nx, Pz] =size(A_bmp); Nindex=20; k=1:Nx; EffBW=zeros(1,Nindex); Color Noise Synthesis Page 6 EffBWindex=0; EffBWmax=0; istore=0; Figure 7: Blurred and in focus sample images from data set. The image names were indexed to allow automated input of each image. Note that we also zeroed out the dc component. for index=0:(Nindex-1) Fullname=sprintf('%s%c%s%d%c%s',Pathname,'\',Filename,index,'.',Filesuffix) A_bmp=double(imread(Fullname)); % load pattern#.bmp [My, Nx, Pz] =size(A_bmp); Ar=A_bmp(:,:,1); Ag=A_bmp(:,:,2); Ab=A_bmp(:,:,3); Abw=Ar+Ag+Ab; Abw=Abw/max(max(Abw)); % the PSD of each row Wy=zeros(1,Nx); wy=zeros(1,Nx); for m=1:My wy(1:Nx)=Abw(m,1:Nx); Wyfft=abs(fft(wy)); % zero out dc; Wyfft(1)=0+i*0; Wy=Wy+abs(Wyfft).^2; end; Wy=Wy/My; Amax=max(Wy); An accumulated PSD area is formed so that the effective bandwidth is found by correspondence with 98% of the PSD area. Note that we only use half the PSD vector for this due to symmetry. % form accumulated energy Nx2=Nx/2; Color Noise Synthesis Page 7 Eaccum=zeros(1,Nx2); for n=1:Nx2 for m=1:n Eaccum(n)=Eaccum(n)+Wy(m); end; end; % Determine equivalent ideal lowpass % let B2 be the index for 0.98 of total Bthresh=0.98 for n=1:Nx2 if Eaccum(n)/Eaccum(Nx2) < Bthresh B2=n; end; end; B2=2*B2+1; if B2>Nx B2=Nx; end; Heff=Amax*irect(1,B2,1,Nx); % store effective bandwidth EffBW(index+1)=B2; if B2>EffBWmax EffBWmax=B2; EffBWindex=index+1; istore=1; end; Figure 8: Image 10 was found to have the highest value of effective bandwidth. figure(7); % figure 8 in this document imagesc(Abw); colormap gray; title('Input Image') if istore==1 title('Input Image with Best Focus') print -djpeg fig7 end; Color Noise Synthesis Page 8 Figure 9: PSD with highest Effective Bandwidth value. % figure(8)% figure 9 in document plot(k,Wy,k,Heff) %axis([1,Nx,0,1.1*max(Wy)]); title('Estimated Image PSD and Eff. BW') xlabel('Discrete Frequency'); ylabel('PSD'); if istore==1 title('Estimated Image PSD and Eff. BW of Best Focus') print -djpeg fig8 istore=0; end; end; % loop index through figures Color Noise Synthesis Page 9 Figure 10: Correspondence between image index and effective bandwidth. %plot final results for effective bandwidth Bw=1:Nindex; figure(9); % image 10 in this document plot(Bw,EffBW); title('Effective Bandwidth of 0.98 Emax') The final results show that the algorithm works well with the real data which had considerable structure and was much less colored than the control noise. In practice, the PSD of the test data could be used to color the test noise for a closer correspondence of the PSDs. 3. REFERENCES 1. Random Signals Detection, Estimation and Data Analysis by K. Sam Shannugan and A. M. Breipohl. John Wiley& Sons, New York. 1988 2. “Carsons Rule” http://en.wikipedia.org/wiki/Carsons_rule Color Noise Synthesis Page 10