-
Notifications
You must be signed in to change notification settings - Fork 1
/
mask_functions.py
42 lines (36 loc) · 1.19 KB
/
mask_functions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import numpy as np
def mask2rle(img, width, height):
rle = []
lastColor = 0;
currentPixel = 0;
runStart = -1;
runLength = 0;
for x in range(width):
for y in range(height):
currentColor = img[x][y]
if currentColor != lastColor:
if currentColor == 255:
runStart = currentPixel;
runLength = 1;
else:
rle.append(str(runStart));
rle.append(str(runLength));
runStart = -1;
runLength = 0;
currentPixel = 0;
elif runStart > -1:
runLength += 1
lastColor = currentColor;
currentPixel+=1;
return " ".join(rle)
def rle2mask(rle, width, height):
mask= np.zeros(width* height)
array = np.asarray([int(x) for x in rle.split()])
starts = array[0::2]
lengths = array[1::2]
current_position = 0
for index, start in enumerate(starts):
current_position += start
mask[current_position:current_position+lengths[index]] = 255
current_position += lengths[index]
return mask.reshape(width, height)