What is Flat Field Correction?

The Flat-field correction (Wikipedia: FFC) is a method used to correct:

the differences of light sensitivity between the pixel sensors of a camera
some artifacts related to the optical system (e.g., non-uniform lighting and vignetting)

The goal is to correct the pixels of the captured (raw) images in such a way that when a uniform background is captured by the system (camera & lens), the resulting output image is uniform.

Formula

This correction is achieved by applying the following operation to each pixel of the raw image:

CorrectedPixel = (RawPixel - Offset) * Gain

where both Offset and Gain coefficients are specific values for each pixel.

The evaluation of the coefficients Offset and Gain requires a calibration procedure explained in the next paragraph.

Calibration

The calibration procedure to compute the coefficients is done in two steps:

Dark image acquisition

A dark image is acquired by the system. This is typically achieved by covering the lens with the cap. The captured image represents the dark current of the sensors, and is considered as a fixed bias that we want to eliminate when acquiring images in normal conditions. This correction is called dark-frame subtraction.

CorrectedPixel = RawPixel - DarkPixel

For each pixel, the DarkPixel value corresponds to the Offset in the above FFC.

Flat image acquisition

A flat image is acquired by the system. For example by capturing a flat (uniform) background, not too bright to avoid saturation and not too dark.

From dark and flat acquisitions, we have enough data to compute the Gain value of the FFC.

For this we define CorrectedPixel as the pixel value we consider to be correct for the flat image. Let's set this value as the average pixel value of the flat image (average(Flat)), corrected by the average of the dark image (average(Flat)). In the FFC terms, this gives:

average(Flat) - average(Dark) = (FlatPixel - DarkPixel) * Gain

This leads to the Gain value

Gain = (average(Flat) - average(Dark)) / (FlatPixel - DarkPixel)

The same computation is repeated (width * height) times, to cover all pixels of both flat and dark images. This results in a specific correction for each pixel of the image.

Note: this calibration procedure must be redone if any part of the system is changed, including the camera unit, lighting or optics equipment.

Calibration of color pixel formats

For color pixel formats, we have several ways of computing the value of average(Flat). In all cases, the Gain computation is repeated (width * height * componentsPerPixel) times to cover all pixel components, which results in specific corrections for each pixel component of the image.

Handling pixel components individually

I.e. in RGB:

using average(Flat[Red]) for computing the Gain values of Red components;
using average(Flat[Green]) for computing the Gain values of Green components;
using average(Flat[Blue]) for computing the Gain values of Blue components.

Handling pixel components together

I.e. in RGB, using average(average(Flat[Red]), average(Flat[Green]), average(Flat[Blue])) for computing the Gain values of Red, Green and Blue components.

This way of computing the average (i.e. over the pixel components) results in FFC coefficients that also correct the balance between components. Therefore, depending on the quality of the uniform background used to acquire the flat image, the FFC can effectively perform a white balance correction.