[GRASS-user] Do filters available in GRASS takes care of cell padding?

Glynn Clements glynn at gclements.plus.com
Fri Dec 4 13:30:50 PST 2015

Uttam Kumar wrote:

> If I am trying to use
> r.mfilter input=band5 output=band5_highpassfilter
> filter=/Users/Uttam/high_pass_filter
> where high_pass_filter is
> TITLE 5x5 High Pass
> -1 -1 -1 -1 -1
> -1 -1 -1 -1 -1
> -1 -1 24 -1 -1
> -1 -1 -1 -1 -1
> -1 -1 -1 -1 -1
> 1.) How do I ensure that cell padding is taken care using r.grow.distance?
> Do I need to apply r.grow.distance on the output of r.mfilter or it should
> be applied before r.mfilter on the input image (band5 in this case)?


If any cell in the filter window is null (including cells outside the
current region), the corresponding output cell will have the value of
the original input cell.

Consequently, the output from r.mfilter with a 5x5 kernel will have at
least a 2-cell border containing the original (unfiltered) cell

To avoid this, you need to enlarge the region by 2 cells in each
direction, use e.g. r.grow.distance to fill the space around the input
map, run r.mfilter on the result, then shrink the region back to the
original size and (optionally) resample the result to remove the

> 2.) Will use of r.resamp.filter will do both - cell padding and apply
> filter in a single execution? However, I felt r.resamp.filter does not
> allow specifying the type of filter such as high pass filter coefficients
> in the input.

r.resamp.filter largely avoids the need to add a border; unless the -n
flag is used it ignores any null cells (including cells outside the
current region), effectively changing the filter weights to zero for
null calls (and adjusting the divisor accordingly).

> r.resamp.filter input=band5 output=band5_resamp filter=box radius=1
> How to specify the filter coefficients here?

r.resamp.filter doesn't perform generalised filtering. It is designed
solely for resampling; the output is intended to match the input. All
of the supported filters are anti-aliasing filters.

r.neighbors with the weight= option can perform generalised filtering,
although it's limited to a single filter kernel. Like r.resamp.filter,
it ignores null cells, which eliminates the need to add a border.

> 3.) r.resamp.stats does not allow to specify the filter size (3 or 5)?

Like r.resamp.filter, r.resamp.stats is designed for resampling
(specifically, downsampling). But instead of interpolating between
nearby values, it calculates a statistical aggregate (mean, median,
etc) over the input cells corresponding to each output cell.

> I feel that in any case the number of rows should increase by 2 (1 top and
> 1 bottom) and number of columns should increase by 2 (1 left and 1 right)
> during cell padding.

Adding a border is a necessary workaround for the way that r.mfilter
handles null cells. Ignoring them (as most other such modules do) is a
better solution, as it effectively re-distributes the weight of the
null cell over the non-null cells according to their own weights,
rather than assigning it all to an arbitrarily-chosen cell (which is
the effect of adding a border by extrusion or reflection).

> 4.) Which one of the above GRASS commands would be best suited to perform
> window operation (high pass filtering) along with cell padding?

r.neighbors method=sum.with the weights= option.

Glynn Clements <glynn at gclements.plus.com>

More information about the grass-user mailing list