-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_mat2numpy.py
135 lines (96 loc) · 3.84 KB
/
convert_mat2numpy.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load in
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
# Input data files are available in the "../input/" directory.
# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory
import os
for dirname, _, filenames in os.walk(r'D:\项目:ZOC眼科\data_kaggle\2015_BOE_Chiu'):
for filename in filenames:
print(os.path.join(dirname, filename))
# Any results you write to the current directory are saved as output.
from scipy.io import loadmat
import numpy as np
from matplotlib import pyplot as plt
from glob import glob
from os import path
# type: list, glob返回对应规则的文件夹内容
mat_fps = glob(path.join(r'D:\项目:ZOC眼科\data_kaggle\2015_BOE_Chiu', '*.mat'))
print(len(mat_fps))
mat = loadmat(mat_fps[0])
fluid_class = 9
# Here use get_valid_idx to get the valid B-scans index
def get_valid_idx(manualLayer):
idx = []
for i in range(0,61):
temp = manualLayer[:,:,i]
if np.sum(temp) != 0:
idx.append(i)
return idx
# 0~9
def get_valid_img_seg(mat):
manualLayer = np.array(mat['manualLayers1'], dtype=np.uint16)
manualFluid = np.array(mat['manualFluid1'], dtype=np.uint16)
img = np.array(mat['images'], dtype=np.uint8)
valid_idx = get_valid_idx(manualLayer)
manualFluid = manualFluid[:, :, valid_idx]
manualLayer = manualLayer[:, :, valid_idx]
print(manualLayer.shape)
seg = np.zeros((496, 768, 11))
seg[manualFluid > 0] = fluid_class
max_col = -100
min_col = 900
for b_scan_idx in range(0, 11):
for col in range(768):
cur_col = manualLayer[:, col, b_scan_idx]
if np.sum(cur_col) == 0:
continue
max_col = max(max_col, col)
min_col = min(min_col, col)
labels_idx = cur_col.tolist()
# print(f'{b_scan_idx} {labels_idx}')
# labels_idx.append(-1)
# labels_idx.insert(0, 0)
last_st = None
last_ed = None
for label, (st, ed) in enumerate(zip([0]+labels_idx, labels_idx+[-1])):
# print(st, ed)
if st == 0 and ed == 0:
st = last_ed
# 穿越第一层
while(seg[st, col, b_scan_idx] == fluid_class):
st += 1
while(seg[st, col, b_scan_idx] != fluid_class):
seg[st, col, b_scan_idx] = label
st += 1
if st >= 496:
break
continue
if ed == 0:
ed = st + 1
while(seg[ed, col, b_scan_idx] != fluid_class):
ed += 1
if st == 0 and label != 0:
st = ed-1
while(seg[st, col, b_scan_idx] != fluid_class):
st -= 1
st += 1
seg[st:ed, col, b_scan_idx] = label
last_st = st
last_ed = ed
seg[manualFluid > 0] = fluid_class
seg = seg[:, min_col:max_col+1]
img = img[:, min_col:max_col+1]
return img, seg
img, seg = get_valid_img_seg(mat)
print(img.shape)
print(seg.shape)
print(type(img))
print(type(seg))
plt.imshow(seg[:, :, 0], cmap=plt.cm.jet, vmax=9)
plt.show()
plt.imshow(img[:, :, 2])
plt.show()
plt.imshow(seg[:, :, 3], cmap=plt.cm.jet, vmax=9)
plt.show()