-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_annotation_matrix_files.py
41 lines (36 loc) · 1.17 KB
/
create_annotation_matrix_files.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
import glob
import os
import sys
import natsort
import numpy as np
from scipy.io import savemat
from scipy.ndimage import label, zoom
from skimage.io import imread
def create_dir_if_not_exists(path):
if not os.path.exists(path):
os.makedirs(path)
else:
print("Path {} already exists, might be overwriting data".format(path))
input_path = os.path.abspath(sys.argv[1])
output_path = os.path.abspath(sys.argv[2])
create_dir_if_not_exists(output_path)
print(input_path, output_path)
downsample_factor = 3
factor = 2 ** (downsample_factor - 1)
image_paths = natsort.natsorted(
glob.glob(os.path.join(input_path, "*.png")))
for path in image_paths:
img = imread(path)
ann_type = zoom(img, factor, order=0)
binary_image = np.zeros_like(ann_type)
binary_image[ann_type != 0] = 1
ann_inst, num_cells = label(binary_image)
assert np.unique(
ann_inst).tolist() == list(range(0, np.amax(ann_inst) + 1))
mdict = {"inst_map": ann_inst, "type_map": ann_type}
output_mat_path = os.path.join(
output_path, os.path.basename(path).split(".")[0] + ".mat")
print(path, output_mat_path)
savemat(
output_mat_path,
mdict)