Skip to content

Bilateral solver

Fact Value
Also called Fast bilateral solver, Edge-aware smoothing by optimization
Status Implemented and tested
Family Nonlinear spatial filters
Engine Science engine
Type Nonlinear, iterative, bilateral-space least squares
Output Image
Operator shown as algorithm (exact)
Support radius the whole image
Code engine/src/pixelnexus_engine/filters/bilateral_solver.py, bilateral_solver
Operator code engine/src/pixelnexus_engine/filters/bilateral_solver.py, bilateral_solver_representation

Overview

The bilateral solver of Barron and Poole turns edge-aware smoothing into an optimization problem. It looks for the image that is as smooth as possible while it stays close to the input. Smooth here means that pixels near each other with similar colors get similar values. Pixels on either side of an edge between two colors do not have to agree, so the edge stays sharp.

Solving that problem on all pixels would be slow. The solver works on a coarse bilateral grid instead, whose cells group pixels of similar position, brightness and color. That makes the problem hundreds of times smaller. PixelNexus smooths the image guided by itself, with the same confidence everywhere.

Three panels: a step and three thin lines with strong noise, the same at lambda 0, still noisy, and at lambda 128, clean with a sharp step and sharp lines. A few isolated specks remain. Three panels: a step and three thin lines with strong noise, the same at lambda 0, still noisy, and at lambda 128, clean with a sharp step and sharp lines. A few isolated specks remain.
At lambda 0 each pixel gets the mean of its grid cell. At lambda 128 cells of similar color agree, and the noise goes.

How it works

  1. Sort the pixels into cells by position, brightness and color.
  2. Ask every cell to be close to the mean of its pixels, and similar to its neighboring cells.
  3. Solve for all cells at once.
  4. Give each pixel the value of its cell.

Mathematical foundation

\[ \min_{\mathbf{y}}\; \frac{\lambda}{2} \sum_{i,j} \hat{W}_{ij} (y_i - y_j)^2 + \sum_i c_i (y_i - t_i)^2,\qquad \Big(\lambda\,(D_m - D_n \bar{B} D_n) + \operatorname{diag}(S\mathbf{c})\Big)\, \mathbf{y} = S(\mathbf{c} \circ \mathbf{t}),\qquad \hat{\mathbf{x}} = S^{\top} \mathbf{y} \]

The formula shows the default parameter values.

where

  • \(\mathbf{t}\): The input channel
  • \(\mathbf{c}\): The confidence of each pixel, 1 here
  • \(\lambda\): The smoothness weight
  • \(S\): The matrix that adds pixels into grid vertices
  • \(\bar{B}\): The blur over neighboring vertices, [1, 2, 1] in each of the 5 dimensions
  • \(D_n, D_m\): The diagonal scalings from bistochastization
  • \(\mathbf{y}\): The values at the vertices

t is the input channel and c the confidence of each pixel, here 1 everywhere. The first sum asks for similar values where the bilateral affinity W is high: near in position and color. On the grid, S adds pixels into vertices and the blur B joins neighboring vertices. D_n and D_m come from bistochastization, which scales B so that its rows keep the pixel counts. The problem then becomes the sparse linear system shown, one unknown per vertex. Its solution y is sliced back to the pixels by S transposed.

The bilateral affinity W of two pixels is high when they are close in position and in color. On the grid, it becomes S transposed B S: two pixels are linked when their cells are the same or next to each other. Bistochastization scales the rows of B so that each one sums to the number of pixels of its vertex. Without it, cells with many pixels would pull harder. The problem is quadratic, so setting its gradient to 0 gives a linear system. The system is sparse and positive definite, which suits the conjugate gradient method.

At lambda 0 the smoothness term vanishes, and every pixel gets the mean of its cell. With noise of sigma 20 and a brightness sigma of 16, noise spreads the pixels over many cells, so this mean removes little noise. Larger lambda joins neighboring cells, and the noise goes.

Kernel or operator

There is no fixed kernel. The Inspector shows the grid, the system and the slice with the current values.

Algorithm

  1. Give each pixel the grid cell of its position, brightness and color, each divided by its sigma and rounded down.
  2. Build S, which adds pixels into cells, and the blur B over neighboring cells.
  3. Scale B by bistochastization, in 10 steps.
  4. For each color channel, solve the sparse linear system with preconditioned conjugate gradients.
  5. Give each pixel the value of its cell.

Parameters

Parameter Default Values Meaning
Spatial sigma sigmaSpatial 8 2 to 64 px The cell size of the grid in x and y. Pixels further apart than this rarely share a vertex.
Brightness sigma sigmaLuma 16 2 to 64 The cell size of the grid in brightness Y, in levels.
Color sigma sigmaChroma 16 2 to 64 The cell size of the grid in Cb and Cr, in levels.
Smoothness lambda lambda 128 0 to 1024 The weight of smoothness against closeness to the input.

Implementation

bilateral_solver.py in engine/src/pixelnexus_engine/filters builds the grid with numpy.unique over the rounded-down cell coordinates, and S and B as SciPy sparse matrices. It solves with scipy.sparse.linalg.cg, with the diagonal of A as a preconditioner, from the cell means. It stops at a relative tolerance of 0.00001 or after 50 steps. The guide in Y'CbCr, the fixed confidence and the defaults are PixelNexus's own.

Worked example

Result PSNR (dB)
Noisy 22.14
Lambda 0 22.45
Lambda 32 27.82
Lambda 128 31.15
Lambda 512 32.43

PSNR against the clean image of the figure, 120 x 120, over the three channels. Gaussian noise of sigma 20 in each channel, rounded and clipped to 8 bits. The other parameters have their defaults: spatial sigma 8, brightness and color sigma 16.

The PSNR rises with lambda, fast at first. In the figure, at lambda 128, the step and the thin lines stay sharp, since their colors fall into other cells than the background.

Complexity

  • Time: \(O(N + V k)\)
  • Memory: \(O(N + V)\)

N is the number of pixels, V the number of occupied grid cells, often hundreds of times fewer, and k the conjugate gradient steps, at most 50.

Artifacts and limitations

Limitations

  • Pixels of the same cell always get the same value, so fine detail inside a cell is lost.
  • Cells are cut at fixed boundaries, so two similar colors on either side of a boundary are not joined.

Artifacts

  • Isolated noise pixels with a far-off color stay as specks, alone in their cell.
  • Slight banding in smooth ramps, at the brightness boundaries of the cells.

Tests

The tests check what follows from the definitions. A flat image comes back unchanged. The vertices are exactly the rounded-down cells. At lambda 0 each pixel gets the mean of its cell. After bistochastization, the scaled blur keeps the pixel count of each vertex within 5 percent. The result solves the linear system. On a noisy disk, the PSNR rises by more than 6 dB, while the pixels on either side of the edge stay over 100 levels apart.

References

  • Barron, J. T., and Poole, B. The fast bilateral solver. Computer Vision, ECCV 2016, Lecture Notes in Computer Science, 617 to 632, 2016. DOI 10.1007/978-3-319-46487-9_38.