Vector anisotropic diffusion¶
| Fact | Value |
|---|---|
| Also called | Color anisotropic diffusion |
| Status | Implemented and tested |
| Family | Nonlinear spatial filters |
| Engine | Browser |
| Type | Nonlinear, iterative, nonlinear diffusion |
| Output | Image |
| Operator shown as | iterative (exact) |
| Support radius | iterations |
| Code | web/src/core/filters/nonlinear/diffusion.ts, vectorAnisotropicDiffusionFilter |
| Operator code | web/src/core/filters/nonlinear/diffusion.ts, conduction |
| Channels | RGB |
Overview¶
Vector anisotropic diffusion is the color version of anisotropic diffusion. Instead of a separate conduction for each channel, all channels share one conduction from the length of the color difference. An edge in any channel blocks the flow in all three, so edges stay in the same place in every channel, and colors do not bleed into each other.
How it works¶
- Find the color difference between each pixel and its four neighbors.
- Turn the length of each color difference into one conduction for all channels.
- Move each channel toward the neighbors by the step size times the differences times the conductions.
- Repeat for the chosen number of iterations.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(\mathbf{I}\): The color vector of a pixel
- \(\lVert \nabla_n \mathbf{I} \rVert\): The length of the color difference to neighbor n
- \(g\): The conduction function
- \(\lambda\): The step size
Bold I is the color vector (R, G, B). nabla_n is the color difference to the neighbor in direction n. Its length is the Euclidean distance between the two colors. All channels share the conduction g of that length, as Sapiro and Ringach propose for multivalued images.
Sapiro and Ringach treat a color image as a vector-valued image and let the diffusion depend on the norm of the vector gradient. In the discrete form here, the norm of the difference to each neighbor is the Euclidean distance between the two colors. For a gray image, that distance is the square root of 3 times the gray difference. So the same edge needs a K that is the square root of 3 times larger than in the scalar version.
Kernel or operator¶
The Inspector shows the update step with the shared conduction. The worked example below runs one step for a red pixel next to a green one.
Algorithm¶
- Extend the image by one pixel per iteration with the border mode.
- For each pixel and neighbor, compute the color difference and its length.
- Add lambda times the difference times the conduction of the length, to all three channels.
- Repeat, then cut the extension off.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Iterations iterations |
15 | 1 to 100 | How many update steps run. |
Edge threshold K kappa |
30 | 1 to 150 | The color difference, in 8-bit levels, at which the conduction falls off. |
Step size lambda |
0.2 | 0.01 to 0.25 | How far each step moves a color toward its neighbors. At most 0.25 keeps the scheme stable. |
Conduction conduction |
Exponential, exp(-(d/K)^2) | Exponential, exp(-(d/K)^2), Rational, 1 / (1 + (d/K)^2) | The function g that turns a color difference into a conduction. |
Border mode border |
Reflect | Reflect 101 (default), Reflect, Replicate, Constant (zero), Wrap | How pixels outside the image are filled when the window reaches past the edge. |
Implementation¶
vectorAnisotropicDiffusionFilter in web/src/core/filters/nonlinear/diffusion.ts shares the step code with the scalar version, with one conduction for the three planes. The extension by one pixel per iteration works the same way. It is a color filter: the framework in web/src/core/filters/run.ts hands it the red, green and blue planes together, with the border mode, and keeps alpha. Each strip of the image gets a halo as wide as the support radius, so strips give exactly the pixels of a whole-image run.
Worked example¶
One step for a red pixel (120, 60, 60), with K = 30 and lambda = 0.2. Its east neighbor is green.
| Neighbor | Color | Color difference | Length | g |
|---|---|---|---|---|
| North | (126, 62, 58) | (6, 2, -2) | 6.63 | 0.9523 |
| South | (118, 64, 61) | (-2, 4, 1) | 4.58 | 0.9769 |
| East | (60, 120, 60) | (-60, 60, 0) | 84.85 | 0.0003 |
| West | (124, 57, 63) | (4, -3, 3) | 5.83 | 0.9629 |
The green neighbor has a color difference of length 84.9, so its conduction is almost 0 in all three channels. The red neighbors flow in. The new color is (121.52, 60.59, 60.39), still red. The corners of the window do not take part, because only the four direct neighbors count.
Complexity¶
- Time: \(O(N\,T)\)
- Memory: \(O(N)\)
N is the number of pixels and T the number of iterations.
Artifacts and limitations¶
Limitations
- Noise in one channel can block smoothing in the others.
- Many iterations are slow.
Artifacts
- Flat color patches after many iterations.
Tests¶
The reference runs the same steps with NumPy. Before it is used, the script checks that on a gray image it equals the scalar version with K divided by the square root of 3. The test compares the filter on a color input for all five border modes.
References¶
- Sapiro, G. and Ringach, D. L. Anisotropic diffusion of multivalued images with applications to color filtering. IEEE Transactions on Image Processing 5(11), 1582 to 1586, 1996. DOI 10.1109/83.541429.
- Perona, P. and Malik, J. Scale-space and edge detection using anisotropic diffusion. IEEE Transactions on Pattern Analysis and Machine Intelligence 12(7), 629 to 639, 1990. DOI 10.1109/34.56205.