python - 使用 12 位图像从头开始创建新的 DICOM

标签 python dicom pydicom

我正在尝试使用 pydicom 从头开始​​创建一个新的 dicom 文件。我在 stackoverflowpydicom github examples 的以下链接的帮助下构建了一个文件。这是我的函数:

def generate_dicom(pixel_array,filename):

    sop_uid = pydicom.uid.generate_uid()
    sop_class_uid = pydicom._storage_sopclass_uids.ComputedRadiographyImageStorage

    #create metadata
    file_meta = Dataset()
    file_meta.MediaStorageSOPClassUID = sop_class_uid
    file_meta.MediaStorageSOPInstanceUID = sop_uid
    file_meta.ImplementationClassUID = '1.2.276.0.7230010.3.0.3.6.2'
    file_meta.FileMetaInformationGroupLength = 218
    file_meta.FileMetaInformationVersion = b'\x00\x01'
    file_meta.ImplementationVersionName = 'OFFIS_DCMTK_362'
    file_meta.SourcApplicationEntitTitle = 'AZMED'

    ds = FileDataset(filename, {},file_meta = file_meta,preamble=b"\x00" * 128)

    # Set the transfer syntax implicitly, if not set explicitly above
    ds.is_little_endian = True
    ds.is_implicit_VR = True

    #change modality
    ds.Modality = 'CR'

    # Set creation date/time
    dt = datetime.datetime.now()
    ds.ContentDate = dt.strftime('%Y%m%d')
    timeStr = dt.strftime('%H%M%S.%f')  # long format with micro seconds
    ds.ContentTime = timeStr

    ds.StudyInstanceUID = pydicom.uid.generate_uid()
    ds.SeriesInstanceUID = pydicom.uid.generate_uid()
    ds.SOPInstanceUID =  sop_uid
    ds.SOPClassUID = sop_class_uid
    ds.SecondaryCaptureDeviceManufctur = 'AZMed_SAS'
    ds.FrameOfReferenceUID = pydicom.uid.generate_uid()

    # set patient name and ID
    ds.PatientName = "Test^Firstname"
    ds.PatientID = "123456"

    ## These are the necessary imaging components of the FileDataset object.
    # we create 16 bit pixel arrays here
    ds.SamplesPerPixel = 1
    ds.PhotometricInterpretation = "MONOCHROME2"
    ds.PixelRepresentation = 0
    ds.HighBit = 15
    ds.BitsStored = 16
    ds.BitsAllocated = 16
    #ds.SmallestImagePixelValue = b"\x00\x00"
    #ds.LargestImagePixelValue = b"\xff\xff"
    ds.Columns = pixel_array.shape[0]
    ds.Rows = pixel_array.shape[1]

    if pixel_array.dtype != np.uint16:
        pixel_array = pixel_array.astype(np.uint16)

    ds.PixelData = pixel_array.tostring()
    ds.ImagesInAcquisition = "1"

    # reutrn the dataset of dicom
    return ds

对于这个函数,我使用 cv2 传递图像:

new_pixel_array = cv2.imread('/home/toing/image_01.png', -1)

当我将形状为 (1500, 200)、12 位值的 new_pixel_array 传递给我的函数并输入 uint16 到我的函数时,我得到一个当我尝试读取生成的 dicom 时,图像非常模糊。

ds = generate_dicom(new_pixel_array, 'toing.dcm')
ds.pixel_array

enter image description here 但是,当我尝试通过时:

x = np.arange(16).reshape(16,1)
new_pixel_array = (x + x.T) * 32
new_pixel_array = np.tile(new_pixel_array,(16,16))

按照 this 答案中的建议,我正确显示了所需的平铺图像。

在传递给我的函数之前,我还尝试将 new_pixel_array 转换为 8 位值,但我仍然得到相同的结果。另一方面,生成的平铺图像在转换时工作正常。 我在这里做错了什么?

最佳答案

在我看来,尺寸设置不正确。

如果我理解正确,您的图像应该是 (1500, 200) 即高度为 1500 像素,宽度为 200 像素。

ds.Columns = pixel_array.shape[0]  # 1500
ds.Rows = pixel_array.shape[1]     # 200

在您提供的代码片段中,您将宽度或列设置为形状的第一个值。

尝试更改这些值

关于python - 使用 12 位图像从头开始创建新的 DICOM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60599618/

相关文章:

python - 如何使用 Python 列出目录

Django 模型中的 Python factory_boy 库 m2m?

python - 匹配两个字符之间的字符串

python - 在Python中将两个时间序列与日期时间索引的不同元素组合

python - 将 DICOM 标签转换成更易读的东西

python - Anaconda 及包安装(pydicom)

python - 无法使用 pynetdicom3 访问 PACS

image-processing - 有没有办法压缩 DICOM 数据?

DICOM:SOPInstanceUID 标签有什么意义?

python - int16、uint8 等图像背后的逻辑