Skip to content

Instantly share code, notes, and snippets.

@gabrielgarza
Created January 6, 2019 23:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabrielgarza/7b7dfd906d424a1714e5cc2e4c24c059 to your computer and use it in GitHub Desktop.
Save gabrielgarza/7b7dfd906d424a1714e5cc2e4c24c059 to your computer and use it in GitHub Desktop.
def rle_decode(self, mask_rle, shape=(768, 768)):
'''
mask_rle: run-length as string formated (start length)
shape: (height,width) of array to return
Returns numpy array, 1 - mask, 0 - background
'''
if not isinstance(mask_rle, str):
img = np.zeros(shape[0]*shape[1], dtype=np.uint8)
return img.reshape(shape).T
s = mask_rle.split()
starts, lengths = [np.asarray(x, dtype=int) for x in (s[0:][::2], s[1:][::2])]
starts -= 1
ends = starts + lengths
img = np.zeros(shape[0]*shape[1], dtype=np.uint8)
for lo, hi in zip(starts, ends):
img[lo:hi] = 1
return img.reshape(shape).T
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment