# What is the advantage of weighted median filter over median filter?

I could not find any proof or comparison of weighted median and median filters. Yes the more occurring pixel contributes much but what should be the criteria of choosing weight function. And any explanation of cases where weighted median is strongly recommended to be used.

Median Filter replaces pixel value c with p where p is the median of pixel values in neighborhood of c

In the case of weighted median there are N $\left[ I_1,I_2,..,I_N \right]$ neighbor pixels,for each pixel there is also weight. Weighted median of that neighborhood is $k^{th}$ pixel where $k$ is the minimum integer with $\sum\limits_{i=1}^k w_{i}\geq\frac{1}{2}\sum\limits_{i=1}^N w_{i}$

• well, i'm not into image processing (mentioning "pixel" hints at that), but i know what a median filter is. thing is, i dunno what a weighted median filter is. weighting samples in a mean makes some sense but i dunno what the sense is if you change the values of some samples, relative to others, before essentially sorting them. what is the rhyme or reason that sample $x[n_1]$ should be boosted over sample $x[n_2]$ when otherwise $x[n_1]<x[n_2]$? when sample order is swapped or changed, why are some samples selected and not others? Mar 29 '15 at 3:58
• You need to provide some context to your question - what is the problem you face, specific - image, videos, audio, sonar....?
– Moti
Mar 29 '15 at 5:33
• @Moti that's what I am asking,spesific images. I am updating question. Mar 29 '15 at 9:13
• I see the weights. Where are the image values? There is no sum in median filter (to the best of my knowledge) - this is usually an order filter that you pick a value that is the middle of the list. Weights may be used to remove certain bias, such as a slop background.
– Moti
Mar 29 '15 at 15:34
• This is not a median , this is WEIGHTED MEDIAN. If all weights are 1 then it simply becomes traditional median filter. Mar 29 '15 at 17:57

In 1D, you should remember that the median $$\hat{m}$$ minimizes a sum of absolute values ($$\ell_1$$ norm): $$\hat{m} = \arg \min \sum_{k=1}^k |x_k-m|.$$ You can find out that the answer (in 1D only) is the center value of the $$x_k$$ for an odd number of samples, and any value between the two center values for an even number of samples (traditionally their average).

The weighted median, to me, boils down to: $$\hat{m}_w = \arg \min \sum_{k=1}^k w_i|x_k-m|,$$ just like the weighted mean $$\hat{M}$$ minimizes ($$\ell_2$$ norm): $$\hat{M} = \arg \min \sum_{k=1}^k w_i|x_k-M|^2.$$ In the definition I use, let $$x=[1,2,3,2,4]$$. The standard median is $$2$$, uniquely based on the rank. You can then take integer weights, like $$w=[1,2,5,2,1]$$. They aim at limiting the pure ranking effect of the median, and at introducing some spatialisation or "refocused location". A weighted median would consist in duplicating/triplicating/$$n$$-plicating the initial values with respect to the weights and their respective location: $$x_w=[1,2,2,3,3,3,3,3,2,2,4]$$ and take the median of the new data: $$3$$. The definition extends to rational and real weights (perhaps complex).

The advantages of the weighted median in images are mostly two-fold, since you can recover the median with $$w_k=1$$:

1. Restore some spatialisation, absent in the traditional median, which generates "moving edges", by better centering the median around the central pixel of the square window (if weights in the mask are shapes like a pyramid). That is evident from the example above: the median picks $$2$$, but the weighting answer $$3$$, as a central edge, would be a better choice,
2. Allow negative weights, to better mimic not only smoothing filters (positive weights) but also "median-derivative-like" filters.

One of my sources is Nonlinear Image Processing, by Mitra & Sicuranza.

So the weighted median is always "better" as more generic, provided you can find a neat weighting. To recenter, a pyramidal shape (centered at the center pixel of the square) ought to be better than a flat mask. For instance, take a weighting based on Pascal triangle coefficients.

The concept of a true median in $$n$$ dimensions is more complicated than the above procedure, since there is no "natural ordering" (compatible with some operations) in 2D, and requires optimization.

Weighting is a common approach to control the importance per pixel. In other words, one might require or desire different importance for different pixels. Then, the 50% percentile is estimated via taking the weights into account. Instead of treating each pixel equally, the sorting function is tweaked to consider the weights.

Imagine, we want to process pixel $p$ in image $I$, in the neighborhood $R(p)$. The radius of this local window $R$ is $r$. Number of pixels in such locality is $(2r+1)^2$. For each neighbor $q \in R(p)$, a weight $w_{pq}$ is associated. The typical choice of weighting is the affinity of $p$ and $q$ in the feature map $\mathbf{f}$, which can be any feature, but in general selected to be intensity, color and etc. So we write

$w_{pq}=g(\mathbf{f}(p), \mathbf{f}(q))$

A reasonable choice for function $g$ is Gaussian (common preference for affinity measures):

$g=exp(-\frac{\lVert \mathbf{f}(p)-\mathbf{f}(q) \rVert}{\sigma^2})$

The weighted median operates as follows:

$p^*=\min{k}$ s.t. $\sum\limits_{q=1}^k w_{pq} \geq \frac{1}{2}\sum\limits_{q=1}^n w_{pq}$

This means that for all pixels before the median point $p^*$, the sum is roughly half of all weights summed together.

If you have some knowledge of how the noise behaves (e.g. you have vertical streaks in your image typically, rather than salt and pepper noise), you can adjust the pixel weights so that you prefer to use pixels to the sides of the pixel being filtered more than those above and below.

Depending on the image as well, you can also reduce the influence of pixels farther from the center of the window. This can be useful in things like imagers where if you have pixels

1 2 3

4 5 6

7 8 9

and each pixel represents say a 10 x 10 meter square, you may want to weight pixels 1,3,7,9 lower than 2,4,6,8 since they are farther from the area imaged in pixel 5. In this case, if there are vertical streaks, you'd weight down pixels 2 and 8.