AL B
AL B

Reputation: 223

Image derivative and FFT-Matlab

I have an RGB image and I am trying to find if this image is in focus or out of focus. In the beginning I did a 2D FFT but when I plotted the radial spectrum there was no clear differentiation between images that are in focus and images that are out of focus. I was told to use derivatives of the images but when I plot this new spectrum the results do not look what I expected. Because it is part of a larger program, I am writing a pseudocode here, this is not the complete program

 %read the file and the part of the image I am working on
  file='test1.jp2'
  image_part=imread(file,'PixelRegion',{[xpixmin xpixmax],[ypixmin ypixmax]});
 %derivatives
  dx=diff(double(image_part),1,1);
  dy=diff(double(image_part),1,2);
  ........
  created tapers with Slepian sequences (dpss), multiplied with dx and dy and 
  then new outcome is tap_dx,tap_dy
  .......   
 %FFT
  fft2_dx=fft2(tap_dx)
  fft2_dy=fft2(tap_dy)
 %magnitude and fftshift
  fft2_abs_dx=fftshift(abs(fft2_dx))
  fft2_abs_dy=fftshift(abs(fft2_dy))
  %to take the radial spectrum average the Fourier spectrum over the different   
  frequencies(fr)
   avg_dx=mean(fft2_abs_dx(fr))
   avg_dy=mean(fft2_abs_dy(fr))   
   plot(fr,avg_dx+avg_dy)

Before I did the derivative the radial plot starts from a maximum point and then goes down in a monotonic way to the minimum value. When I plot the derivative though the radial plot starts from a maximum point then goes to the minimum point and then it increases again which does not seem to be right. Has anyone tried to find whether an image is in focus or out of focus using this technique. I have not found any relevant references for that.

The purpose of the project is not to correct the focus of the image but figure out if an image is out of focus and if it is to reject it using an automated way.
Thank you in advance.

Upvotes: 0

Views: 1011

Answers (1)

Bruce Dean
Bruce Dean

Reputation: 2828

Defocus is manifested through a quadratic term in the optical system pupil. You will need to choose some basis set, say Zernike or Seidel, then work out the gradient w.r.t to the coeff corresponding to defocus.

The image you collected is a convolution of the system point spread function (PSF) and the true object you are imaging:

ImageData = Image ** PSF + noise, where ** denotes convolution.

Note that the PSF is given by (complex conj. square of the PSF complex amplitude):

PSF = PSF_ca *. conj(PSF_ca), 

where

PSF_ca  = FFT(pupil_complex_amplitude),

and

pupil_complex_amplitude = A*exp(-i*2*pi*pupil_phase), A is the aperture function.

and (with i*i = -1):

pupil_phase = defocusCoeff*basisTerm;

You can handle the convolution in the expression for ImageData using the convolution theorem, that way you can express it in terms of FFTs and solve directly for the blur kernel (the PSF). This is called blind deconvolution because you don't know either the true object or the PSF.

Once you have the PSF you do phase retrieval on the PSF to get the defocus, that is, if you want to solve this rigorously.

I think there are some examples of deconvolution in the Matlab help files, have you looked at the examples there?

Implementing a "gradient of the image" doesn't solve for image-defocus, why are you doing that?

Upvotes: 1

Related Questions