Skip to content

Debanding

Fact Value
Also called Banding removal, False contour removal, Gradient smoothing
Status Implemented and tested
Family Restoration and reconstruction
Engine Science engine
Type Nonlinear, spatial, random reference averaging under a threshold
Output Image
Operator shown as algorithm (exact)
Support radius range
Code engine/src/pixelnexus_engine/filters/artifacts.py, deband
Operator code engine/src/pixelnexus_engine/filters/artifacts.py, deband_representation
Packages NumPy
Channels RGB, Brightness only

Overview

Debanding smooths the steps that show up in gentle gradients, such as skies, when an image has too few levels or was compressed hard. For each pixel, the filter looks at four pixels at a random distance and angle. If all four are close to it, the pixel lies in a smooth area, and it takes their mean, a value between the steps. Real edges differ by more than the threshold and stay. It runs in the PixelNexus engine on your computer.

Four panels: a gray ramp with faint curved bands; the debanded ramp; the banded ramp with its contrast stretched, showing six clear bands; and the debanded ramp stretched the same way, now a smooth gradient with fine grain. Four panels: a gray ramp with faint curved bands; the debanded ramp; the banded ramp with its contrast stretched, showing six clear bands; and the debanded ramp stretched the same way, now a smooth gradient with fine grain.
A gentle ramp in steps of 4 levels and the result with a threshold of 5. The lower panels stretch the range 95 to 130 to the full gray scale, so the bands show clearly.

How it works

  1. Draw a random distance, up to the range, and a random angle for every pixel. The seed fixes them.
  2. Read four references: two along the angle and two at right angles, on both sides of the pixel.
  3. If all four are within the threshold of the pixel, write their mean.
  4. Otherwise keep the pixel.

Mathematical foundation

\[ r_{1,2} = p \pm (d \cos\theta,\ d \sin\theta), \quad r_{3,4} = p \pm (-d \sin\theta,\ d \cos\theta), \qquad q(p) = \begin{cases} \tfrac{1}{4} \sum_i I(r_i) & \max_i \lvert I(r_i) - I(p) \rvert < T \\ I(p) & \text{otherwise} \end{cases}, \qquad d \le 16,\ T = 4 \]

The formula shows the default parameter values.

where

  • \(p\): The pixel
  • \(r_i\): The four reference pixels
  • \(d, \theta\): The random distance and angle of pixel p
  • \(T\): The threshold

Banding is a gradient drawn with too few levels, so it breaks into flat steps. A flat step has no detail to keep, so it can be replaced by an average over a wide area, which brings back the levels in between. Real edges must be kept. For every pixel p, the filter draws a distance d from 1 to the range and an angle theta, at random but with a fixed seed. The four references are p plus and minus d in that direction, and p plus and minus d at right angles to it, rounded to whole pixels. If all four lie within the threshold T of the pixel, the area is flat or a gentle step, and the pixel takes their mean. Otherwise it stays. The random positions turn the step into fine grain, which the eye accepts better than a sharp contour. This follows the idea of the deband filters of flash3kyuu_deband and FFmpeg. The details here are PixelNexus's own.

A band is a flat step where the true image rises slowly. Averaging over an area wider than the band brings back the levels in between. A plain blur would also soften real edges, so the filter only averages where all four references agree with the pixel to within the threshold. The references sit in mirrored pairs, so their mean does not shift the gradient. Random positions mean that neighbors average different areas, which turns the step into fine grain. The eye accepts grain far better than a sharp contour. The idea comes from the deband filters of flash3kyuu_deband and FFmpeg. The details here, such as four references and the all-close test, are PixelNexus's own.

Kernel or operator

The Inspector asks the engine for the stages with the current settings: the reference positions and the replacement rule.

Algorithm

  1. Draw a random distance and angle for every pixel, with the seed.
  2. Read four references, two along the angle and two at right angles, on both sides.
  3. If all four are within the threshold of the pixel, write their mean.
  4. Otherwise keep the pixel.

Parameters

Parameter Default Values Meaning
Range range 16 2 to 64 px The largest distance of the reference pixels.
Threshold threshold 4 0.5 to 20 A pixel is replaced only where all four references lie closer to it than this, in pixel values.
Seed seed 0 0 to 9999 The seed of the random positions. The same seed gives the same result.
Border mode border Reflect Reflect 101 (default), Reflect, Replicate, Constant (zero), Wrap How pixels outside the image are filled when the window reaches past the edge.
Channels channelScope RGB RGB, Brightness only Which part of the color image the filter changes.

Implementation

deband in engine/src/pixelnexus_engine/filters/artifacts.py draws the offsets once with deband_offsets, so all channels use the same positions, then runs deband_plane on each channel the scope names. There is no live preview. The result arrives when you click Apply.

Worked example

Threshold Range 8 Range 16 Range 32
3 49.26 49.26 49.26
5 54.90 61.87 55.98
8 54.90 61.87 55.98

A ramp from 100 to 124 across 160 pixels, with a wave of 6 levels down the rows, rounded to steps of 4 levels, so each band is about 27 pixels wide. The numbers are the PSNR in dB between the 5 x 5 local mean of the result and the smooth ramp, away from the border. This measures how well the steps blend, while ignoring the fine grain the filter adds. The banded ramp itself has 49.26 dB. A threshold of 3 is below the step of 4, so nothing changes. A range of 32 is longer than a band, so more references reach a band that differs by more than the threshold, and fewer pixels are replaced.

Complexity

  • Time: \(O(N)\)
  • Memory: \(O(N)\)

N is the number of pixels. Each pixel reads four references, whatever the range.

Artifacts and limitations

Limitations

  • Adds fine grain in smoothed areas.
  • Faint texture weaker than the threshold is smoothed too.
  • Steps higher than the threshold stay.

Artifacts

  • A grainy look in flat areas.
  • Thin lines near gradients can fade if the threshold is high.

Tests

On a ramp in steps of 4 levels, the local mean of the result must be at least 3 dB closer to the smooth ramp. A step higher than the threshold must stay exactly. The offsets must stay within the range and follow the seed. The engine API tests run jobs and check the stages of the representation.

References