Skip to content

Bokeh blur

Fact Value
Also called Lens blur, Disk blur with highlights
Status Implemented and tested
Family Artistic and geometric effects
Engine Browser
Type Nonlinear, spatial, highlight boost and disk blur
Output Image
Operator shown as algorithm (exact)
Support radius ceil(radius - 0.5)
Code web/src/core/filters/artistic/lens.ts, bokehFilter
Operator code web/src/core/filters/artistic/lens.ts, bokehPlanes

Overview

Bokeh is the look of the out-of-focus parts of a photo. A lens spreads each point of light into a disk, so small lights become bright circles. This filter blurs with a disk in linear light and first boosts the highlights, which a photo usually clips.

Two panels: small colored lights on a dark blue background, and the same lights blurred into soft round disks, the white ones bright. Two panels: small colored lights on a dark blue background, and the same lights blurred into soft round disks, the white ones bright.
Small lights on a dark background, and the bokeh blur with radius 8 and highlight boost 6.

How it works

  1. Decode the image to linear light.
  2. Scale up the highlights, smoothly from the threshold to white.
  3. Blur with a disk kernel.
  4. Encode again and clip.

Mathematical foundation

\[ c_b = c \left(1 + \beta\, \operatorname{smoothstep}\left(\frac{Y - t}{1 - t}\right)\right), \quad c' = D_R * c_b, \qquad R = 8,\ t = \frac{ 75 }{100},\ \beta = 4 \]

The formula shows the default parameter values.

where

  • \(c\): A channel in linear light
  • \(Y\): The linear luminance
  • \(D_R\): The disk kernel of radius R
  • \(t, \beta\): The highlight threshold and the boost

c is a channel in linear light and Y the linear luminance. A lens that is out of focus spreads each point of light into a disk, its circle of confusion. Bright lights in a photo are often clipped at white, so their true light is lost. The filter scales highlights up by a factor that rises smoothly from 1 at the threshold to 1 + beta at white. So they act as if they had been brighter. Then it blurs with the disk kernel of the disk averaging filter. Bright points spread into bright, hard-edged disks, while dark areas blur normally. The results are clipped when encoded. The rule is PixelNexus's own.

In the thin lens model of Potmesil and Chakravarty, a point out of focus images to a disk, the circle of confusion. Blurring with a disk kernel imitates that for one depth. A clipped highlight has lost its true brightness, so a disk blur alone would spread it into a dim disk. The boost puts back some of the lost light.

Kernel or operator

The Inspector shows the steps of the effect as formulas with the current settings.

Algorithm

  1. Decode the image to linear light.
  2. Scale up the highlights by the smoothstep of how far they are above the threshold.
  3. Blur with the disk kernel.
  4. Encode back to sRGB and clip.

Parameters

Parameter Default Values Meaning
Radius radius 8 1 to 30 px The radius of the disk, the shape of an out-of-focus point.
Highlight threshold threshold 75 0 to 100 % The linear luminance where highlights start to be boosted.
Highlight boost boost 4 0 to 20 How much the brightest highlights are scaled up before the blur.
Border mode border Reflect 101 (default) Reflect 101 (default), Reflect, Replicate, Constant (zero), Wrap How pixels outside the image are filled when the window reaches past the edge.

Implementation

bokehFilter in web/src/core/filters/artistic/lens.ts uses bokehPlanes and the disk kernel of the disk averaging filter.

Worked example

Distance from the white light 0 3 6 9 12
boost 0 100 100 94 54 10
boost 4 207 207 195 115 10

The red channel along the row through the white light, with radius 8. Without the boost, the 5 by 5 light spreads its light thinly over the disk. With the boost, it fills a much brighter disk. Both fall to the background between 9 and 12 pixels away, the disk radius plus the half width of the light.

Complexity

  • Time: \(O(N R^2)\)
  • Memory: \(O(N)\)

N is the number of pixels and R the disk radius. The disk is not separable.

Artifacts and limitations

Limitations

  • The whole image is blurred the same. Real bokeh depends on depth.
  • Slow for large radii.

Artifacts

  • Bright areas grow and lose detail at high boosts.

Tests

The reference writes the boost and the disk blur again with NumPy and SciPy, and compares every output value on the test images of the fixtures.

References

  • Potmesil, M. and Chakravarty, I. A lens and aperture camera model for synthetic image generation. ACM SIGGRAPH Computer Graphics, 297 to 305, 1981. DOI 10.1145/965161.806818.