← All projects

PAPER IMPLEMENTATION / PYTHON

FADEFog Density Evaluation

This is a fog density evaluation tool I implemented in Python based on the FADE paper. It needs no paired fog-free image: a single RGB image is enough to compute a FADE score and a local fog density map.

View on GitHub
  • Python
  • NumPy
  • SciPy

The paper and its source.

The paper proposes FADE (Fog Aware Density Evaluator) to estimate perceptual fog density from a single image using natural scene statistics, without a paired fog-free image.

It also uses FADE to guide the DEFADE defogging method. This project implements the fog density evaluation part, producing a whole-image score and local heatmap.

Referenceless Prediction of Perceptual Fog Density and Perceptual Image Defogging

Lark Kwon Choi, Jaehee You, Alan Conrad Bovik

IEEE Transactions on Image Processing, 24(11), 3888–3901, November 2015.

DOI: 10.1109/TIP.2015.2456502

01 / FADE

A fog density score and a heatmap.

Each image produces two results: a numeric FADE score reflecting fog density across the whole image, and a heatmap showing local fog density. Choose an example to compare the original image with its grayscale heatmap.

Higher FADE scores indicate denser fog.

Mountain landscape

The mountain scene has the lowest whole-image FADE score of these three examples.

Whole-image FADE score0.9113
Original image
Mountain landscape

576 × 768 px

Fog density heatmap (grayscale)
Mountain landscape — Grayscale fog density heatmap; brighter patches indicate higher local fog density values. Magenta marks patches with no valid result.

Density map grid: 72 × 96 patches · Patches with no valid result: 2

Shared local density scale

0 · Lower20.2478 · Higher

No valid result (NaN)

These examples show three different scenes, not different fog levels added to the same photograph.

Each map cell represents an 8×8-pixel image patch. All three maps share one grayscale scale, from zero to the highest finite local value across the examples. Incomplete patches at the bottom and right edges are omitted.

The whole-image score is computed from the aggregated reference distances; it is not the average of the local map, a percentage, or a physical measurement of fog concentration.

Example images: fade_py / test_image ↗

02 / FADE

FADE’s principle and computation.

FADE compares perceptual features with statistical reference models of fog-free and foggy natural images. A paired clear image is not required, while the reference statistics are loaded from the package’s precomputed .mat files.

Fog changes local contrast, texture, brightness and color. FADE measures these changes through 12 features. The flow below describes the current Python implementation, including its patch processing and distance aggregation.

What the 12 features measure

f01–f05

Structure and local variation

Mean-subtracted contrast-normalized (MSCN) variance, variances of nonnegative and nonpositive vertical MSCN products, mean local standard deviation and mean coefficient of variation.

f06–f09

Contrast and information

Mean contrast energy in the gray, yellow–blue and red–green channels, plus grayscale patch entropy.

f10–f12

Intensity and color

Mean pixel-wise dark-channel intensity, mean saturation and colorfulness.

From image patches to reference distances

  1. Crop and prepare

    Crop the RGB image to multiples of 8, retaining its upper-left region. Prepare grayscale, color channels, MSCN and contrast energy through a shared context.

  2. Extract patch features

    Compute f01–f12 for non-overlapping 8×8 patches. Each feature produces a grid with H//8 rows and W//8 columns.

  3. Assemble the feature matrix

    Flatten the feature grids in row-major order and join them into an N×12 matrix. Apply the natural-log transform log1p to each value.

  4. Compute each patch’s variance

    Calculate a sample variance across the 12 transformed components of each patch. This scalar supplies the current implementation’s covariance proxy.

  5. Load both reference models

    Read the 12-dimensional mean and 12×12 covariance for the fog-free and foggy classes. These parameters are cached; inference does not fit new reference models.

  6. Calculate both distances

    Compute Mahalanobis-like distances by mixing the variance proxy with each reference covariance and solving linear systems in batches. Each patch yields a distance to the fog-free model and one to the foggy model.

Two outputs from the same distances

Whole-image score

Take nanmean of the distances to each class separately, ignoring each group’s NaN values. Divide the mean distance to the fog-free model by the sum of the mean distance to the foggy model and 1.

Local heatmap

For each patch, divide its distance to the fog-free model by the sum of its distance to the foggy model and 1, then place the results back into the patch grid.

Df is the mean distance to the fog-free reference; Dff is the mean distance to the foggy reference. d_free(b) and d_foggy(b) are the corresponding distances for patch b.

The whole-image score is a ratio of two distance means, so it generally differs from the mean of the local heatmap. Local values are not normalized to [0,1]; the grayscale scale above is only for display.

03 / PYTHON

What I implemented.

The FADE algorithm comes from the paper. My work is its Python implementation and engineering improvements in fade_py.

A usable Python implementation

I implemented the feature extraction and scoring pipeline with NumPy and SciPy, exposing fade() for a score and fade_with_map() for a score and map.

Independent, reusable modules

I separated the perceptual features, numerical operations, reference data and pipeline, making the implementation easier to inspect and maintain.

Vectorization and caching

I vectorized patch entropy and distance calculations, reused intermediate results and cached reference statistics. Batching bounds temporary working memory.