AutoFocusWithPSDestimation

download AutoFocusWithPSDestimation

of 10

Transcript of AutoFocusWithPSDestimation

  • 8/2/2019 AutoFocusWithPSDestimation

    1/10

    Color Noise Synthesis Page 1

    AUTO-FOCUS USING PSD ESTIMATION EFFECTIVE

    BANDWIDTHBy 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 SIMULATIONForm an input noise image from White Gaussian Noise.

    clear all;% dimensions of imageNx=512;My=Nx;% form the control noise image from Gaussian distributionw=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)');

  • 8/2/2019 AutoFocusWithPSDestimation

    2/10

    Color Noise Synthesis Page 2

    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 rowWy=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 lowpassE=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');

  • 8/2/2019 AutoFocusWithPSDestimation

    3/10

    Color Noise Synthesis Page 3

    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 processnmax=10;deltadev=60Nindex=floor(2*nmax+1);EffBW=zeros(1,Nindex);sigmaAll=zeros(1,Nindex);

    EffBWindex=0;EffBWmax=0;istore=0;for n=-nmax:nmax % -nmax

  • 8/2/2019 AutoFocusWithPSDestimation

    4/10

    Color Noise Synthesis Page 4

    if B2>NxB2=Nx;

    end;Heff=Amax*irect(1,B2,1,Nx);% store effective bandwidthEffBW(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 -djpegfig3end;

    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

  • 8/2/2019 AutoFocusWithPSDestimation

    5/10

    Color Noise Synthesis Page 5

    print -djpegfig4end;

    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 -djpegfig5istore=0;

    end;end;

  • 8/2/2019 AutoFocusWithPSDestimation

    6/10

    Color Noise Synthesis Page 6

    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 AUTOFOCUSDownload 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 DATAPathname='ee640data'% path or folder name in reference to your default

    pathFilename='autofocusdataB_'% name of files not including the index or

    suffix

    Filesuffix='jpg'% suffix or image type% get size of imagesindex=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);

  • 8/2/2019 AutoFocusWithPSDestimation

    7/10

    Color Noise Synthesis Page 7

    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 rowWy=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 energyNx2=Nx/2;

  • 8/2/2019 AutoFocusWithPSDestimation

    8/10

    Color Noise Synthesis Page 8

    Eaccum=zeros(1,Nx2);for n=1:Nx2

    for m=1:nEaccum(n)=Eaccum(n)+Wy(m);

    end;end;% Determine equivalent ideal lowpass% let B2 be the index for 0.98 of totalBthresh=0.98for n=1:Nx2

    if Eaccum(n)/Eaccum(Nx2) < BthreshB2=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>EffBWmaxEffBWmax=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 documentimagesc(Abw);colormap gray;title('Input Image')if istore==1

    title('Input Image with Best Focus')print -djpegfig7

    end;

  • 8/2/2019 AutoFocusWithPSDestimation

    9/10

    Color Noise Synthesis Page 9

    Figure 9: PSD with highest Effective Bandwidth value.

    %figure(8)% figure 9 in documentplot(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 -djpegfig8istore=0;

    end;end; % loop index through figures

  • 8/2/2019 AutoFocusWithPSDestimation

    10/10

    Color Noise Synthesis Page 10

    Figure 10: Correspondence between image index and effective bandwidth.

    %plot final results for effective bandwidthBw=1:Nindex;figure(9); % image 10 in this documentplot(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. REFERENCES1. 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