-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_masks.py
38 lines (28 loc) · 1.09 KB
/
generate_masks.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
import skimage.draw
import numpy as np
import tifffile
import scipy.misc
import os
from os.path import join
import scipy.ndimage
import imageio
root_data_path = 'data'
for subfolder in os.listdir(root_data_path):
data_path = join(root_data_path, subfolder)
for dirname in os.listdir(data_path):
print (dirname)
dirpath = join(data_path, dirname)
data = np.load(join(dirpath, 'metadata.npz'))
bb = data['bb']
shape = data['shape']
filepath = join(join(dirpath, 'images'), os.listdir(join(dirpath, 'images'))[0])
im_np = tifffile.imread(filepath)
borders = shape
mask_np = np.zeros(im_np.shape[:2], dtype=bool)
x = (borders[:,0] - bb[0,0]) / (bb[1,0] - bb[0,0])
y = (borders[:,1] - bb[0,1]) / (bb[1,1] - bb[0,1])
x = np.round(x * mask_np.shape[1]).astype(int)
y = np.round(y * mask_np.shape[0]).astype(int)
rr, cc = skimage.draw.polygon(y, x)
mask_np[rr, cc] = True
imageio.imsave(join(dirpath, 'mask.png'), np.flipud(mask_np).astype('uint8') * 255)