Skip to content

Oil-painting effect

Fact Value
Also called Oil paint, Paint daubs
Status Implemented and tested
Family Artistic and geometric effects
Engine Browser
Type Nonlinear, spatial, local mode of brightness bins
Output Image
Operator shown as algorithm (exact)
Support radius radius
Code web/src/core/filters/artistic/painterly.ts, oilPaintingFilter
Operator code web/src/core/filters/artistic/painterly.ts, oilPaint

Overview

The oil-painting effect turns each pixel into the average color of the brightness that most of its neighbors share. Small details vanish into flat patches, while strong edges stay, which looks like daubs of paint.

Two panels: a test scene with a blue sky, green ground and red, yellow and blue disks, and the oil painting version with flat patches and kept edges. Two panels: a test scene with a blue sky, green ground and red, yellow and blue disks, and the oil painting version with flat patches and kept edges.
A test scene, and the oil painting effect with radius 3 and 20 levels.

How it works

  1. Sort the luma of the pixels into equal bins.
  2. In the window around each pixel, find the most common bin.
  3. Output the mean color of the window pixels in that bin.

Mathematical foundation

\[ b(q) = \left\lfloor \frac{L\, Y(q)}{256} \right\rfloor, \quad b^* = \arg\max_b \#\{q \in W(p) : b(q) = b\}, \quad C'(p) = \operatorname{mean}\{C(q) : q \in W(p),\ b(q) = b^*\}, \qquad L = 20,\ r = 3 \]

The formula shows the default parameter values.

where

  • \(Y\): The luma of a pixel
  • \(b(q)\): The bin of pixel q
  • \(W(p)\): The window around pixel p
  • \(L\): The number of bins

Y is the luma 0.2126 R + 0.7152 G + 0.0722 B and W(p) the square window of side 2r + 1 around the pixel. The luma values fall into L equal bins. The most common bin in the window wins, and ties go to the darker bin. The output is the mean color of the window pixels in that bin. So a pixel takes the color of the brightness that most of its neighbors share, and small details vanish into patches. OpenCV's xphoto module implements this effect and cites Holzmann's book for it. OpenCV leaves pixels outside the image out of the window and quantizes the result. Here the window follows the border mode, and nothing is quantized.

The most common bin is the mode of a coarse histogram of the window. Near an edge, the window holds two groups of brightness, and the larger group wins. So the edge stays where it was, but each side becomes flat. The window slides along each row: one column leaves and one enters, so each step costs one column of the window.

Kernel or operator

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

Algorithm

  1. Compute the luma of every pixel and its bin.
  2. Slide the window along each row, keeping the count and the color sums of each bin.
  3. At each pixel, take the most common bin and output the mean color of its pixels.

Parameters

Parameter Default Values Meaning
Radius radius 3 1 to 10 px The radius r of the square window, which has 2r + 1 pixels on each side.
Levels levels 20 2 to 64 How many equal bins the luma falls into.
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

oilPaintingFilter in web/src/core/filters/artistic/painterly.ts uses oilPaint, which keeps the count and the color sums of each bin as the window slides.

Worked example

Settings Distinct colors
Input 1353
radius 1, 20 levels 673
radius 3, 20 levels 563
radius 3, 8 levels 617
radius 6, 8 levels 831

The number of different colors in the scene of the figure, before and after. Each pixel takes the mean color of its window's main bin, so far fewer colors remain than in the input. The count does not fall steadily with larger windows or fewer bins, since each window makes its own mean.

Complexity

  • Time: \(O(N r)\)
  • Memory: \(O(N + L)\)

N is the number of pixels and r the radius. Each step along a row removes one column of the window and adds one.

Artifacts and limitations

Limitations

  • Small windows give little effect, and large windows lose structure.
  • The patches have no brush direction.

Artifacts

  • Blocky, square patches, from the square window.

Tests

The reference writes the window rule again with NumPy and SciPy, and compares every output value on the test images of the fixtures. Before the cases are written, the script checks that no luma value lies on a bin edge, where 32-bit and 64-bit floats could disagree. A unit test checks that a flat image stays flat.

References