"TypeError: a bytes-like object is required, not 'str'" reading compressed DICOM volume into numpy array
"TypeError: a bytes-like object is required, not 'str'" reading compressed DICOM volume into numpy array
我在 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)
returns
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
returns12.
我试着用下面的代码阅读这本书。
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()
returns a char*
类型为 Python str
,所以对于 Python 3 它需要编码回来bytes
如所述 here:
gdcm_array = image.GetBuffer().encode("utf-8", "surrogateescape")
我在 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)
returns
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
returns12.
我试着用下面的代码阅读这本书。
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()
returns a char*
类型为 Python str
,所以对于 Python 3 它需要编码回来bytes
如所述 here:
gdcm_array = image.GetBuffer().encode("utf-8", "surrogateescape")