Excellent question. Using the 2D FFT output is a powerful and computationally efficient way to assess image quality attributes like focus, sharpness, and motion blur. The fundamental principle is that sharp, detailed images have more energy in their high-frequency components, while blurry or out-of-focus images have their energy concentrated in the low-frequency components.
After you've performed the 2D FFT on an image frame, you get a 2D array of complex numbers representing the frequency spectrum. Here's a breakdown of the calculations you can perform on this output to generate a quality score.
Pre-computation Step: The Magnitude Spectrum
For almost all of these methods, the first step is to convert the complex FFT output into a human-interpretable format: the magnitude (or power) spectrum.
- FFT Shift (
fftshift): The raw output of an FFT places the zero-frequency component (the DC component, representing average brightness) at the top-left corner. Use a fftshift function to move this DC component to the center of the spectrum. This makes the frequency layout more intuitive: low frequencies are in the center, and frequencies increase as you move outwards towards the edges.
- Calculate Magnitude: The FFT output
F is an array of complex numbers. Calculate the absolute magnitude of each component: Magnitude = abs(F). Often, people use the log of the magnitude for visualization (log(1 + abs(F))), but for scoring, the direct magnitude or the squared magnitude (Power Spectrum = abs(F)^2) is typically used.
Calculations for Scoring Image Quality
Here are several methods, ranging from simple and effective to more nuanced, that you can use to score each frame's magnitude spectrum. A higher score will generally indicate a sharper, better-quality image.
1. High-Frequency Energy Ratio (Most Common & Effective)
This is the most direct and reliable method for general sharpness. The logic is to measure what proportion of the image's total energy lies in the high-frequency bands.
How to Calculate:
- Define a "low-frequency" region. This is typically a circular mask around the center of the (shifted) magnitude spectrum. Let's say you define a radius
r that covers, for example, the inner 5-15% of the spectrum's dimensions.
- Sum the magnitudes of all FFT coefficients outside this central low-frequency mask. This sum is your
HighFrequencyEnergy.
- (Optional but Recommended) To make the score robust to changes in overall image brightness, normalize it. You can do this by dividing by the total energy (sum of all magnitudes in the spectrum).
Formula:
Score = sum(Magnitude[mask_high_frequency]) / sum(Magnitude[all])
Interpretation:
- A high score means a larger proportion of energy is in the high frequencies, indicating sharp edges and fine details.
- A low score indicates that energy is concentrated in the center, which is characteristic of a blurry or out-of-focus image.
Tuning: The size of the central mask (r) is a parameter you may need to tune. A larger mask is more aggressive in what it considers "high frequency."
2. Average Frequency Magnitude (Weighted Mean)
Instead of a binary mask, this method calculates the average radial frequency, weighted by the magnitude at that frequency.
How to Calculate:
- For every point
(u,v) in the magnitude spectrum, calculate its radial distance from the center: d = sqrt(u^2 + v^2). This d represents the frequency.
- Calculate the weighted average of these distances, where the weight for each point is its magnitude.
Formula:
Score = sum(Magnitude(u,v) * d(u,v)) / sum(Magnitude(u,v))
Interpretation:
- If an image has strong high-frequency components (high magnitude far from the center), they will contribute heavily to the sum, pulling the average
Score higher.
- A blurry image with energy near the center will have a low average score.
3. Detecting Motion Blur Directionality (More Advanced)
Motion blur is not uniform; it has a direction. In the frequency domain, this manifests as a characteristic smearing or a series of dark nulls (lines of zero energy) perpendicular to the direction of motion. A sharp image has more evenly distributed (isotropic) high-frequency content.
How to Calculate a "Blur Anisotropy" Score:
- Work with the high-frequency portion of the magnitude spectrum (e.g., by masking out the center as in Method 1).
- Integrate the energy along different angles (radial lines) from the center. You can do this by converting the spectrum to polar coordinates or by iterating through angles and summing pixel values along those lines.
- You will get an energy profile as a function of angle (e.g., a 1D array of energy vs. 0-180 degrees).
- Calculate the variance or standard deviation of this angular energy profile.
Interpretation:
- Low Variance: The high-frequency energy is evenly distributed across all directions (isotropic). This is characteristic of a sharp, non-motion-blurred image. This is a good score.
- High Variance: The energy is concentrated in a specific direction (anisotropic). This indicates strong directional patterns, a tell-tale sign of motion blur. This is a bad score.
Note: For this method, a lower score is better. You can invert it (1 / variance) to align with the other methods where higher is better.
Putting It All Together: Your Algorithm
For each 15-frame video segment:
- Initialize
best_score = -1 and best_frame_index = -1.
- Loop through each of the 15 frames in the segment:
a. Convert the current frame to grayscale (color information is usually not necessary for this and it simplifies the FFT to a single channel).
b. Perform the 2D FFT on the grayscale frame.
c. Apply
fftshift to center the frequency spectrum.
d. Calculate the magnitude spectrum.
e. Calculate a quality score for this frame using one of the methods above (the High-Frequency Energy Ratio is an excellent starting point).
f. Compare and update:
if current_score > best_score: best_score = current_score best_frame_index = current_frame_index
- After the loop finishes,
best_frame_index will hold the index of the sharpest frame in that 15-frame segment.
- Extract and save or use this "best frame."
- Repeat the entire process for the next segment in the video.
Important Considerations
- Noise vs. Detail: High-frequency camera sensor noise can be misinterpreted as fine detail, potentially giving a very noisy frame an artificially high score. If noise is a significant problem, you might consider defining your "high-frequency" band to be a ring—excluding both the low-frequency center and the extreme high-frequency edges where noise often dominates.
- Image Content: A perfectly focused image of a plain wall or a clear sky will naturally have very little high-frequency content and will get a low score. This method works best because you are doing a relative comparison within a short segment. The assumption is that the content does not change drastically in 15 frames, so the frame with the most high-frequency energy is the one that is least affected by blur or focus issues.
- Computational Cost: While FFT is fast, this process can still be intensive for high-resolution video. Performing the analysis on downscaled frames can be a good trade-off between speed and accuracy.