Skip to contents

Computes mean spectral values for a set of segment IDs by iterating over segmentation tiles stored on disk and streaming values from the original image raster. This function avoids virtual raster (VRT) reads and is designed for memory-safe processing of large images.

Usage

segment_means_from_tiles(img, seg_tile_files, ids, block_nrows = 512)

Arguments

img

A `SpatRaster` containing the original multi-band image data.

seg_tile_files

Character vector of file paths to segmentation tiles (e.g., produced by [make_tiles_disk()]).

ids

Integer vector of segment IDs for which means should be computed.

block_nrows

Integer. Number of raster rows to process per read block when streaming values from disk.

Value

A numeric matrix with `length(ids)` rows and `nlyr(img)` columns. Rows correspond to segment IDs (in the order of `ids`), and columns correspond to image bands. Segments with no contributing pixels return `NA` values.

Details

For each segmentation tile, the corresponding spatial subset of `img` is cropped and read in row blocks. If grid geometry differs between the image and a tile, the image subset is resampled to the tile grid before streaming.

Accumulation is performed as sums and pixel counts per segment, followed by normalization to means. Segment labels with `NA` values are ignored.

This function is typically used after tiled segmentation and ID reconciliation to compute region statistics without loading full rasters into memory.

See also