Skip to content

Deblocking

Fact Value
Also called JPEG deblocking, Re-application of JPEG, Block artifact removal
Status Implemented and tested
Family Restoration and reconstruction
Engine Science engine
Type Nonlinear, transform, shifted JPEG re-application and averaging
Output Image
Operator shown as algorithm (exact)
Support radius global
Code engine/src/pixelnexus_engine/filters/artifacts.py, deblock
Operator code engine/src/pixelnexus_engine/filters/artifacts.py, deblock_representation
Packages SciPy

Overview

Deblocking hides the 8 x 8 blocks of strongly compressed JPEG images. It uses Nosratinia's re-application of JPEG: shift the image within the 8 x 8 grid, compress it again with the same quality, shift it back, and average over many shifts. The new blocks straddle the old block edges, so the average is smooth there. It runs in the PixelNexus engine on your computer.

Four panels: a Siemens star saved as JPEG with quality 20, with blocky, noisy spoke edges; the deblocked star, with cleaner spokes; the result with one grid, which looks like the JPEG; and the original. Four panels: a Siemens star saved as JPEG with quality 20, with blocky, noisy spoke edges; the deblocked star, with cleaner spokes; the result with one grid, which looks like the JPEG; 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 only, which changes almost nothing, and the original.

How it works

  1. Convert to the Y'CbCr of JFIF, as JPEG does.
  2. Build the quantization tables for the quality, as the IJG library does.
  3. For each shift of the grid, cut the shifted plane into 8 x 8 blocks and take their DCT. Round each coefficient to a multiple of its table entry, and transform back.
  4. Shift back and average over all shifts.
  5. Convert back to RGB.

Mathematical foundation

\[ Q = \min\!\left(255,\ \max\!\left(1,\ \left\lfloor \frac{Q_K\, s + 50}{100} \right\rfloor\right)\right), \quad s = \begin{cases} \lfloor 5000 / q \rfloor & q < 50 \\ 200 - 2q & q \ge 50 \end{cases}, \qquad J(B) = \operatorname{IDCT}\!\left(Q \cdot \operatorname{round}\!\left(\frac{\operatorname{DCT}(B - 128)}{Q}\right)\right) + 128, \qquad \hat{I} = \frac{1}{n^2} \sum_{s} S_{-s}\, J\big(S_s I\big), \qquad q = 50 \]

The formula shows the default parameter values.

where

  • \(q\): The JPEG quality
  • \(Q_K, Q\): The table of Annex K, and the table for quality q
  • \(B\): An 8 x 8 block
  • \(J\): The lossy step of JPEG on a block
  • \(S_s\): A shift of the image by s, within the 8 x 8 grid
  • \(n\): The number of shifts per axis

JPEG splits each plane into 8 x 8 blocks and takes the DCT of each block after subtracting 128. It rounds every coefficient to a multiple of its entry in a quantization table Q. Q_K is the table of ITU-T T.81 Annex K, scaled for the quality q as the IJG library does. The rounding makes each block jump at its edge. Nosratinia observed that compressing the decoded image again, with the same tables but on a shifted grid, smooths those jumps, since the new blocks straddle the old edges. Each shifted copy is a slightly different estimate of the original. Their average, over n by n shifts S_s, is smoother and closer to the original. With n = 1 and the original grid, the image comes back almost unchanged, since a decoded JPEG is already rounded on that grid.

JPEG loses detail in one step: it rounds the DCT coefficients of each 8 x 8 block to multiples of a quantization table. Each block is rounded on its own, so neighboring blocks no longer meet smoothly. Nosratinia noticed that the decoded image already satisfies the rounding on its own grid, so compressing it again there changes almost nothing. On a shifted grid, the rounding acts across the old block edges and smooths them. Each shifted copy is a slightly different estimate of the original, and their average is better than any one of them. The tables are those of ITU-T T.81 Annex K, scaled for the quality as the IJG library does, so they match most JPEG files. The filter must be told the quality, since it gets pixels, not the file.

Kernel or operator

The Inspector asks the engine for the stages with the current settings. They are the color conversion, the quantization table with its first row, the re-application on one block, and the average over the shifts.

Algorithm

  1. Convert to the Y'CbCr of JFIF.
  2. Build the quantization tables for the quality.
  3. For each shift, compress every 8 x 8 block of the shifted plane again and decode it.
  4. Shift back and average over all shifts.
  5. Convert back to RGB.

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 that most programs use. It sets the quantization tables.
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 are averaged along each axis, evenly spaced. 1 re-applies JPEG on the original grid only.
Channels channels Y, Cb and Cr Y, Cb and Cr, Luma only Which planes of the Y'CbCr of JFIF are processed.
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.

Implementation

deblock in engine/src/pixelnexus_engine/filters/artifacts.py converts with jfif_ycbcr, runs deblock_plane on Y, and on Cb and Cr unless only luma is asked for, and converts back with jfif_rgb. The function quantization_table builds the tables, and the function requantize does the lossy step with scipy.fft.dctn and idctn on all blocks at once. 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 23.58 23.77
Siemens star 20 25.42 25.55 26.06
Siemens star 30 26.88 27.08 27.56
Siemens star 50 29.12 29.27 29.91
Colored squares 10 24.30 25.21 24.16
Colored squares 20 26.08 27.35 25.62
Colored squares 30 27.59 28.55 27.30
Colored squares 50 29.90 30.94 30.22

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)\)
  • Memory: \(O(N)\)

N is the number of pixels and n the shifts per axis. Each shift costs one 8 x 8 DCT and inverse DCT per block.

Artifacts and limitations

Limitations

  • The quality must be known or guessed.
  • Does not model chroma subsampling, so color blocks of 16 x 16 pixels stay partly.
  • Assumes the block grid starts at the top left pixel. Cropped JPEGs can be off the grid.

Artifacts

  • Slight blur of fine texture when the quality is set too low.

Tests

The quantization tables must equal those of libjpeg, read from JPEG files that Pillow writes, for qualities from 1 to 100. The JFIF color conversion must match Pillow to one level and invert. With one grid, a decoded JPEG must come back almost unchanged. With 64 grids, a JPEG of quality 20 must come at least 0.3 dB closer to the original. The engine API tests run jobs and check the stages of the representation.

References