Created
January 6, 2019 23:58
-
-
Save gabrielgarza/7b7dfd906d424a1714e5cc2e4c24c059 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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