-
Notifications
You must be signed in to change notification settings - Fork 0
/
dicom_io.py
336 lines (294 loc) · 13 KB
/
dicom_io.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import os, sys
import numpy as np
import pydicom
from Siemens_dicom_structreader import Unpacker
import re
def natural_sort(l):
convert = lambda text: int(text) if text.isdigit() else text.lower()
alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)]
return sorted(l, key=alphanum_key)
class Dicom(object):
def __init__(self, inputpath):
self.input = inputpath
def initialize(self):
# Find the paths to any images we can denoise and hold on to them
DcmDict = dict(dataReadOrder = [], matrixsize = [])
dims = None
print('...reading input data')
#for (subdir, dirs, files) in os.walk(self.input):
files = list()
if not os.path.exists(self.input):
sys.exit('input dicom directory not found')
for (dirpath, dirnames, filenames) in os.walk(self.input):
files += [os.path.join(dirpath, file) for file in filenames]
files = natural_sort(np.unique(files).tolist())
for f in files:
if not f or '.DS_Store' in f or 'ED_0001' in f:
continue
dcmpath = os.path.join(f)
info = pydicom.dcmread(dcmpath, force=True)
if not hasattr(info, 'SeriesDescription'):
continue
ismosaic = False
isorig = False
if 'ORIGINAL' in info.ImageType:
isorig = True
if isorig:
if dims is None:
dims = [info.Rows, info.Columns]
elif [info.Rows, info.Columns] != dims:
print('...series dimension mismatch, skipping slice. check manually')
continue
else:
dims = [info.Rows, info.Columns]
DcmDict["dataReadOrder"].append(dcmpath)
DcmDict["matrixsize"].append(dims)
if not DcmDict["dataReadOrder"]:
sys.exit('no data found')
im = self.readDicom(DcmDict)
return im, DcmDict
def getnewSOPiUID(self):
"""
Creates an SOPUID based on template dicom. This is deprecated by pydicom.generate_uid()
"""
dcmlist = os.listdir(self.input)
dcmlist = [x for x in dcmlist if not x == '.DS_Store']
newSOPUID = []
for i in range(0, len(dcmlist)):
info = pydicom.dcmread(os.path.join(self.input, dcmlist[i]), force=True)
SOPUID = info.SOPInstanceUID
newSOPUID.append(SOPUID[:-16] + str(int(np.floor(1000000000 + 8999999999*np.random.random()))) + SOPUID[-6:])
return newSOPUID
def readDicom(self, dcmdict):
dcmlist = dcmdict["dataReadOrder"]
dcmlist = [x for x in dcmlist if not x == '.DS_Store']
info = pydicom.dcmread(dcmlist[0], force=True)
if not hasattr(info, 'ImageType'):
info.ImageType = 'None'
ismosaic = False
if 'MOSAIC' in info.ImageType:
ismosaic = True
if 'DIFFUSION' in info.ImageType:
grad = np.zeros((len(dcmlist), 4))
else:
grad = None
datasize = [info.Rows, info.Columns]
if hasattr(info, 'AcquisitionMatrix'):
matrixsize = list(info.AcquisitionMatrix[i] for i in [0, 3])
if ismosaic:
NumberOfTiles = [int(x)/int(y) for x,y in zip(datasize, matrixsize)]
csa = info[0x0029,0x1010].value
csaobj = Unpacker(csa, endian='<')
csadict = csaobj.csaread(csa)
nmos = csadict['tags']['NumberOfImagesInMosaic']['items']
nmos = nmos[0]
else:
NumberOfTiles = [0,0]
nmos = 1
#print('mosaic size = ' + str(NumberOfTiles))
#print('matrix size = ' + str(matrixsize))
if ismosaic:
img = np.zeros((matrixsize[0], matrixsize[1], nmos, len(dcmlist)))
# reformat from mosaic to 4D image
for i in range(0, len(dcmlist)):
info = pydicom.dcmread(dcmlist[i], force=True)
data = info.pixel_array
data = np.reshape(data, (datasize[0], datasize[1]), order='F')
if 'DIFFUSION' in info.ImageType:
csa = info[0x0029,0x1010].value
csadict = csaobj.csaread(csa)
bvec = csadict['tags']['DiffusionGradientDirection']['items']
bval = csadict['tags']['B_value']['items']
# bval = info[0x0019,0x0100c].value
bval = bval[0]
if bval == 0:
bvec = [0, 0 ,0]
grad[i,:3] = np.array(bvec)
grad[i,-1] = bval
c = 0
for j in range(1, int(NumberOfTiles[0])+1):
for k in range(1, int(NumberOfTiles[1])+1):
if c > nmos-1:
continue
img[:,:,c,i] = data[(j-1)*matrixsize[0]:(j)*matrixsize[0], (k-1)*matrixsize[1]:(k)*matrixsize[1]]
c = c+1
else:
if len(dcmlist) > 1:
img = np.zeros((datasize[0], datasize[1], len(dcmlist)))
for i in range(0, len(dcmlist)):
info = pydicom.dcmread(dcmlist[i], force=True)
print(dcmlist[i])
data = info.pixel_array
img[:,:,i] = np.squeeze(data)
#dwi[:,:,i] = np.reshape(data, (datasize[0], datasize[1]), order='F')
else:
info = pydicom.dcmread(dcmlist[0], force=True)
img = np.squeeze(info.pixel_array).transpose(1,2,0)
return img
def writeMosaicDicom(self, Signal, path, outputpath, scale):
if isinstance(outputpath, str):
if not os.path.isdir(outputpath):
os.makedirs(outputpath)
else:
for i in outputpath:
root, base = os.path.split(i)
if not os.path.isdir(root):
os.mkdir(root)
SN = []
SI = []
if len(path) == 1:
newSI = []
newSN = []
dcmlist = path
outlist = outputpath
info = pydicom.dcmread(dcmlist[0], force=True)
sn = info.SeriesNumber
newsn = sn + np.random.randint(1000) + 200
SN.append(sn)
newSN.append(newsn)
si = info.SeriesInstanceUID
newsi = si[:-16] + str(int(np.floor(1000000000 + 8999999999*np.random.random()))) + si[-6:]
SI.append(si)
newSI.append(newsi)
else:
for i in path:
info = pydicom.dcmread(i, force=True)
sn = info.SeriesNumber
SN.append(sn)
si = info.SeriesInstanceUID
SI.append(si)
newSI = []
newSN = []
newuniquesn = []
newuniquesi = []
for i in range(0, len(np.unique(SN))):
newuniquesn.append(np.unique(SN)[i] + np.random.randint(200) + 200)
newuniquesi.append(np.unique(SI)[i][:-16] + str(int(np.floor(1000000000 + 8999999999*np.random.random()))) + np.unique(SI)[i][-6:])
for j in range(0, len(path)):
for i in range(0, len(np.unique(SN))):
if SN[j] == np.unique(SN)[i]:
newSN.append(newuniquesn[i])
newSI.append(newuniquesi[i])
dcmlist = path
outlist = outputpath
info = pydicom.dcmread(dcmlist[0], force=True)
datasize = [info.Rows, info.Columns]
matrixsize = list(info.AcquisitionMatrix[i] for i in [0, 3])
NumberOfTiles = [int(x)/int(y) for x,y in zip(datasize, matrixsize)]
csa = info[0x0029,0x1010].value
csaobj = Unpacker(csa, endian='<')
csadict = csaobj.csaread(csa)
nmos = csadict['tags']['NumberOfImagesInMosaic']['items'][0]
newSeriesNumber = 0
for i in range(0, len(dcmlist)):
info = pydicom.read_file(dcmlist[i], force=True)
SeriesID = info.SeriesInstanceUID
SeriesNumber = info.SeriesNumber
for ind, num in enumerate(SN):
if SeriesNumber == SN[ind]:
newSeriesNumber = newSN[ind]
if SeriesID == SI[ind]:
newSeriesID = newSI[ind]
info.SeriesInstanceUID = newSeriesID
info.SeriesNumber = newSeriesNumber
SeriesDescription = info.SeriesDescription
newSeriesDescription = SeriesDescription + ' hb_skullstripped'
info.SeriesDescription = newSeriesDescription
SOPUID = info.SOPInstanceUID
newSOPUID = SOPUID[:-16] + str(int(np.floor(1000000000 + 8999999999*np.random.random()))) + SOPUID[-6:]
info.SOPInstanceUID = newSOPUID
info.CommentsOnPerformedProcedureStep = 'test image, DO NOT READ'
imgdn = np.zeros((datasize[0], datasize[1]))
c = 0
for j in range(1, int(NumberOfTiles[0])+1):
for k in range(1, int(NumberOfTiles[1])+1):
if c > nmos - 1:
continue
if np.ndim(Signal) == 3:
imgdn[(j-1)*matrixsize[0]:(j)*matrixsize[0], (k-1)*matrixsize[1]:(k)*matrixsize[1]] = Signal[:,:,c]
else:
imgdn[(j-1)*matrixsize[0]:(j)*matrixsize[0], (k-1)*matrixsize[1]:(k)*matrixsize[1]] = Signal[:,:,c,i]
c = c + 1
if scale:
imgdn = imgdn * 1000
imgdn[imgdn > 65535] = 0
imgdn[imgdn < 0] = 0
imgdn = imgdn.astype('uint16')
info.PixelData = imgdn.tobytes()
if np.ndim(Signal) == 3:
rootpath = outputpath
root, base = os.path.split(path[0])
tmp, ext = os.path.splitext(base)
else:
rootpath, base = os.path.split(outlist[i])
info.save_as(os.path.join(rootpath, base.replace(".dcm",'_new.dcm')))
def writeVolDicom(self, Signal, path, outputpath, scale):
if isinstance(outputpath, str):
if not os.path.isdir(outputpath):
os.makedirs(outputpath)
else:
for i in outputpath:
root, base = os.path.split(i)
if not os.path.isdir(root):
os.mkdir(root)
SI = []
SN = []
for i in path:
info = pydicom.dcmread(i, force=True)
sn = info.SeriesNumber
SN.append(sn)
si = info.SeriesInstanceUID
SI.append(si)
newSI = []
newSN = []
newuniquesn = []
newuniquesi = []
for i in range(0, len(np.unique(SN))):
newuniquesn.append(np.unique(SN)[i] + 100)
newuniquesi.append(np.unique(SI)[i][:-16] + str(int(np.floor(1000000000 + 8999999999*np.random.random()))) + np.unique(SI)[i][-6:])
for j in range(0, len(path)):
for i in range(0, len(np.unique(SN))):
if SN[j] == np.unique(SN)[i]:
newSN.append(newuniquesn[i])
newSI.append(newuniquesi[i])
dcmlist = path
outlist = outputpath
info = pydicom.dcmread(dcmlist[0], force=True)
datasize = [info.Rows, info.Columns]
if hasattr(info, 'AcquisitionMatrix'):
matrixsize = list(info.AcquisitionMatrix[i] for i in [0, 3])
for i in range(0, len(dcmlist)):
info = pydicom.read_file(dcmlist[i], force=True)
SeriesID = info.SeriesInstanceUID
SeriesNumber = info.SeriesNumber
for ind, num in enumerate(SN):
if SeriesNumber == SN[ind]:
newSeriesNumber = newSN[ind]
if SeriesID == SI[ind]:
newSeriesID = newSI[ind]
info.SeriesInstanceUID = newSeriesID
info.SeriesNumber = newSeriesNumber
SeriesDescription = info.SeriesDescription
newSeriesDescription = SeriesDescription + ' hb_skullstripped'
info.SeriesDescription = newSeriesDescription
SOPUID = info.SOPInstanceUID
newSOPUID = SOPUID[:-16] + str(int(np.floor(1000000000 + 8999999999*np.random.random()))) + SOPUID[-6:]
info.SOPInstanceUID = newSOPUID
info.CommentsOnPerformedProcedureStep = 'Research image, DO NOT READ'
#import pdb; pdb.set_trace()
if len(dcmlist) > 1:
imgdn = Signal[:,:,i]
else:
imgdn = Signal
imgdn[imgdn > 65500] = 0
imgdn[imgdn < 0] = 0
imgdn = imgdn.astype('uint16')
info.PixelData = imgdn.tobytes()
#if np.ndim(Signal) == 3:
# rootpath = outputpath
# root, base = os.path.split(path[0])
# tmp, ext = os.path.splitext(base)
#else:
rootpath, base = os.path.split(outlist[i])
iname = os.path.join(rootpath, base + '_' + str(info.SeriesNumber) + '_new.dcm')
info.save_as(iname)