Skip to content

JPEG artifact removal (classical)

Fact Value
Also called JPEG cleanup, JPEG artifact reduction
Status Implemented and tested
Family Restoration and reconstruction
Engine Science engine
Type Nonlinear, transform, deblocking followed by deringing
Output Image
Operator shown as algorithm (exact)
Support radius global
Code engine/src/pixelnexus_engine/filters/artifacts.py, jpeg_cleanup
Operator code engine/src/pixelnexus_engine/filters/artifacts.py, jpeg_cleanup_representation
Packages SciPy, OpenCV
Channels Brightness only, RGB

Overview

JPEG artifact removal cleans strongly compressed JPEG images in two steps, with no learned model. Deblocking re-applies JPEG on shifted grids and averages the results, which hides the 8 x 8 blocks. Deringing then smooths the ripples next to strong edges. It runs in the PixelNexus engine on your computer.

Four panels: a Siemens star saved as JPEG with quality 20, with blocky, rippled spokes; the cleaned star, with smooth, clean spokes; the result with one grid, which only removes ripples; and the original. Four panels: a Siemens star saved as JPEG with quality 20, with blocky, rippled spokes; the cleaned star, with smooth, clean spokes; the result with one grid, which only removes ripples; and the original.
A Siemens star saved as JPEG with quality 20, the result with the defaults and quality 20, the result with one grid, and the original.

How it works

  1. Deblock: re-apply JPEG on shifted 8 x 8 grids in the Y'CbCr of JFIF and average.
  2. Find the strong edges of the deblocked luma and build a soft zone around them.
  3. Smooth the zone with the bilateral filter and blend it in.

Mathematical foundation

\[ \hat{I} = \operatorname{dering}\big(\operatorname{deblock}(I)\big), \qquad \operatorname{deblock}(I) = \frac{1}{n^2} \sum_s S_{-s}\, J\big(S_s I\big), \qquad \operatorname{dering}(I) = w\, \operatorname{bilateral}(I) + (1 - w)\, I, \qquad q = 50,\ T = 20 \]

The formula shows the default parameter values.

where

  • \(J\): The lossy step of JPEG for quality q
  • \(S_s\): A shift of the image within the 8 x 8 grid
  • \(w\): The blend weight of the zone around strong edges
  • \(T\): The edge threshold of the deringing step

JPEG leaves two kinds of damage. Block edges come from rounding each 8 x 8 block on its own, and ringing comes from the rounding of the fine detail next to strong edges. The first step is the deblocking filter, Nosratinia's re-application of JPEG. J is the lossy step of JPEG with the quantization tables of quality q, and S_s shifts the image within the 8 x 8 grid. The second step is the deringing filter. w is the soft weight of the zone around strong edges, and the bilateral filter smooths the ripples there. See those two filters for the details. The deblocking step also removes some ringing, so the deringing step has less to do.

JPEG damage has two parts. The blocks come from rounding each 8 x 8 block on its own. The ripples come from rounding away the fine detail next to strong edges. The deblocking filter handles the first, and also some of the second. The deringing filter handles what is left near strong edges. See those two pages for the details. A learned JPEG restoration model is planned in Phase 7, and this filter is the classical baseline to compare it with.

Kernel or operator

The Inspector asks the engine for the stages with the current settings: the four stages of deblocking followed by the three stages of deringing.

Algorithm

  1. Deblock with the re-application of JPEG, averaged over the shifts.
  2. Find strong edges on the luma of the deblocked image and build the zone around them.
  3. Smooth the zone with the bilateral filter and blend it in.

Parameters

Parameter Default Values Meaning
JPEG quality quality 50 1 to 100 The quality the image was saved with, from 1 to 100, as in the IJG library. It sets the quantization tables of the deblocking step.
Shifts per axis shifts 8 (64 grids) 1 (1 grid), 2 (4 grids), 4 (16 grids), 8 (64 grids) How many shifts of the 8 x 8 grid the deblocking step averages along each axis.
Deblock channels channels Y, Cb and Cr Y, Cb and Cr, Luma only Which planes of the Y'CbCr of JFIF the deblocking step processes.
Edge threshold threshold 20 1 to 200 Strong edges are where the gradient of the smoothed luma reaches this many levels per pixel.
Zone radius radius 4 1 to 15 px How far from a strong edge the smoothing reaches. Ringing of JPEG stays within its 8 x 8 block.
Range sigma sigmaColor 25 1 to 100 The range sigma of the bilateral filter, in pixel values. Differences well above it are kept.
Space sigma sigmaSpace 2 0.5 to 6 px The spatial sigma of the bilateral filter. Its window has a radius of ceil(2 sigma).
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 Brightness only RGB, Brightness only Which part of the color image the filter changes.

Implementation

jpeg_cleanup in engine/src/pixelnexus_engine/filters/artifacts.py runs deblock_image, then dering_planes with the same parameters. There is no live preview. The result arrives when you click Apply.

Worked example

Image Quality JPEG 2 x 2 shifts 8 x 8 shifts
Siemens star 10 23.43 25.44 25.70
Siemens star 20 25.42 28.12 28.99
Siemens star 30 26.88 30.16 30.76
Siemens star 50 29.12 32.55 33.23
Colored squares 10 24.30 25.36 24.28
Colored squares 20 26.08 27.45 25.67
Colored squares 30 27.59 28.65 27.34
Colored squares 50 29.90 31.00 30.24

The PSNR in dB against the original, for images of 160 by 160 pixels saved with Pillow's JPEG encoder at each quality, without chroma subsampling, and decoded again. The filter is told the true quality of the file. The last two columns use 4 and 64 shifted grids. On the colored squares, whose sharp color edges repeat every 20 pixels, 4 grids beat 64, and 64 grids even fall below the JPEG at low quality. Averaging many grids also blurs sharp edges, most of all in Cb and Cr, whose table is coarse. The colored squares are the scene of the dehazing example. The Siemens star has many strong edges, where ringing is worst.

Complexity

  • Time: \(O(n^2 N + N r_s^2)\)
  • Memory: \(O(N)\)

N is the number of pixels, n the shifts per axis and r_s the radius of the bilateral window.

Artifacts and limitations

Limitations

  • The quality must be known or guessed.
  • The deringing zone rule is approximate, as in the deringing filter.
  • Lost detail does not come back.

Artifacts

  • Slight blur of fine texture near edges.

Tests

The result must equal deringing applied to the output of deblocking. The two steps are tested on their own pages. The engine API tests run jobs and check the stages of the representation.

References

  • Nosratinia, A. Enhancement of JPEG-compressed images by re-application of JPEG. Journal of VLSI Signal Processing Systems 27(1 to 2), 69 to 79, 2001. DOI 10.1023/A:1008167430544.
  • Tomasi, C., and Manduchi, R. Bilateral filtering for gray and color images. Sixth International Conference on Computer Vision, 839 to 846, 1998. DOI 10.1109/ICCV.1998.710815.