---
title: Li–Osher Weighted Median Filter
url: https://www.emergentmind.com/topics/weighted-median-filtering-li-osher
type: topic
---

# Li–Osher Weighted Median Filter

The Li–Osher weighted median filter is an edge-preserving nonlinear filter defined by a local, weighted minimization of the $L^1$ loss. It extends the classical median filter by allowing heterogeneous weighting of observations, resulting in enhanced adaptability to image and motion boundaries. The weighted median, as introduced by Li and Osher in the context of PDE-based denoising, has been leveraged as a critical post-processing refinement in optical flow methods to better preserve flow discontinuities while suppressing noise. Doshi and Kiran [2207.10302] implement this filter as the final sharpening step in an edge-preserving variational optical-flow pipeline, reporting marked improvements in both flow accuracy and edge fidelity.

## 1. Mathematical Formulation

At each pixel $x \in \Omega$, for a scalar signal $u(x)$ (e.g., a flow component), the filter operates on the local square neighborhood 
$$
W_R(x) = \{ y \in \Omega : \| y-x \|_\infty \le R \}
$$
with half-width $R$. Each neighbor $y$ is assigned a nonnegative weight $w(x, y)$. The filtered output $v(x)$ is the minimizer of the weighted $L^1$ cost:
$$
v(x) = \underset{z \in \mathbb{R}}{\arg\min} \sum_{y \in W_R(x)} w(x, y)\,|z - u(y)|.
$$
Alternatively, defining the nondecreasing order statistics $u_{(1)} \le u_{(2)} \le \dots \le u_{(m)}$ (with corresponding weights $w_{(j)}$), $v(x)$ is the smallest $u_{(k)}$ for which the cumulative weight $W_-(k) = \sum_{j=1}^k w_{(j)}$ satisfies $W_-(k) \ge \frac{1}{2} \sum_{j=1}^m w_{(j)}$.

## 2. Parameters and Weight Computation

Three principal parameters define the filter:

- **Window size $R$**: Typical choices are $R \in \{4, 5, 7, 9, \ldots, 13\}$, selected and tuned per dataset or sequence.
- **Patch similarity weights $w(x, y)$**: For any $y \in W_R(x)$,
$$
w(x, y) = \exp\Bigg( -\frac{1}{h^2} \int_{t\in\Omega} G_\delta(t) |f(x+t) - f(y+t)| \, dt \Bigg),
$$
where $G_\delta$ is a Gaussian with standard deviation $\delta$ (patch-smoothing), $h$ is a contrast-sensitivity parameter (often the flow grid spacing), and $f$ is the reference image. Both $\delta$ and $h$ are jointly tuned per sequence.
- **(Optional) Regularization term**: The generalized energy $E(z) = \sum_i w_i |z-u_i| + F(z)$ is used with $F(z)=0$, yielding a pure weighted median.

Weights are computed based on patch similarity, following the nonlocal means paradigm, with larger $w(x, y)$ for similar patches.

## 3. Algorithmic Implementation

A single pass proceeds as follows for each pixel $x$:

1. **Neighborhood extraction**: Collect $y$ with $\|y-x\|_\infty \le R$.
2. **Weight computation**: For each $y$, compute $w_y = \exp\left( -\frac{1}{h^2} \sum_{t \in \text{patch}} G_\delta(t) \, |f(x+t) - f(y+t)| \right)$.
3. **Sort and select**: 
   - Form the set of pairs $\{(u(y), w_y)\}$ for all $y$.
   - Sort by $u(y)$ in ascending order.
   - Accumulate weights, select the minimal $u(y)$ at which accumulated weight passes half the total, set as $v(x)$.

Boundary handling is performed by mirror-padding or clamping at image edges. Weights $w(x, y)$ are recomputed only once per sequence or iteration and cached. Sorting is implemented via bucket sort or $O(m)$ selection algorithms, where $m = (2R+1)^2$. Per-pixel computational complexity is $O(R^2)$ for both weight lookup and median selection.

## 4. Integration in Optical Flow Estimation

The weighted median filter is applied as a post-processing refinement after coarse-to-fine variational optical flow estimation with $L^1$-TV regularization solved by the Chambolle–Pock primal–dual scheme. At each pyramid level, after the final (possibly iterated) classical median filtering, the Li–Osher weighted median filter is applied once per flow component. The output is upsampled for initialization at the next pyramid level. Only a single weighted median pass is conducted per level, with the final, full-resolution pass being emphasized for edge sharpening.

Pipeline summary:

| Step                      | Operation                                  | Location in Pipeline                    |
|---------------------------|--------------------------------------------|-----------------------------------------|
| Solve variational flow    | Chambolle–Pock primal-dual                 | Per pyramid level                       |
| Iterated median           | Standard (unweighted) median, possibly multiple passes | Per pyramid level             |
| Weighted median           | Li–Osher filter, one pass per flow component | After all variational and iterated median steps |

## 5. Empirical Performance and Edge Preservation

Application of the weighted median filter as the final refinement sharpens flow-field discontinuities—specifically at object boundaries—mitigating “bleeding” artifacts intrinsic to $L^1$-TV regularization. On the Middlebury optical flow dataset, the end-to-end pipeline of Doshi & Kiran, which incorporates both iterated median and Li–Osher weighted median filtering, achieves an average angular error (AAE) of $3.791^\circ$ and an average end-point error (EPE) of $0.362$ pixels, improving upon HS+nonlocal ($4.508^\circ$, $0.370$ px) and HS+nonlocal+guided filter ($\sim 4.28^\circ$, $0.388$ px). Approximately $5\!-\!10\%$ of the edge fidelity improvement is attributed to the weighted median step, as observed by the authors. In controlled tests with synthetic data, the filter preserves sharp motion edges even under heavy Gaussian or salt-and-pepper noise, outperforming pure iterated median filtering in maintaining crisp vector-field boundaries [2207.10302].

## 6. Implementation Optimizations

To ensure computational feasibility, weights are cached in compact ($2R+1)^2$ arrays per-pixel or per-warp. Efficient $O(R^2)$ selection strategies are employed for the median calculation. No GPU acceleration is reported, but the algorithm's pixelwise independence is well-suited for parallelization on architectures such as CUDA. For $R \le 13$, real-time application is considered practically attainable on modern hardware.

## 7. Context, Limitations, and Further Directions

The Li–Osher weighted median filter generalizes the median to accommodate spatially or photometrically varying influences, enhancing edge-preservation over classical filters. Its use in variational optical flow estimation demonstrates the practical utility of patch-based weighting rooted in nonlocal means concepts. While the improvement in flow accuracy attributable solely to the weighted median is not isolated in ablation studies, integration within existing variational frameworks yields measurable benefits in edge sharpness and accuracy under noise. A plausible implication is that further optimization, adaptive parameter learning, or extension to higher-dimensional flows could enhance performance in related imaging and motion analysis applications [2207.10302].

Source: https://www.emergentmind.com/topics/weighted-median-filtering-li-osher