gdal raster neighbors
Added in version 3.12.
Compute the value of each pixel from its neighbors (focal statistics).
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] [not available in pipelines]
-o, --output <OUTPUT> Output raster dataset [required] [not available in pipelines]
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 or warning message) [not available in pipelines]
Options:
-f, --of, --format, --output-format <OUTPUT-FORMAT> Output format ("GDALG" allowed) [not available in pipelines]
--co, --creation-option <KEY>=<VALUE> Creation option [may be repeated] [not available in pipelines]
--overwrite Whether overwriting existing output dataset is allowed [not available in pipelines]
Mutually exclusive with --append
--append Append as a subdataset to existing output [not available in pipelines]
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=UInt8|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] [not available in pipelines]
--oo, --open-option <KEY>=<VALUE> Open options [may be repeated] [not available in pipelines]
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.
Raster dataset before (left) and after (right) summation with a 3x3 equal-weight kernel. NoData values are considered zero for the purpose of the summation. Edge cells are replicated where the kernel window extends beyond the edge of the dataset.
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.
Program-Specific Options
- --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, orunsharp-masking, corresponding to those kernels (convolution matrix), with the addition of:equalcorresponding to a kernel with all coefficients at one kernel size can be any odd number between 3 and 99):\[\begin{split}\begin{align} \begin{bmatrix} 1 & 1 & 1\\ 1 & 1 & 1 \\ 1 & 1 & 1 \end{bmatrix} \end{align}\end{split}\]For method
mean, this corresponds to a box blur filter.ucorresponding to an horizontal derivative with coefficients:\[\begin{split}\begin{align} \begin{bmatrix} 0 & 0 & 0\\ -0.5 & 0 & 0.5 \\ 0 & 0 & 0 \end{bmatrix} \end{align}\end{split}\]vcorresponding to a vertical derivative with coefficients:\[\begin{split}\begin{align} \begin{bmatrix} 0 & -0.5 & 0\\ 0 & 0 & 0 \\ 0 & 0.5 & 0 \end{bmatrix} \end{align}\end{split}\]
edge1,edge2,sharpen,uandvare only supported for a kernel size equal to 3.gaussianis supported for a kernel size equal to 3 or 5.unsharp-maskingis 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
--methodis set tomean, 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
--kernelis specified several times, there will one output band for each combination of kernel and input band.
- --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--kernelis set tou,v,edge1,edge2, or a user defined kernel whose sum of coefficients is zero, in which casesumis 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 kernelmax: computes the maximum of the value of contributing source pixels multiplied by the corresponding weight of the kernelstddev: computes the standard deviation of the value of contributing source pixels multiplied by the corresponding weight of the kernelmedian: computes the median of the value of contributing source pixels multiplied by the corresponding weight of the kernelmode(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
noneto leave the NoData value undefined. If--nodatais not specified, gdal raster neighbors will use a NoData value from the first source dataset to have one.
- --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.
Standard Options
Details
- --append
Append input raster as a new subdataset to an existing output file. Only works with drivers that support adding subdatasets such as GTiff -- GeoTIFF File Format and GPKG -- GeoPackage raster This also creates the output dataset if it does not exist yet.
- --co, --creation-option <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.
- --if, --input-format <format>
Format/driver name to be attempted to open the input file(s). It is generally not necessary to specify it, but it can be used to skip automatic driver detection, when it fails to select the appropriate driver. This option can be repeated several times to specify several candidate drivers. Note that it does not force those drivers to open the dataset. In particular, some drivers have requirements on file extensions.
May be repeated.
- --oo, --open-option <NAME>=<VALUE>
Dataset open option (format specific).
May be repeated.
- -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
- --ot, --datatype, --output-data-type <OUTPUT-DATA-TYPE>
Output data type among
Byte,Int8,UInt16,Int16,UInt32,Int32,UInt64,Int64,CInt16,CInt32,Float32,Float64,CFloat32,CFloat64.
- --overwrite
Allow program to overwrite existing target file or dataset. Otherwise, by default, gdal errors out if the target file or dataset already exists.
Return status code
The program returns status code 0 in case of success, and non-zero in case of error (non-blocking errors emitted as warnings are considered as a successful execution).
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