dicom.py 文件源码

python
阅读 26 收藏 0 点赞 0 评论 0

项目:som 作者: vsoch 项目源码 文件源码
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
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号