Skip to contents

Runs deep learning segmentation models (e.g., YOLO-seg, Grounded-SAM, U2-Net, StarDist) on a batch of images defined by a directory path, a filename pattern, a vector of file paths, or a list of Image objects. Supports sequential or parallel processing (via mirai), optional automated morphometric feature extraction (via dl_extract_phenotypes()), and returns a consolidated predictions dataset along with per-image summaries.

Usage

image_segment_dl_batch(
  data,
  pattern = NULL,
  path = NULL,
  parallel = FALSE,
  sahi = FALSE,
  slice_height = 640,
  slice_width = 640,
  overlap_height_ratio = 0.2,
  overlap_width_ratio = 0.2,
  match_metric = "IOS",
  match_threshold = 0.5,
  postprocess_type = "GREEDY_NMM",
  measure = TRUE,
  pixel_size = NULL,
  show_point = FALSE,
  keep_images = FALSE,
  save_image = FALSE,
  dir_processed = "./proc",
  prefix = "proc_",
  verbose = TRUE,
  imgsz = NULL,
  as_job = FALSE,
  ...
)

image_segment_batch(
  data,
  pattern = NULL,
  path = NULL,
  parallel = FALSE,
  sahi = FALSE,
  slice_height = 640,
  slice_width = 640,
  overlap_height_ratio = 0.2,
  overlap_width_ratio = 0.2,
  match_metric = "IOS",
  match_threshold = 0.5,
  postprocess_type = "GREEDY_NMM",
  measure = TRUE,
  pixel_size = NULL,
  show_point = FALSE,
  keep_images = FALSE,
  save_image = FALSE,
  dir_processed = "./proc",
  prefix = "proc_",
  verbose = TRUE,
  imgsz = NULL,
  as_job = FALSE,
  ...
)

yolo_segment_batch(
  data,
  pattern = NULL,
  path = NULL,
  parallel = FALSE,
  sahi = FALSE,
  slice_height = 640,
  slice_width = 640,
  overlap_height_ratio = 0.2,
  overlap_width_ratio = 0.2,
  match_metric = "IOS",
  match_threshold = 0.5,
  postprocess_type = "GREEDY_NMM",
  measure = TRUE,
  pixel_size = NULL,
  show_point = FALSE,
  keep_images = FALSE,
  save_image = FALSE,
  dir_processed = "./proc",
  prefix = "proc_",
  verbose = TRUE,
  imgsz = NULL,
  as_job = FALSE,
  ...
)

Arguments

data

Input data. Can be:

  • A character string representing a directory path containing images (e.g., "flax_grains").

  • A character pattern to match image files in path or the current directory (e.g., "-", "^img_").

  • A character vector of image file paths.

  • A list of Image objects or numeric arrays.

pattern

Optional regex pattern to filter image filenames when data is a directory path.

path

Optional directory path to search when data is a pattern. Defaults to ".".

parallel

Logical. If TRUE, runs inference in parallel across images using mirai. Defaults to FALSE.

sahi

Logical. If TRUE, uses SAHI (Slicing Aided Hyper Inference) for tiled segmentation on large or high-resolution images. Defaults to FALSE.

slice_height

Integer. Height of sliding window slices when sahi = TRUE. Defaults to 640.

slice_width

Integer. Width of sliding window slices when sahi = TRUE. Defaults to 640.

overlap_height_ratio

Numeric scalar between 0 and 0.5. Vertical slice overlap fraction when sahi = TRUE. Defaults to 0.20.

overlap_width_ratio

Numeric scalar between 0 and 0.5. Horizontal slice overlap fraction when sahi = TRUE. Defaults to 0.20.

match_metric

Character string. Box merging overlap metric used when sahi = TRUE ("IOS" or "IOU"). Defaults to "IOS".

match_threshold

Numeric scalar. Overlap threshold for merging slice detections when sahi = TRUE. Defaults to 0.50.

postprocess_type

Character string. Post-processing algorithm used when sahi = TRUE ("GREEDY_NMM" or "NMS"). Defaults to "GREEDY_NMM".

measure

Logical. If TRUE, automatically extracts granular morphometric, spectral, and geometric phenotypes for every segmented instance using dl_extract_phenotypes(). Defaults to TRUE.

pixel_size

Optional numeric physical size per pixel (e.g., 0.05 for 0.05 cm/px) passed to dl_extract_phenotypes().

show_point

Logical. Whether to show a point at the centroid of each segmented object. Defaults to FALSE.

keep_images

Logical. Whether to keep the full original images attached as attributes in $results. Defaults to FALSE to prevent excessive RAM consumption during large batches (saving gigabytes of memory).

save_image

Logical. Whether to save segmented or highlight mask images to disk. Defaults to FALSE.

dir_processed

Character string. Directory where segmented images should be saved when save_image = TRUE. Defaults to "./proc".

prefix

Character string. Prefix prepended to saved output filenames. Defaults to "proc_".

verbose

Logical. Whether to show progress bars, timing, and formatted CLI summaries. Defaults to TRUE.

imgsz

Target input image resolution for YOLO inference (e.g., 640, 960, or 1024). If NULL (default), inferred automatically or defaults to 640.

as_job

Logical. If TRUE and running inside RStudio, runs the batch segmentation process in an RStudio Background Job. Defaults to FALSE.

...

Additional arguments passed to image_segment_dl() (e.g., model, type, threshold, box_threshold, text_threshold, iou_threshold, prompt, col_background, col_highlight, alpha, bbox, mask, engine, device_id).

Value

An S3 object of class c("dl_segment_batch", "list") containing:

  • predictions: A consolidated data.frame of all detected bounding boxes or instance centroids across all images.

  • phenotypes: (If measure = TRUE and instance segmentation was run) A consolidated data.frame of all morphological, spectral, and shape traits with column img.

  • summary: A per-image data.frame summarizing total objects detected, total segmented area, and average size.

  • results: A named list of raw individual segmentation outputs from image_segment_dl().

Performance

The model is resolved once and every image reuses the cached ONNX session. With parallel = TRUE each worker process loads its own copy of the model, so:

  • on CPU (engine = "cpu") and threads = 0, each worker gets physical cores / workers ONNX threads to avoid oversubscribing the CPU;

  • on GPU (engine = "gpu") concurrency is capped at getOption("pliman.gpu_workers", 2), because every worker opens its own GPU session (GPU memory grows with the number of workers);

  • temporary workers are started and stopped on every call. Start persistent workers with pliman_daemons() to keep models loaded across calls;

  • pass file paths rather than in-memory images, so only paths are sent to the workers.

Examples

if (FALSE) { # interactive()
library(pliman)

# 1. Batch instance segmentation with YOLO26-seg (Parallel)
res <- image_segment_dl_batch(
  data = "flax_grains",
  model = "yolo26n-seg",
  parallel = TRUE,
  measure = TRUE
)
head(res$summary)
head(res$phenotypes)
}