# Symmetric nearest neighbour filter

The SNN smoothing filter is designed to preserve edges in data and is very effective at noise reduction. It compares opposite pairs of pixels in the support, and selects those that are closest in value to the central input pixel (see the figure). It then returns the mean of the selected pixels. In this way, it avoids smoothing across edges. In my experience, it is somewhere between the median and Kuwahara filters in terms of edge preservation. It is a good choice for filtering a faulted horizon, for which edge preservation is paramount. According to Hall (2007^{[1]}) it is the most versatile filter in the interpreter’s toolkit.

## Example

Result **h** was generated from noisy **b** to try to recover original **a**. See smoothing filters for the other methods.

## Implementation in Python

This is how the algorithm is implemented in **bruges**. It's rather slow, but it works.

```
import numpy as np
import scipy.ndimage
def snn(horizon, size=5):
def nearest(a, num):
return a.flat[np.abs(a - num).argmin()]
def func(this, pairs):
centre = this[this.size // 2]
select = [nearest(this[p], centre) for p in pairs]
return np.mean(select)
pairs = [[i, size**2-1 - i] for i in range(size**2 // 2)]
return scipy.ndimage.generic_filter(horizon, func, size=size,
extra_keywords={'pairs': pairs}
)
```

## See also

## References

- ↑ Hall, M (2007). Smooth operator: smoothing seismic horizons and attributes.
*The Leading Edge***26**(1), January 2007, p 16-20. DOI:10.1190/1.2431821