-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmhd_file.py
212 lines (192 loc) · 7.09 KB
/
mhd_file.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import cv2
import SimpleITK as sitk
from matplotlib import pyplot
import numpy as np
from vtkmodules.util import numpy_support
import vtk
import io
import PIL
image =sitk.ReadImage('G:/test/Osirix_Data/TOUTATIX/3DPrint_CorCTA_ResultFiles/Diastole65_DownSmp2_SegLabels.mhd')
image_arr = sitk.GetArrayFromImage(image)
image_arr = image_arr.transpose()
ConstOrigin = image.GetOrigin()
ConstPixelSpacing = image.GetSpacing()
ConstPixelDims = np.shape(image_arr)
print(ConstPixelDims)
image_1 = int(ConstPixelDims[0]//2)
image_2 = int(ConstPixelDims[1]//2)
image_3 = int(ConstPixelDims[2]//2)
x = np.arange(0.0, (ConstPixelDims[0] + 1) * ConstPixelSpacing[0],ConstPixelSpacing[0])
y = np.arange(0.0, (ConstPixelDims[1] + 1) * ConstPixelSpacing[1],ConstPixelSpacing[1])
z = np.arange(0.0, (ConstPixelDims[2] + 1) * ConstPixelSpacing[2],ConstPixelSpacing[2])
fig1 = pyplot.figure(dpi=300)
pyplot.axes().set_aspect('equal','datalim')
pyplot.set_cmap(pyplot.gray())
pyplot.pcolormesh(x, y, np.flipud(image_arr[:, :, image_3]).transpose()) # 第三个维度表示现在展示的是第几层
pyplot.axis('off')
buffer_ = io.BytesIO()
pyplot.savefig(buffer_,format='png')
buffer_.seek(0)
img1 = PIL.Image.open(buffer_)
img_arr1 = np.asarray(img1)
buffer_.close()
fig2 = pyplot.figure(dpi=300)
pyplot.axes().set_aspect('equal','datalim')
pyplot.set_cmap(pyplot.gray())
pyplot.pcolormesh(y, z, np.fliplr(np.rot90((image_arr[image_1, :, :]),3)))
pyplot.axis('off')
buffer_ = io.BytesIO()
pyplot.savefig(buffer_,format='png')
buffer_.seek(0)
img2 = PIL.Image.open(buffer_)
img_arr2 = np.asarray(img2)
buffer_.close()
fig3 = pyplot.figure(dpi=300)
pyplot.axes().set_aspect('equal','datalim')
pyplot.set_cmap(pyplot.gray())
pyplot.pcolormesh(x, z, np.fliplr(np.rot90((image_arr[:, image_2, :]),3)))
pyplot.axis('off')
buffer_ = io.BytesIO()
pyplot.savefig(buffer_,format='png')
buffer_.seek(0)
img3 = PIL.Image.open(buffer_)
img_arr3 = np.asarray(img3)
buffer_.close()
pyplot.figure(figsize=(3,1),dpi=300)
pyplot.subplot(131)
pyplot.imshow(img_arr1)
pyplot.title('AxialSlice',fontsize=4,y=0.9)
pyplot.axis('off')
pyplot.xticks([])
pyplot.yticks([])
pyplot.subplot(132)
pyplot.imshow(img_arr2)
pyplot.title('CoronalSlice',fontsize=4,y=0.9)
pyplot.axis('off')
pyplot.xticks([])
pyplot.yticks([])
pyplot.subplot(133)
pyplot.imshow(img_arr3)
pyplot.title('SagitalSlice',fontsize=4,y=0.9)
pyplot.axis('off')
pyplot.xticks([])
pyplot.yticks([])
pyplot.tight_layout(pad=0.5,w_pad=2)
pyplot.subplots_adjust(wspace=0,hspace=0)
# pyplot.savefig('E:\\Dicom\\test\\images\\'+'image.jpg')
pyplot.show()
'''
pyplot.figure(dpi=300)
pyplot.subplot(1,3,1)
pyplot.axes().set_aspect('equal', 'datalim')
pyplot.set_cmap(pyplot.gray())
pyplot.pcolormesh(x, y,np.flipud(image_arr[:, :, image_3]).transpose())
# pyplot.axis('off')
# pyplot.savefig('G:/test/ART_VC_uCT/'+'AxialSlice'+'.jpg')
# pyplot.show()
pyplot.subplot(1,3,2)
# pyplot.figure(dpi=300)
pyplot.axes().set_aspect('equal','datalim')
pyplot.set_cmap(pyplot.gray())
pyplot.pcolormesh(y, z, np.fliplr(np.rot90((image_arr[image_1, :, :]),3)))
# pyplot.axis('off')
# pyplot.savefig('G:/test/ART_VC_uCT/'+'CoronalSlice'+'.jpg')
# pyplot.show()
pyplot.subplot(1,3,3)
# pyplot.figure(dpi=300)
pyplot.axes().set_aspect('equal','datalim')
pyplot.set_cmap(pyplot.gray())
pyplot.pcolormesh(x, z, np.fliplr(np.rot90((image_arr[:, image_2, :]),3)))
# pyplot.axis('off')
# pyplot.savefig('G:/test/ART_VC_uCT/'+'SagitalSlice'+'.jpg')
pyplot.show()
'''
Array_vtk = numpy_support.numpy_to_vtk(image_arr.ravel('F'), deep=True, array_type=vtk.VTK_FLOAT)
imagedata = vtk.vtkImageData()
imagedata.SetOrigin(ConstOrigin)
imagedata.SetSpacing(ConstPixelSpacing)
imagedata.SetDimensions(ConstPixelDims)
imagedata.GetPointData().SetScalars(Array_vtk)
origin = np.array(ConstOrigin)
ConstPixelSpacing = np.array(ConstPixelSpacing)
ConstPixelDims = np.array(ConstPixelDims)
center = origin + (ConstPixelSpacing * ConstPixelDims / 2)
DirectionCosines_x = (0, 0, 1, 0, 1, 0, -1, 0, 0)
DirectionCosines_y = (1, 0, 0, 0, 0, -1, 0, 1, 0)
DirectionCosines_z = (1, 0, 0, 0, 1, 0, 0, 0, 1)
def mip_x():
ImageSlab = vtk.vtkImageSlabReslice()
ImageSlab.SetInputData(imagedata)
ImageSlab.SetResliceAxesOrigin(center)
ImageSlab.SetResliceAxesDirectionCosines(DirectionCosines_x)
ImageSlab.SetSlabThickness(ConstPixelSpacing[0]*ConstPixelDims[0])
ImageSlab.SetBlendModeToMax()
ImageSlab.SetSlabResolution(ConstPixelSpacing[0])
ImageSlab.Update()
image = ImageSlab.GetOutput()
m = image.GetDimensions()
vtk_data = image.GetPointData().GetScalars()
arr = numpy_support.vtk_to_numpy(vtk_data).reshape(m[1], m[0])
arr = (arr - np.min(arr)) / ((np.max(arr) - np.min(arr)) / 255)
width = ConstPixelDims[1]
height = int(ConstPixelDims[2] * (ConstPixelSpacing[2] / ConstPixelSpacing[1]))
dim = (width, height)
resized = cv2.resize(np.rot90(arr, 1), dim, interpolation=cv2.INTER_AREA)
# cv2.imwrite( path + name +'.jpg', resized)
return resized
def mip_y():
ImageSlab = vtk.vtkImageSlabReslice()
ImageSlab.SetInputData(imagedata)
ImageSlab.SetResliceAxesOrigin(center)
ImageSlab.SetResliceAxesDirectionCosines(DirectionCosines_y)
ImageSlab.SetSlabThickness(ConstPixelSpacing[1]*ConstPixelDims[1])
ImageSlab.SetBlendModeToMax()
ImageSlab.SetSlabResolution(ConstPixelSpacing[1])
ImageSlab.Update()
image = ImageSlab.GetOutput()
m = image.GetDimensions()
vtk_data = image.GetPointData().GetScalars()
arr = numpy_support.vtk_to_numpy(vtk_data).reshape(m[1], m[0])
arr = (arr - np.min(arr)) / ((np.max(arr) - np.min(arr)) / 255)
width = int(ConstPixelDims[2] * (ConstPixelSpacing[2] / ConstPixelSpacing[0]))
height = ConstPixelDims[0]
dim = (width, height)
resized = cv2.resize(np.rot90(arr, -1), dim, interpolation=cv2.INTER_AREA)
# cv2.imwrite( path + name +'.jpg', resized)
return resized
def mip_z():
ImageSlab = vtk.vtkImageSlabReslice()
ImageSlab.SetInputData(imagedata)
ImageSlab.SetResliceAxesOrigin(center)
ImageSlab.SetResliceAxesDirectionCosines(DirectionCosines_z)
ImageSlab.SetSlabThickness(ConstPixelSpacing[2] * ConstPixelDims[2])
ImageSlab.SetBlendModeToMax()
ImageSlab.SetSlabResolution(ConstPixelSpacing[2])
ImageSlab.Update()
image = ImageSlab.GetOutput()
m = image.GetDimensions()
vtk_data = image.GetPointData().GetScalars()
arr = numpy_support.vtk_to_numpy(vtk_data).reshape(m[1], m[0])
arr = (arr - np.min(arr)) / ((np.max(arr) - np.min(arr)) / 255)
# cv2.imwrite( path + name +'.jpg', arr)
return arr
pyplot.figure(figsize=(3,1),dpi=300)
pyplot.subplot(131)
pyplot.imshow(np.rot90(mip_z(),2),cmap='gray')
pyplot.title('AxialSlice_MIP',fontsize=4,y=1.1)
pyplot.xticks([])
pyplot.yticks([])
pyplot.subplot(132)
pyplot.imshow(mip_x(),cmap='gray')
pyplot.title('CoronalSlice_MIP',fontsize=4,y=1.1)
pyplot.xticks([])
pyplot.yticks([])
pyplot.subplot(133)
pyplot.imshow(np.rot90(mip_y(),1),cmap='gray')
pyplot.title('SagitalSlice_MIP',fontsize=4,y=1.1)
pyplot.xticks([])
pyplot.yticks([])
pyplot.tight_layout(pad=1.3,w_pad=2)
pyplot.subplots_adjust(wspace=0,hspace=0)
# pyplot.savefig('E:\\Dicom\\test\\images\\'+'MIP_image.jpg')
pyplot.show()