gpt4 book ai didi

python - "TypeError: a bytes-like object is required, not ' str '"将压缩的 DICOM 卷读入 numpy 数组

转载 作者:行者123 更新时间:2023-12-01 13:11:48 25 4
gpt4 key购买 nike

我在 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() 返回一个 char* 类型作为 Python str,所以对于 Python 3 它需要被编码回来为 bytes,如 here 所述:

gdcm_array = image.GetBuffer().encode("utf-8", "surrogateescape")

关于python - "TypeError: a bytes-like object is required, not ' str '"将压缩的 DICOM 卷读入 numpy 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59505105/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com