projet-long/utils/utils.py
milesial 35f955cbf8 Now using utils.data.Dataset
Former-commit-id: c75d9c075e18add5cd8683faf827937393bf2c94
2019-11-23 14:22:42 +01:00

15 lines
545 B
Python

import numpy as np
# credits to https://stackoverflow.com/users/6076729/manuel-lagunas
def rle_encode(mask_image):
pixels = mask_image.flatten()
# We avoid issues with '1' at the start or end (at the corners of
# the original image) by setting those pixels to '0' explicitly.
# We do not expect these to be non-zero for an accurate mask,
# so this should not harm the score.
pixels[0] = 0
pixels[-1] = 0
runs = np.where(pixels[1:] != pixels[:-1])[0] + 2
runs[1::2] = runs[1::2] - runs[:-1:2]
return runs