Python &引用;TypeError:需要类似字节的对象,而不是';str'&引用;将压缩的DICOM卷读入numpy阵列

Python &引用;TypeError:需要类似字节的对象,而不是';str'&引用;将压缩的DICOM卷读入numpy阵列,python,dicom,pydicom,gdcm,Python,Dicom,Pydicom,Gdcm,我正在CentOS 7上的Anaconda Spyder上使用Python 3.7.3 我有一个单独文件中的3D DICOM卷:/usr/share/aliza/datasets/DICOM/00_MR/Tra_FLAIR.dcm print(image.file_meta.TransferSyntaxUID) 返回 1.2.840.10008.1.2.4.70 import pydicom as dicom image=dicom.read_file('/usr/share/aliza/

我正在CentOS 7上的Anaconda Spyder上使用Python 3.7.3

我有一个单独文件中的3D DICOM卷:/usr/share/aliza/datasets/DICOM/00_MR/Tra_FLAIR.dcm

print(image.file_meta.TransferSyntaxUID)
返回

1.2.840.10008.1.2.4.70


import pydicom as dicom
image=dicom.read_file('/usr/share/aliza/datasets/DICOM/00_MR/Tra_FLAIR.dcm')
image.pixel_array
image.BitsStored
返回12

我试着用下面的代码阅读这本书

import numpy as np
import gdcm

def get_numpy_array_type(gdcm_pixel_format):
    """Returns a numpy array typecode given a GDCM Pixel Format."""
    return get_gdcm_to_numpy_typemap()[gdcm_pixel_format]

def get_gdcm_to_numpy_typemap():
    """Returns the GDCM Pixel Format to numpy array type mapping."""
    _gdcm_np = {gdcm.PixelFormat.UINT8  :np.int8,
                gdcm.PixelFormat.INT8   :np.uint8,
                gdcm.PixelFormat.UINT16 :np.uint16,
                gdcm.PixelFormat.INT16  :np.int16,
                gdcm.PixelFormat.UINT32 :np.uint32,
                gdcm.PixelFormat.INT32  :np.int32,
                gdcm.PixelFormat.FLOAT32:np.float32,
                gdcm.PixelFormat.FLOAT64:np.float64 }
    return _gdcm_np

r = gdcm.ImageReader()
r.SetFileName('/usr/share/aliza/datasets/DICOM/00_MR/Tra_FLAIR.dcm')
if not r.Read():
    print('Error reading input')

image = gdcm.Image()
ir = r.GetImage()
dims = ir.GetDimensions()

image.SetDimension(0, ir.GetDimension(0) );
image.SetDimension(1, ir.GetDimension(1) );
image.SetDimension(2, ir.GetDimension(2) );

pixeltype = ir.GetPixelFormat();
image.SetPixelFormat( pixeltype );

pi = ir.GetPhotometricInterpretation();
image.SetPhotometricInterpretation( pi );

pixeldata = gdcm.DataElement( gdcm.Tag(0x7fe0,0x0010) )
str1 = ir.GetBuffer()

image.SetDataElement( pixeldata )
pf = image.GetPixelFormat()
dtype = get_numpy_array_type(pf.GetScalarType())
gdcm_array = image.GetBuffer()
result = np.frombuffer(gdcm_array, dtype=dtype)
这导致

TypeError: a bytes-like object is required, not 'str'

GDCM的
Image.GetBuffer()
以Python
str
的形式返回一个
char*
类型,因此对于Python 3,需要将其编码回
字节,如下所述:

gdcm_array=image.GetBuffer().encode(“utf-8”,“代理景观”)