View source: R/erodil_raster.R
erodil_raster | R Documentation |
Mixing erosion and dilation processes to clean binary images, getting rid of isolated pixels and little holes.
For the single steps, see erode()
.
erodil_raster(raster, ...)
## S3 method for class 'SpatRaster'
erodil_raster(
raster,
width = c(3, 3),
type = "diamond",
erosion = TRUE,
dilation = TRUE,
erosion_first = TRUE,
nt = 1,
...
)
raster |
An object of class SpatRaster containing 1 and 0 (or NAs) values. |
... |
Further arguments passed to |
width |
An integer vector indicating the width and the heigth applied
for the kernel. This is passed to |
type |
A character value indicating the type of shape used for the
kernel (see |
erosion |
A logical value indicating whether the data will be eroded or not. |
dilation |
A logical value indicating whether the data will be dilated or not. |
erosion_first |
A logical value indicating whether erosion should be carried out before dilation or vice versa. |
nt |
Number of times to be processed. |
A SpatRaster object with value 1 for the processed features and 0 for the background. The output is already factorized and includes a color table for the two categories (white and black).
Jan Blöthe and Miguel Alvarez (kamapu@posteo.de).
## Load installed rasterLayer
require(terra)
require(ggplot2)
require(tidyterra)
# Import and prepare data
r <- rast(file.path(path.package("spatialist"), "binras.tif"))
r[is.na(r)] <- 0
r <- as.factor(r)
coltab(r) <- data.frame(value = c(0, 1), col = c("white", "black"))
## Make only erosion or only dilation
r2 <- rast(c(
original = r,
eroded = erodil_raster(r, dilation = FALSE),
dilated = erodil_raster(r, erosion = FALSE)
))
ggplot() + geom_spatraster(data = r2) + facet_wrap(~lyr)
## Erode two times
r2 <- rast(c(
original = r,
dilated = erodil_raster(r, erosion = FALSE),
dilated2 = erodil_raster(r, erosion = FALSE, nt = 2)
))
ggplot() + geom_spatraster(data = r2) + facet_wrap(~lyr)
## By default erosion will be done before dilation
r2 <- rast(c(
original = r,
eroded_first = erodil_raster(r),
dilated_first = erodil_raster(r, erosion_first = FALSE),
both = erodil_raster(erodil_raster(r, erosion_first = FALSE))
))
ggplot() + geom_spatraster(data = r2) + facet_wrap(~lyr)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.