Bilateral Filter Edge Smoothing
A bilateral filter blends only neighbours that are close and look similar. Flat zones smooth out while true edges refuse to mix.
Why Does This Exist?
The Gaussian blurs across edges because it weighs by distance alone: a dark pixel happily blends with a bright neighbour. But most photos are flat regions divided by sharp boundaries, and mixing across a boundary is exactly what you must not do. The bilateral filter adds a second opinion, similarity, so pixels vote only when they are near and alike.
It is the edge-aware member of the smoothing family. Use it for denoising photos you will display, where median output looks patchy and Gaussian output looks melted.
Think of It Like This
A club with two bouncers
Entry needs two stamps: you must live nearby (spatial closeness) and dress like the regulars (similar brightness). A neighbour in the wrong outfit is turned away, which is how an edge pixel gets excluded from the blend. Gaussian clubs employ only the first bouncer, so anyone nearby walks in and the party, your edge, gets diluted.
How It Actually Works
Each neighbour's vote multiplies two Gaussian weights. The spatial weight falls with distance, set by (how far to look). The range weight falls with brightness difference, set by (how different a pixel may be and still count). is the center value and a neighbour:
The output is the weighted mean using , normalized by the weight sum. Across a strong edge the range term collapses toward zero, so the far side contributes nothing and the edge survives.
Worked example
Center pixel 50, neighbour A at 52, neighbour B at 200, with . Range weight of A is , nearly a full vote. Range weight of B is , effectively zero. Same distances, wildly different votes, and the edge between 50 and 200 never mixes.
Watch Out For
Range sigma too wide degrades to Gaussian
Crank far above the image contrast and every neighbour counts as similar, so the filter blurs across edges exactly like the Gaussian you meant to beat. The symptom is melted boundaries despite paying bilateral cost. Set near the noise level: small enough to reject true edges, large enough to accept grain.
The Quick Version
- The bilateral filter weights neighbours by distance times brightness similarity.
- Flat regions smooth normally while strong edges block blending across them.
- Sigma-s sets reach and sigma-r sets how different a voter may be.
- It costs far more than Gaussian because weights change at every pixel.
- Pick sigma-r near the noise level to keep edges while killing grain.