MATLAB Answers

Alec
0

Image processing with gradient FFT

Asked by Alec
on 4 Dec 2013
Latest activity Edited by Eric
on 6 Dec 2013
I have a stack of RGB images and I am trying to find which of those are out of focus in order to remove them. I do not want to correct the focus I just want to find a way to remove the out of focus images without having to look at the actual images but by using the power spectrum. I have done FFT2 to the image and then used the radial average of the power spectrum to see if there is a difference between the in focus or out of focus but I do not see a difference between the two. I decided to use the gradient of the image instead
[gx,gy]=gradient(image);
g_mag=sqrt((gx.^2)+(gy.^2));
new_fft=fft2(g_mag);
shift_ftt=fftshift(abs(new_fft));
I am not sure if I need to remove the mean or add some zero padding (i have tried with and without those) but when I average the power spectrum I do not get the kind of graph I expect. The way I calculate the radial plot (numpix is how many pixels is the region of image I am working on and numf is how many frequencies i am using):
x = 2*(meshgrid(1:numpix)/numpix)-1;
y = 2*(meshgrid(1:numpix)/numpix)-1;
r1 = sqrt(x.^2 + y.^2);
r1(r1>1) = 0;
r1 = r1*numf;
for i=1:numf
d{i} = find((i-1)<r1 & r1<=i);
end;
for i=1:numf
r(i)=mean(fftshift(abs(new_fft((d{i}))));
end
and what I am trying to plot is the r vs the frequencies,freqs=[1:numf]/numf I expect the graph to look like a bell curve starting from zero, peaking and ending close to zero again. I am not sure what I am doing wrong, anyone has any experience in this?

  0 Comments

Sign in to comment.

1 Answer

Answer by Eric
on 5 Dec 2013
 Accepted Answer

You should check out Focus Measure in the File Exchange at
This has a variety of Fourier focus methods already implemented. You can either use one of these or compare your code against these.
Good luck,
Eric

  2 Comments

Alec
on 6 Dec 2013
Dear Eric,
thank you for the answer. I looked at the code you pointed out. Although it is helpful for me to construct the gradient,I need use the FFT2 and find the power spectrum to compare rather than finding a threshold. I was wondering if my code is accurate or my expectation of seeing a bell curve for the radial plot is mistaken.
Eric
on 6 Dec 2013
You might check out this code again. For example, consider the 'DCTE' option. It calculates the 2D discrete cosine transform, takes the magnitude squared of that, and then looks at some metrics of that. That's a lot like finding the power spectrum and then analyzing it.
You could also look at the reference: [Shen2006] Shen, C. & Chen, H. Robust focus measure for low-contrast images. 2006.
You could do a cited-reference search to see who's referenced this work and perhaps tweaked things a bit relative to what Shen did.
Anyways, I was trying to address your initial problem: "I just want to find a way to remove the out of focus images without having to look at the actual images but by using the power spectrum." Unfortunately, I don't have time to work through your code for the radial plot. But determining focus from imagery is a topic that has been worked a great deal and perhaps a better place to start is what folks have already tried.
Good luck,
Eric

Sign in to comment.