Generate an animated GIF illustrating how SNIC segmentation evolves as seeds are progressively added. This function runs a sequence of SNIC segmentations using incremental subsets of the provided seeds and compiles the results into an animation.
Arguments
- x
A
SpatRasterrepresenting the image to segment. Dimensions and coordinate reference are inferred automatically.- seeds
A two-column object specifying seed coordinates. If
xhas a CRS, use columnslatandlon(inEPSG:4326); otherwise use pixel indices(r, c). Typically created withsnic_gridor interactively withsnic_grid_manual.- file_path
Path where the resulting GIF is saved. The file must not already exist and the parent directory must be writable.
- max_frames
Maximum number of frames to render. If there are more seeds than
max_frames, only the firstmax_framesseeds are used.- delay
Per-frame delay in centiseconds (1/100 s). Passed to
magick::image_animate(). Default is 10 (0.1 s per frame).- progress
Logical scalar; if
TRUE, show the textual progress bar while generating frames.- ...
Additional arguments forwarded to
snic_plotwhen drawing each frame (e.g., RGB band indices or palette options).- snic_args
Named list of extra arguments passed to
snicon every iteration (e.g.,compactness). Argumentsxandseedsare reserved and cannot be overridden.- device_args
Named list of arguments passed to
grDevices::png()when rendering frames. Defaults tolist(res = 96, bg = "white"). Values such aswidth,height, andfilenameare managed automatically.
Details
For each iteration, the function adds one seed to the current set and
re-runs snic. The segmentation and seed locations are drawn
using snic_plot, saved as PNGs, and then combined into an
animated GIF using the magick package. This is intended for
exploratory and didactic use to illustrate the influence of seed placement
and parameters such as compactness.
Examples
if (requireNamespace("terra", quietly = TRUE) &&
requireNamespace("magick", quietly = TRUE)) {
tif_dir <- system.file("demo-geotiff", package = "snic", mustWork = TRUE)
files <- file.path(
tif_dir,
c(
"S2_20LMR_B02_20220630.tif",
"S2_20LMR_B04_20220630.tif",
"S2_20LMR_B08_20220630.tif",
"S2_20LMR_B12_20220630.tif"
)
)
s2 <- terra::aggregate(terra::rast(files), fact = 8)
set.seed(42)
seeds <- snic_grid(s2, type = "random", spacing = 10L, padding = 0L)
gif_file <- snic_animation(
s2,
seeds = seeds,
file_path = tempfile("snic-demo", fileext = ".gif"),
max_frames = 20L,
snic_args = list(compactness = 0.1),
r = 4, g = 3, b = 1,
device_args = list(height = 192, width = 256)
)
gif_file
}
#> Saved animation to /tmp/RtmpdLIM2b/snic-demo1e8c3a053f2e.gif (2.0 s, 10.0 fps)
#> [1] "/tmp/RtmpdLIM2b/snic-demo1e8c3a053f2e.gif"