def read_series(dicoms,return_nifti=True):
'''read_series will read in a series of dicoms belonging to a group
:param dicoms: a list of dicom files to parse, assumed in same series and
equal size
:param return_nifti: If True (default) will turn image as Nifti File
'''
# Sort the dicoms
dicoms.sort()
# Get the size of the image
params = sniff_header(dicoms[0])
xdim = params['xdim']
ydim = params['ydim']
windom_center = params['window_center']
bot.debug("First dicom found with dimension %s by %s, using as standard." %(xdim,ydim))
# Let's get ordering of images based on InstanceNumber
ordered = dict()
for d in range(len(dicoms)):
ds = dicom.read_file(dicoms[d])
if ds.Rows == xdim and ds.Columns == ydim:
ordered[int(ds.InstanceNumber)] = ds.pixel_array
# Sort by order
zdim = len(ordered)
data = numpy.ndarray((xdim,ydim,zdim))
# Start at window center, then go back to zero
index = 0
for key in sorted(ordered.keys()):
data[:,:,index] = ordered[key]
index +=1
if return_nifti == True:
affine = numpy.diag((1,1,1,0))
data = nibabel.Nifti1Image(data,affine=affine)
return data
评论列表
文章目录