gll89的个人博客分享 http://blog.sciencenet.cn/u/gll89

博文

在ubuntu上安装: conda install -c conda-forge pydicom 读取dicom并可视

已有 11101 次阅读 2019-6-10 11:05 |个人分类:Python|系统分类:科研笔记

  1. 在ubuntu上安装: conda install -c conda-forge pydicom

  2. 读取dicom并可视化

    import pydicom, os

    import matplotlib.pyplot as plt

    path = os.path.join(root, file)

    im = pydicom.dcmread(path)   # type of data is FileDataset

    px = im.pixel_array          # convert im to the type of numpy.ndarray

    plt.figure()

    plt.imshow(px)

    plt.show()          # do not forget this

  3. 更改一个dicom文件的图像信息   

    import dicom, dicom.UID
    from dicom.dataset import Dataset, FileDataset
    import numpy as np
    import datetime, time
    
    def write_dicom(pixel_array,filename):
        """
        INPUTS:
        pixel_array: 2D numpy ndarray.  If pixel_array is larger than 2D, errors.
        filename: string name for the output file.
        """
    
        ## This code block was taken from the output of a MATLAB secondary
        ## capture.  I do not know what the long dotted UIDs mean, but
        ## this code works.
        file_meta = Dataset()
        file_meta.MediaStorageSOPClassUID = 'Secondary Capture Image Storage'
        file_meta.MediaStorageSOPInstanceUID = '1.3.6.1.4.1.9590.100.1.1.111165684411017669021768385720736873780'
        file_meta.ImplementationClassUID = '1.3.6.1.4.1.9590.100.1.0.100.4.0'
        ds = FileDataset(filename, {},file_meta = file_meta,preamble="\0"*128)
        ds.Modality = 'WSD'
        ds.ContentDate = str(datetime.date.today()).replace('-','')
        ds.ContentTime = str(time.time()) #milliseconds since the epoch
        ds.StudyInstanceUID =  '1.3.6.1.4.1.9590.100.1.1.124313977412360175234271287472804872093'
        ds.SeriesInstanceUID = '1.3.6.1.4.1.9590.100.1.1.369231118011061003403421859172643143649'
        ds.SOPInstanceUID =    '1.3.6.1.4.1.9590.100.1.1.111165684411017669021768385720736873780'
        ds.SOPClassUID = 'Secondary Capture Image Storage'
        ds.SecondaryCaptureDeviceManufctur = 'Python 2.7.3'
    
        ## These are the necessary imaging components of the FileDataset object.
        ds.SamplesPerPixel = 1
        ds.PhotometricInterpretation = "MONOCHROME2"
        ds.PixelRepresentation = 0
        ds.HighBit = 15
        ds.BitsStored = 16
        ds.BitsAllocated = 16
        ds.SmallestImagePixelValue = '\\x00\\x00'
        ds.LargestImagePixelValue = '\\xff\\xff'
         
    
        return
    
     if __name__ == "__main__":
        x = np.arange(16).reshape(16,1)
        pixel_array = (x + x.T) * 32
        pixel_array = np.tile(pixel_array,(16,16))
        write_dicom(pixel_array,'pretty.dcm')

From: https://stackoverflow.com/questions/14350675/create-pydicom-file-from-numpy-array




https://blog.sciencenet.cn/blog-1969089-1184125.html

上一篇:基金申请书经验
下一篇:Pydicom基础知识
收藏 IP: 60.191.2.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-4-23 14:45

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部