Filters#
GeoUtils provides several filters to process raster data. They can be applied to Raster objects.
Available filters#
The following filters are currently available in GeoUtils:
Filter Name |
Description |
Typical Effect |
|---|---|---|
|
Applies a Gaussian (blur) filter with a specified sigma. |
Smooths the image, reduces noise while slightly blurring edges. |
|
Applies a median filter over a sliding window. |
Reduces noise while preserving edges better than Gaussian. |
|
Applies a mean (average) filter with a specified kernel size. |
Smooths the image uniformly, reduces high-frequency noise. |
|
Applies a maximum filter over a sliding window. |
Enhances bright regions, expands high-intensity areas. |
|
Applies a minimum filter over a sliding window. |
Suppresses bright regions, expands dark regions. |
|
Removes pixels that deviate strongly from local neighborhood average (within a radius). |
Removes outliers and anomalous values based on local context. |
|
Allows users to define their own filter function to be applied to a numpy array. |
Parameters#
Parameter |
Definition |
Available for filter |
Type |
Default value |
|---|---|---|---|---|
|
Filtering engine to use, either “scipy” or “numba”. |
|
str |
scipy |
|
The minimum difference abs(array - mean) for a pixel to be considered an outlier |
|
float |
2 |
|
The radius in which the average value is calculated |
|
float |
5 |
|
The sigma of the Gaussian kernel |
|
float |
5 |
|
The size of the window to use (must be odd). |
|
int |
5 |
|
Kwargs from scipy are available |
|
dict |
Note
The median filter can be computationally intensive, especially on large rasters. GeoUtils supports the use of
Numba to accelerate filter computations. To enable Numba, ensure it is installed in your
environment and set the engine parameter to numba when applying the filter
Applying filters#
Filters can be applied to a Raster object using the filter() function.
For example:
import geoutils as gu
filename_rast = gu.examples.get_path("exploradores_aster_dem")
rast = gu.Raster(filename_rast)
# Filter the raster with a median filter of size 5
rast_filtered = rast.filter("median", size=5)
/home/docs/checkouts/readthedocs.org/user_builds/geoutils/conda/latest/lib/python3.14/site-packages/scipy/ndimage/_filters.py:183: RuntimeWarning: All-NaN slice encountered
return footprinted_function(xp.asarray(view), **kwargs)
For example, to apply a median filter:
# Apply a median filter with a kernel size of 3
filtered_raster = gu.filters.median_filter(rast.data, size=3)
Users can also apply custom filters by providing a function that takes a 2D numpy array as input and returns a filtered 2D numpy array.
import numpy as np
# Filter the raster with a hand-made filter
def double_filter(arr: np.ndarray) -> np.ndarray:
return arr * 2
rast_double = rast.filter(double_filter)