Cartoon effect¶
| Fact | Value |
|---|---|
| Also called | Cartoonize, Toon effect |
| Status | Implemented and tested |
| Family | Artistic and geometric effects |
| Engine | Browser |
| Type | Nonlinear, spatial, bilateral smoothing with line mask |
| Output | Image |
| Operator shown as | algorithm (exact) |
| Support radius | max(passes * ceil(3 sigmaSpace), 2 + (lineSize - 1) / 2) |
| Code | web/src/core/filters/artistic/painterly.ts, cartoonFilter |
| Operator code | web/src/core/filters/artistic/painterly.ts, cartoonLines |
Overview¶
The cartoon effect flattens colors and draws black lines along the edges. The flattening comes from repeated bilateral filtering, which smooths within regions but not across edges. The lines come from an adaptive threshold of the median filtered luma.
How it works¶
- Smooth the colors with the bilateral filter in color mode, once per pass.
- Round the luma to whole levels and take its 5 by 5 median.
- Mark the pixels that are darker than their local mean by more than the threshold.
- Paint the marked pixels black and the rest with the smoothed colors.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(C_s\): The smoothed color
- \(M\): The median filtered luma
- \(k, s, d\): The passes, the line window and the line threshold
The colors are smoothed by k passes of the bilateral filter of Tomasi and Manduchi in color mode, which flattens areas while keeping edges. The lines come from the luma Y, rounded to whole levels and median filtered over 5 by 5 to remove noise. A pixel is a line where this median is darker than its local mean over s by s by more than d, the rule of the adaptive mean threshold. Lines are black, and all other pixels take the smoothed color. The recipe is PixelNexus's own.
Each bilateral pass averages a pixel with neighbors of similar color, so repeated passes flatten regions while edges stay. The median removes noise and fine texture before the lines are found. The lines are the dark side of each edge, where a pixel is darker than its neighborhood.
Kernel or operator¶
The Inspector shows the steps of the effect as formulas with the current settings.
Algorithm¶
- Smooth the colors with the bilateral filter in color mode, the chosen number of times.
- Round the luma to whole levels and take its 5 by 5 median.
- Mark the pixels whose median is darker than its local mean by more than the threshold.
- Paint the marked pixels black and the others with the smoothed colors.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Smoothing size sigmaSpace |
3 | 0.5 to 10 px | The spatial sigma of each bilateral pass. |
Color tolerance sigmaRange |
40 | 1 to 150 | The range sigma of each bilateral pass, a distance between RGB colors. |
Passes passes |
2 | 1 to 5 | How many times the bilateral filter runs. |
Line window lineSize |
9 | 3 to 31, odd px | The window of the local mean that the lines compare with. |
Line threshold lineOffset |
4 | 0 to 30 | How much darker than its surroundings a pixel must be to become a line. |
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¶
cartoonFilter in web/src/core/filters/artistic/painterly.ts uses bilateralPlanes from the bilateral filter, medianPlane, boxMean and cartoonLines.
Worked example¶
| Line threshold | Line pixels (%) | Distinct colors |
|---|---|---|
| 1 | 15.2 | 123 |
| 2 | 8.8 | 124 |
| 4 | 8 | 124 |
| 8 | 7.5 | 124 |
The scene of the figure has 1353 distinct colors. Two bilateral passes cut that number down, and a higher line threshold keeps only the strongest edges as lines.
Complexity¶
- Time: \(O(N k r^2)\)
- Memory: \(O(N)\)
N is the number of pixels, k the passes and r the bilateral radius, 3 sigma.
Artifacts and limitations¶
Limitations
- Slow with large smoothing sizes and many passes.
- Fine texture can still give many small lines.
Artifacts
- Speckled lines in noisy or textured areas.
Tests¶
The reference writes the recipe, with the bilateral reference of the bilateral filter again with NumPy and SciPy, and compares every output value on the test images of the fixtures. A first version of the reference took the median of unrounded luma and missed one pixel, which showed that the median filter works on whole levels. The rule now says so.
web/src/core/filters/filters.test.tsweb/src/core/filters/run.test.tsweb/src/core/filters/artistic/artistic.test.ts
References¶
- 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.