Outline¶
| Fact | Value |
|---|---|
| Also called | Contour, Pillow CONTOUR |
| Status | Implemented and tested |
| Family | Artistic and geometric effects |
| Engine | Browser |
| Type | Linear, spatial, Laplacian with offset |
| Output | Image |
| Operator shown as | fixed-kernel (exact) |
| Support radius | 1 |
| Code | web/src/core/filters/artistic/kernels.ts, outlineFilter |
| Operator code | web/src/core/filters/artistic/kernels.ts, CONTOUR_ROWS |
| Channels | RGB, Brightness only |
Overview¶
Outline turns an image into dark lines on white. It is Pillow's CONTOUR filter: a Laplacian plus 255, so flat areas are white and the darker side of each edge gets a dark line.
How it works¶
- Multiply each pixel by 8 and subtract its 8 neighbors.
- Add 255, so that flat areas become white.
- Clip the result to 0 to 255.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(I(x, y)\): The input at the pixel
This is Pillow's CONTOUR filter: 8 in the center and -1 at the 8 neighbors, plus 255. The sum without the offset is a Laplacian, 0 in flat areas. So flat areas turn white. A pixel darker than its neighbors gives a negative sum and a dark line. A pixel brighter than its neighbors gives a value above 255, which clips to white. So each edge gets a dark line on its darker side.
At a step from dark to bright, the pixel on the dark side is darker than the mean of its neighbors, so its sum is negative and it turns dark. The pixel on the bright side has a positive sum, which the offset pushes past 255, so it stays white. That is why the line is one pixel wide and lies on the dark side.
Kernel or operator¶
The Inspector shows the 3 x 3 kernel and the offset of 255.
Algorithm¶
- Multiply the pixel by 8 and subtract its 8 neighbors.
- Add 255 and clip to 0 to 255.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
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. |
Output mapping outputMapping |
Clip to 0 to 255 | Absolute value, Offset by 128, Stretch to full range, Clip to 0 to 255 | How signed results, such as gradients, become 8-bit pixel values. |
Channels channelScope |
RGB | RGB, Brightness only | Which part of the color image the filter changes. |
Implementation¶
outlineFilter in web/src/core/filters/artistic/kernels.ts correlates the image with CONTOUR_ROWS and adds 255.
Worked example¶
| Filter | x = 3 | x = 4 | x = 5 | x = 6 | x = 7 | x = 8 |
|---|---|---|---|---|---|---|
| Input | 50 | 50 | 50 | 200 | 200 | 200 |
| Emboss | 128 | 128 | 128 | 255 | 128 | 128 |
| Outline | 255 | 255 | 0 | 255 | 255 | 255 |
| Edge enhance | 50 | 50 | 0 | 255 | 200 | 200 |
| Edge enhance, more | 50 | 50 | 0 | 255 | 200 | 200 |
The middle row of a 12 by 3 image that steps from 50 to 200 between x = 5 and x = 6. Values are clipped to 0 to 255 and rounded. The emboss result depends on the pixel below and to the left, which is on the same side of a vertical step, so only the step itself shows.
Complexity¶
- Time: \(O(N)\)
- Memory: \(O(N)\)
N is the number of pixels.
Artifacts and limitations¶
Limitations
- Noise and texture become many small dark marks. Smooth the image first.
- Lines appear only on the darker side of an edge.
Artifacts
- Speckle in textured areas.
Tests¶
The fixture cases compare every pixel with scipy.ndimage.correlate and the same kernel, for three border modes. Before the cases are written, the result is checked against Pillow's CONTOUR away from the edge.
References¶
- Pillow documentation, ImageFilter module, built-in filters. https://pillow.readthedocs.io/en/stable/reference/ImageFilter.html