Raster converters#

Detection models produce segmentation masks; machinery consumes vector geometries. The converters in shapelib.tools.converters translate between the two, in both directions, for the three geometry types.

The high-level way: from_mask / to_mask#

Each shape class exposes the conversion as methods:

import rasterio
from shapelib import Lines, Polygons, Points

orto = rasterio.open("orthomosaic.tif")

# mask (np.ndarray) -> geometries georeferenced by the raster
lines = Lines.from_mask(mask, dataset_reader=orto)
fields = Polygons.from_mask(mask, dataset_reader=orto)
plants = Points.from_mask(mask, dataset_reader=orto)

# geometries -> mask aligned with the raster
mask = lines.to_mask(dataset_reader=orto)

The functions behind them#

The same conversions are available as plain functions when you need more control over the parameters:

from shapelib.tools.converters.lines_raster_converter import (
    mask_to_lines, lines_to_mask,
)
from shapelib.tools.converters.polygon_raster_converter import (
    mask_to_polygons, polygons_to_mask,
)
from shapelib.tools.converters.points_raster_converter import (
    mask_to_points, points_to_mask,
)

mask_to_lines skeletonizes the mask and traces the center lines — this is the step that turns a planting-row segmentation into line geometries. Its tracing core is implemented in Cython (find_lines_lang) for speed.

See also

Full signatures in the API reference.