# [GRASS-user] Gauss filter in r.neighbors

Glynn Clements glynn at gclements.plus.com
Thu Oct 2 05:27:51 PDT 2014

```Martin Album Ytre-Eide wrote:

> I am using the the r.neighbors function and I have an idea about
> what the 'Gauss' parameter will do for me, but I can't find any
> documentation for it. Experimenting with it, left me clueless.

Using gauss= is equivalent to using weight= with a weights file
corresponding to a Gaussian filter.

The exact weights which are used can be found in the
gaussian_weights() function in raster/r.neighbors/readweights.c. They
are effectively:

ncb.weights[i][j] = exp(-(x*x+y*y)/(2*sigma2))/(2*M_PI*sigma2);

where x, y is the offset (in cells) from the centre of the
neighbourhood and sigma2 is the square of the value passed to the
gauss= option.

> What I won't to do:

> Apply a Gaussian weighting of cell neighbors and sum the values of
> the neighbors. If all cells=1, I would get only 1's back.

You need to use method=average to make the effective weights sum to
one (when weights are used, method=average divides the weighted sum of
the cell values by the sum of the weights).

IIRC, the weights are such that they would sum to one if you had an
infinitely-large neighbourhood, but the fact that you're cropping the
curve to a finite neighbourhood means that the sum will always be less
than one.

The weights are determined by sigma alone, and are unaffected by the
neighbourhood size.

> I have a map of zeros and ones. I tried this:
>
> r.neighbors -c input='input_map' output='output_map' method=sum gauss=10 size=33
>
> The result is a map with only zeros.

It works for me; e.g.

\$ r.mapcalc --o 'foo = rand(0,2)' seed=1
\$ r.neighbors --o -c in=foo out=bar method=average size=33 gauss=10
\$ r.info -r bar
min=0.442173333781042
max=0.568925311919939
\$ r.neighbors --o -c in=foo out=bar method=sum size=33 gauss=10
\$ r.info -r bar
min=0.0985901293262625
max=0.449425795562072

\$ r.mapcalc --o 'foo = 1'
\$ r.neighbors --o -c in=foo out=bar method=average size=33 gauss=10
\$ r.info -r bar
min=1
max=1
\$ r.neighbors --o -c in=foo out=bar method=sum size=33 gauss=10
\$ r.info -r bar
min=0.221413499330273
max=0.812157275619236

--
Glynn Clements <glynn at gclements.plus.com>
```