gdal-raster-neighbors - Man Page

Name

gdal-raster-neighbors — Compute the value of each pixel from its neighbors (focal statistics)

Added in version 3.12.

Synopsis

Usage: gdal raster neighbors [OPTIONS] <INPUT> <OUTPUT>

Compute the value of each pixel from its neighbors (focal statistics)

Positional arguments:
  -i, --input <INPUT>                                      Input raster datasets [required]
  -o, --output <OUTPUT>                                    Output raster dataset [required]

Common Options:
  -h, --help                                               Display help message and exit
  --json-usage                                             Display usage as JSON document and exit
  --config <KEY>=<VALUE>                                   Configuration option [may be repeated]
  -q, --quiet                                              Quiet mode (no progress bar)

Options:
  -f, --of, --format, --output-format <OUTPUT-FORMAT>      Output format ("GDALG" allowed)
  --co, --creation-option <KEY>=<VALUE>                    Creation option [may be repeated]
  --overwrite                                              Whether overwriting existing output is allowed
                                                           Mutually exclusive with --append
  --append                                                 Append as a subdataset to existing output
                                                           Mutually exclusive with --overwrite
  -b, --band <BAND>                                        Input band (1-based index)
  --method <METHOD>                                        Method to combine weighed source pixels. METHOD=mean|sum|min|max|stddev|median|mode [may be repeated]
  --size <SIZE>                                            Neighborhood size
  --kernel <KERNEL>                                        Convolution kernel(s) to apply [1.. values] [required]
  --ot, --datatype, --output-data-type <OUTPUT-DATA-TYPE>  Output data type. OUTPUT-DATA-TYPE=Byte|Int8|UInt16|Int16|UInt32|Int32|UInt64|Int64|CInt16|CInt32|Float16|Float32|Float64|CFloat32|CFloat64 (default: Float64)
  --nodata <NODATA>                                        Assign a specified nodata value to output bands ('none', numeric value, 'nan', 'inf', '-inf')

Advanced Options:
  --if, --input-format <INPUT-FORMAT>                      Input formats [may be repeated]
  --oo, --open-option <KEY>=<VALUE>                        Open options [may be repeated]

Description

gdal raster neighbors applies a kernel (convolution matrix) and a method to compute the target pixel value from a neighbourhood of the source pixel value.

At the top edge, the values of the first row are replicated to virtually extend the source window by a number of rows equal to the radius of the kernel. And similarly for the bottom, left and right edges. This strategy may potentially lead to unexpected results depending on the applied kernel.

For a given target cell, if the corresponding source value (the one at the center of the kernel) is nodata, the resulting pixel is nodata. For other source values in the neighborhood defined by the kernel, source nodata values are ignored.

This algorithm can be part of a gdal pipeline or gdal raster pipeline.

Options

The following options are available:

-f,  --of,  --format,  --output-format <OUTPUT-FORMAT>

Which output raster format to use. Allowed values may be given by gdal --formats | grep raster | grep rw | sort

--co <NAME>=<VALUE>

Many formats have one or more optional creation options that can be used to control particulars about the file created. For instance, the GeoTIFF driver supports creation options to control compression, and whether the file should be tiled.

May be repeated.

The creation options available vary by format driver, and some simple formats have no creation options at all. A list of options supported for a format can be listed with the --formats command line option but the documentation for the format is the definitive source of information on driver creation options. See Raster drivers format specific documentation for legal creation options for each format.

--overwrite

Allow program to overwrite existing target file or dataset. Otherwise, by default, gdal errors out if the target file or dataset already exists.

--band <BAND>

Source band number (indexing starts at one). If it is not specified, all input bands are used.

--kernel

