Source code for neodroidvision.segmentation.masks.run_length_encoding
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import numpy
__author__ = "Christian Heider Nielsen"
__doc__ = r"""
Created on 20/10/2019
"""
__all__ = ["run_length_to_mask", "mask_to_run_length"]
[docs]def run_length_to_mask(
mask_rle: str = "", shape: tuple = (1400, 2100)
) -> numpy.ndarray:
"""
Decode rle encoded mask.
:param mask_rle: run-length as string formatted (start length)
:param shape: (height, width) of array to return
Returns numpy array, 1 - mask, 0 - background"""
s = mask_rle.split()
starts, lengths = [numpy.asarray(x, dtype=int) for x in (s[0:][::2], s[1:][::2])]
starts -= 1
ends = starts + lengths
img = numpy.zeros(shape[0] * shape[1], dtype=numpy.uint8)
for lo, hi in zip(starts, ends):
img[lo:hi] = 1
return img.reshape(shape, order="F")
[docs]def mask_to_run_length(img: numpy.ndarray) -> str:
"""
Convert mask to rle.
img: numpy array, 1 - mask, 0 - background
Returns run length as string formated"""
pixels = img.T.flatten()
pixels = numpy.concatenate([[0], pixels, [0]])
runs = numpy.where(pixels[1:] != pixels[:-1])[0] + 1
runs[1::2] -= runs[::2]
return " ".join(str(x) for x in runs)