-
Notifications
You must be signed in to change notification settings - Fork 0
/
reslice.py
117 lines (92 loc) · 3.01 KB
/
reslice.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
"""
==========================================================
Load CT slices and plot axial, sagittal and coronal images
==========================================================
This example illustrates loading multiple files, sorting them by slice
location, building a 3D image and reslicing it in different planes.
.. usage:
reslice.py <glob>
where <glob> refers to a set of DICOM image files.
Example: python reslice.py "*.dcm". The quotes are needed to protect
the glob from your system and leave it for the script.
.. note:
Uses numpy and matplotlib.
Tested using series 2 from here
http://www.pcir.org/researchers/54879843_20060101.html
"""
import pydicom
import numpy as np
import matplotlib.pyplot as plt
import sys
import glob
import os
#获取根目录文件
def get_root_file(root_file):
list_file = os.listdir(root_file)
list_file.sort(key=lambda x: str(x.split('.')[0]))
return list_file
root_dicom="E:\\bigorgs\\Datasets\\1\\"
list_dicom = get_root_file(root_dicom)
num_dicom = len(list_dicom)
files = []
for i in range(num_dicom):
file_root = root_dicom + list_dicom[i]
dcm = pydicom.dcmread(file_root,force=True)
dcm.file_meta.TransferSyntaxUID = pydicom.uid.ImplicitVRLittleEndian
# print(dcm)
print("loading: {}".format(file_root))
# files.append(pydicom.dcmread(file_root,force=True))
files.append(dcm)
# load the DICOM files
# files = []
# print('glob: {}'.format(sys.argv[1]))
# for fname in glob.glob(sys.argv[1], recursive=False):
# print("loading: {}".format(fname))
# files.append(pydicom.dcmread(fname))
print("file count: {}".format(len(files)))
# skip files with no SliceLocation (eg scout views)
slices = []
skipcount = 0
for f in files:
if hasattr(f, 'SliceLocation'):
slices.append(f)
else:
skipcount = skipcount + 1
print("skipped, no SliceLocation: {}".format(skipcount))
# ensure they are in the correct order
slices = sorted(slices, key=lambda s: s.SliceLocation)
# pixel aspects, assuming all slices are the same
ps = slices[0].PixelSpacing
ss = slices[0].SliceThickness
ax_aspect = ps[1]/ps[0]
sag_aspect = ps[1]/ss
cor_aspect = ss/ps[0]
# print(slices[0].pixel_array.shape)
# create 3D array
img_shape = list(slices[0].pixel_array.shape)
img_shape.append(len(slices))
img3d = np.zeros(img_shape)
# fill 3D array with the images from the files
for i, s in enumerate(slices):
img2d = s.pixel_array
img3d[:, :, i] = img2d
# plot 3 orthogonal slices
a1 = plt.subplot(1, 3, 1)
ax=img3d[:, :, img_shape[2]//2]
print("ax:{}".format(ax))
plt.imshow(img3d[:, :, img_shape[2]//2])
a1.set_aspect(ax_aspect)
# a1.set_aspect('auto')
a3 = plt.subplot(1, 3, 2)
cor=img3d[img_shape[0]//2, :, :]
print("cor:{}".format(cor))
plt.imshow(img3d[img_shape[0]//2, :, :])
a3.set_aspect(cor_aspect)
# a3.set_aspect('auto')
a2 = plt.subplot(1, 3, 3)
sag=img3d[:, img_shape[1]//2, :].T
print("sag:{}".format(sag))
plt.imshow(img3d[:, img_shape[1]//2, :].T)
a2.set_aspect(sag_aspect) #坐标轴比例
# a2.set_aspect('auto')
plt.show()