Image pre-processing (non-linear filtering and Gaussian smoothing)
dem_filtering.Rdapplies two filters to an image:
A non-linear filter: closing (
mclosing) with disk kernel, or median (medianblur) with square kernelA 2D Gaussian smoother (The
derichefilter is applied on both dimensions). Value-dependent smoothing is possible
Usage
dem_filtering(
dem,
nl_filter = "Closing",
nl_size = 5,
sigma = 0.3,
padding = TRUE,
sigmap = NULL
)Arguments
- dem
cimg object (e.g. obtained with
as.cimg) or SpatRaster object (e.g. obtained withrast)- nl_filter
string. type of non-linear filter to apply: "None", "Closing" or "Median"
- nl_size
numeric. kernel width in pixels for non-linear filtering
- sigma
numeric or matrix. If a single number is provided,
sigmais the standard deviation of the Gaussian filter, 0 corresponds to no smoothing. Unit is pixel in casedemis a cimg object, SpatRaster units otherwise. In case of a matrix, the first column corresponds to the standard deviation of the filter, and the second to thresholds for image values (e.g. a filter of standard deviation specified in lineiis applied to pixels in image which values are between thresholds indicated in linesiandi+1). Threshold values should be ordered in increasing order.- padding
boolean. Whether image should be padded by duplicating edge values before filtering to avoid border effects
- sigmap
deprecated (numeric or matrix). (old name for
sigmaparameter, retained for backward compatibility, overwritessigmaif provided, unit is pixel whatever the class ofdem)
Value
A list of two cimg objects or a SpatRaster object with image after non-linear filter and image after both filters
See also
maxima_detection, filters of imager package:
mclosing, medianblur,
deriche
Examples
data(chm_chablais3)
chm_chablais3 <- terra::rast(chm_chablais3)
# filtering with median and Gaussian smoothing
im <- dem_filtering(chm_chablais3, nl_filter = "Median", nl_size = 3, sigma = 0.8)
# filtering with median filter and value-dependent Gaussian smoothing
# (less smoothing for values between 0 and 15)
im2 <- dem_filtering(chm_chablais3,
nl_filter = "Median", nl_size = 3,
sigma = cbind(c(0.2, 0.8), c(0, 15))
)
# plot original image
terra::plot(chm_chablais3, main = "Initial image")
# plot image after median filter
terra::plot(im$non_linear_image, main = "Median filter")
# plot image after median and Gaussian filters
terra::plot(im$smoothed_image, main = "Smoothed image")
# plot image after median and value-dependent Gaussian filters
terra::plot(im2$smoothed_image, main = "Value-dependent smoothing")