MATLAB Answers

Image processing with gradient FFT

15 views (last 30 days)
Alec on 4 Dec 2013
Edited: 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
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);
for i=1:numf
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?

Accepted Answer

Eric on 5 Dec 2013
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 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,

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!