我想将一系列图像的文件类型从 .dcm 转换为 .mha。以下是我的代码:
import numpy
import pydicom
import os
PathDicom ='./DicomResource'
lstFilesDCM = []
for dirName, subdirList, fileList in os.walk(PathDicom):
for filename in fileList:
if '.dcm' in filename.lower():
lstFilesDCM.append(os.path.join(dirName, filename))
RefDs = pydicom.read_file(lstFilesDCM[0])
ConstPixelDims = (int(RefDs.Rows), int(RefDs.Columns), len(lstFilesDCM))
#RefDs.PixelSpacing = 0
ConstPixelSpacing = (float(RefDs.PixelSpacing[0]),
float(RefDs.PixelSpacing[1]), float(RefDs.SliceThickness))
info = ConstPixelDims + ConstPixelSpacing
f = open('info.txt', 'w')
for n in info:
f.write(str(n)+' ')
f.close()
location = []
for i in range(len(lstFilesDCM)):
ds = pydicom.read_file(lstFilesDCM[i])
location.append(ds.SliceLocation)
location.sort()
ArrayDicom = numpy.zeros((len(lstFilesDCM), RefDs.Rows, RefDs.Columns),
dtype=RefDs.pixel_array.dtype)
for filenameDCM in lstFilesDCM:
ds = pydicom.read_file(filenameDCM)
ArrayDicom[location.index(ds.SliceLocation), :, :] = ds.pixel_array
ds = ArrayDicom.tostring()
f = open('1.mha', 'wb')
f.write(ds)
f.close()
有了这个,我收到以下错误:
AttributeError:“FileDataset”对象没有属性“PixelSpacing”
我也尝试添加RefDs.PixelSpacing = 0
. 它引发下一个错误。
有没有人可以帮我解决这个问题?