Convolution kernel to apply to source pixels in a neighborhood of each pixel.

  • either the name of a well-known kernel, among edge1, edge2, sharpen,  gaussian, or unsharp-masking, corresponding to those kernels (convolution matrix), with the addition of:

    • equal corresponding to a kernel with all coefficients at one kernel size can be any odd number between 3 and 99):

      \begin{align}

         \begin{bmatrix} 1 & 1 & 1\\ 1 & 1 & 1 \\ 1 & 1 & 1 \end{bmatrix} \end{align}

      For method mean, this corresponds to a box blur filter.

    • u corresponding to an horizontal derivative with coefficients:

      \begin{align}

         \begin{bmatrix} 0 & 0 & 0\\ -0.5 & 0 & 0.5 \\ 0 & 0 & 0 \end{bmatrix} \end{align}

    • v corresponding to a vertical derivative with coefficients:

      \begin{align}

         \begin{bmatrix} 0 & -0.5 & 0\\ 0 & 0 & 0 \\ 0 & 0.5 & 0 \end{bmatrix} \end{align}

      edge1, edge2, sharpen, u and v are only supported for a kernel size equal to 3.

      gaussian is supported for a kernel size equal to 3 or 5.

      unsharp-masking is supported for a kernel size equal to 5.

  • or the values of the coefficients of the kernel as a square matrix of width and height N, where N is an odd number, as [[val00, val01, ..., val0N],[val10, val11, ..., val1N],...,[valN0, valN1, ..., valNN]].

    If --method is set to mean, this has the effect of adding the sum of the (contributing, i.e. non nodata) weighted source pixels and dividing it by the sum of the coefficients in the kernel.

If --kernel is specified several times, there will one output band for each combination of kernel and input band.

--size <SIZE>

Size of the kernel. Odd number between 3 and 99.

NOTE:

Computation time is proportional to the square of the kernel size.

For kernels edge1, edge2, sharpen, u, v, gaussian, equal, defaults to 3.

For kernel unsharp-masking, defaults to 5.

For kernels specified through their coefficient values, it is deduced from the shape of the matrix.

--method sum|mean|min|max|stddev|median|mode

Function to apply to the weighted source pixels in the neighborhood defined by the kernel. Defaults to mean, except when --kernel is set to u, v, edge1, edge2, or a user defined kernel whose sum of coefficients is zero, in which case sum is used.

  • sum: computes the sum of the value of contributing source pixels multiplied by the corresponding weight of the kernel. This corresponds to a kernel with un-normalized sum of coefficients.
  • mean: computes the average of the value of contributing source pixels multiplied by the corresponding weight of the kernel. This has the effect of normalizing kernel coefficients so their sum is one.
  • min: computes the minimum of the value of contributing source pixels multiplied by the corresponding weight of the kernel
  • max: computes the maximum of the value of contributing source pixels multiplied by the corresponding weight of the kernel
  • stddev: computes the standard deviation of the value of contributing source pixels multiplied by the corresponding weight of the kernel
  • median: computes the median of the value of contributing source pixels multiplied by the corresponding weight of the kernel
  • mode (majority): computes the most frequent of the value of contributing source pixels multiplied by the corresponding weight of the kernel
--nodata

Set the NoData value for the output dataset. May be set to none to leave the NoData value undefined. If --nodata is not specified, gdal raster neighbors will use a NoData value from the first source dataset to have one.

Gdalg Output (on-the-Fly / Streamed Dataset)

This program supports serializing the command line as a JSON file using the GDALG output format. The resulting file can then be opened as a raster dataset using the GDALG: GDAL Streamed Algorithm driver, and apply the specified pipeline in a on-the-fly / streamed way.

Examples

Example 1: Compute the horizontal and vertical derivative of a single-band raster

gdal raster neighbors --kernel u --kernel v in.tif uv.tif

Example 2: Compute the average value around each pixel in a 3x3 neighborhood

gdal raster neighbors --kernel equal --method mean in.tif mean.tif

Example 3: Compute the maximum value around each pixel in a 5x5 neighborhood

gdal raster neighbors --kernel equal --size 5 --method max in.tif max.tif

Example 4: Compute a sharpen filter of a single-band raster, by manually specifying the kernel coefficients.

gdal raster neighbors "--kernel=[[0,-1,0],[-1,5,-1],[0,-1,0]]" in.tif sharpen.tif

Author

Even Rouault <even.rouault@spatialys.com>

Info

Nov 07, 2025 GDAL