-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdataIO.py
More file actions
71 lines (57 loc) · 1.63 KB
/
dataIO.py
File metadata and controls
71 lines (57 loc) · 1.63 KB
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
# coding:utf-8
import os
import numpy as np
import SimpleITK as sitk
def read_mhd_and_raw(path, numpyFlag=True):
"""
This function use sitk
path : Meta data path
ex. /hogehoge.mhd
numpyFlag : Return numpyArray or sitkArray
return : numpyArray(numpyFlag=True)
Note ex.3D :numpyArray axis=[z,y,x], sitkArray axis=(z,y,x)
"""
img = sitk.ReadImage(path)
if not numpyFlag:
return img
nda = sitk.GetArrayFromImage(img) # (img(x,y,z)->numpyArray(z,y,x))
return nda
def write_mhd_and_raw(Data, path):
"""
This function use sitk
Data : sitkArray
path : Meta data path
ex. /hogehoge.mhd
"""
if not isinstance(Data, sitk.SimpleITK.Image):
print('Please check your ''Data'' class')
return False
data_dir, file_name = os.path.split(path)
if not os.path.isdir(data_dir):
os.makedirs(data_dir)
sitk.WriteImage(Data, path, True)
return True
def load_matrix_data(path, dtype):
# load data_list
data_list = []
with open(path) as paths_file:
for line in paths_file:
line = line.split()
if not line: continue
data_list.append(line[:])
data = []
for i in data_list:
print('image from: {}'.format(i[0]))
image = read_mhd_and_raw(i[0]).astype(dtype)
data.append(image)
data = np.asarray(data)
return data
# load list
def load_list(path):
data_list = []
with open(path) as paths_file:
for line in paths_file:
if not line: continue
line = line.replace('\n','')
data_list.append(line[:])
return data_list