2019-10-24 19:37:21 +00:00
|
|
|
import numpy as np
|
2017-08-16 12:24:29 +00:00
|
|
|
|
2018-06-08 17:27:32 +00:00
|
|
|
# credits to https://stackoverflow.com/users/6076729/manuel-lagunas
|
2017-09-26 19:00:51 +00:00
|
|
|
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
|