# Extract sharpness from FFT

I've got in total 6 images, from 2 cameras (thus 3 per camera) storing 16 bit data.

What I need to feedback to the user is like some sort of sharpness coefficient, preferably live while he is adjusting the focus of the whole system (since its looking into the eyes, it depends on the dioptres of the patient).

What I am currently using is a contrast coefficient combined with standard deviation, which should work fine, but with a change of the focus the field of view changes slightly, which can impact the contrast.

Now I wonder when I move to use the FFTW library to get the FFT of my image, it results in an array of fftw_complex (which is double[2]).

For my purpose, it should be fine to calculate the average of the resulting array, but I don't understand how to mangle real / complex components in this. Do I iterate over the whole array, calculate sum equalling sum of double[0] and double[1], or am I totally wrong here? I don't really understand why there is a real and a complex component. I understand that the FFT results in coefficients for different frequency sinus waves, but the frequency of the wave depends on its position in the array, while the amplitude should be the real component. Is the combination of real and complex variable responsible for the direction in the image, or what is it used for, and how would I use it to get an idea of an "average" frequency (sharpness) in the image?

## 1 Answer

As you said, in the fourier transform, the frequency of the wave depends on its position in the array. But, the amplitude of the wave is not always the real component, it is actually the magnitude of the complex number ($\sqrt{real^2+imaginary^2}$).

If your initial image is perfectly symmetric along the exact center, you can get real coefficients, but it's something which seldom appears in image analysis.

Edit: Here's more details... Couldn't find the picture I was refering to...

The discrete Fourier transform, the basis of every FFT algorithm, is defined as such: $$X_k = \sum_{n=0}^{N-1}x_n\cdot e^{-i2\pi kn/N}$$

From Euler's formula, a complex exponential can be separated in a complex sum of cosinus and sinus: $$e^{ix}=\cos(x)+i\sin(x)$$

If we now look back at the Fourier transform, it can be separated into two sums: $$X_k = \sum_{n=0}^{N-1}x_n\cos(-2\pi kn/N) + i \sum_{n=0}^{N-1}x_n\sin(-2\pi kn/N)$$ As a sidenote here, if your image (or signal) is perfectly symemtric about it's center, the cosinus will resonate strongly with your image while the sinus will not output anything. It will result a completly real image. It's the other way around for a perfectly anti-symmetric signal where one half is the negative of the other.

Now knowing this, it's straightforward to compute the amplitude of the resulting FFT, using the formula I gave in the first paragraph.

To get the frequency of the image itself is less obvious. We would expect the result to look somewhat like this, with the zero-frequency component at the center. It is, however, not the case.

The "standard" FFT algorithm will put the zero-frequency component in a corner (I can never remember which one, top-left or bottom-left!), such that the array indice [0,0] corresponds to $k_x=k_y=0$. But wait, once you cross N/2, the pixels represents negative frequencies, counting backwards. A good reminder is Python's scipy.fftpack.fftfreq() function found fftfreq().

My personal tips to take a quick look at an image's spectrum is to shift it then display the logarithm of it. When it's not shifted, it's hard to see asymetries. When looking at the non-log version, it's usually just a big black square with a small fuzzy grey dot...

• Okay, that is what I have guessed so far. Is the direction of that frequency made up by the complex number in the 2 dimensional image? If so, why is there also an imaginary part for a 1d FFT? Mar 12 '13 at 17:34
• The complex number will give you the magnitude and the phase of the frequency, direction is dictated by the direction of the pixel relative to the position of the zero-frequency (or "DC") component. I found a good picture representation somewhere else on SE, I'll update my answer later whenever I find it. Mar 13 '13 at 14:41