在将dcm文件转nii格式的时候报错
Traceback (most recent call last):
File "E:\pycharmproject\Breast3\prepare\1dcm转为nii文件.py", line 47, in <module>
dcm2nii(dcm_xulie_path, os.path.join(save_path, f'{name}.nii'))
File "E:\pycharmproject\Breast3\prepare\1dcm转为nii文件.py", line 33, in dcm2nii
sitk.WriteImage(image3, os.path.join(nii_path, ))
File "D:\anaconda3\envs\pytorch\lib\site-packages\SimpleITK\extra.py", line 425, in WriteImage
return writer.Execute(image)
File "D:\anaconda3\envs\pytorch\lib\site-packages\SimpleITK\SimpleITK.py", line 7907, in Execute
return _SimpleITK.ImageFileWriter_Execute(self, *args)
RuntimeError: Exception thrown in SimpleITK ImageFileWriter_Execute: D:\a\1\sitk-build\ITK\Modules\IO\NIFTI\src\itkNiftiImageIO.cxx:2254:
ITK ERROR: NiftiImageIO(000001F74304E4C0): ERROR: nifti library failed to write imageC:\Users\Administrator\Desktop\Breast\NIIdata\低表达\T1\AN_HAI_YING.nii
代码如下
import os
import shutil
import numpy as np
import pandas as pd
import pydicom
import nibabel as nib
import SimpleITK as sitk
'''
dcms_root_path文件夹下是每一个患者的姓名,每个患者的姓名文件夹下是每张dcm文件
dcm2nii.py 读取dcm文件,并生成nii文件
nii_path是一个新的文件夹,存放新生成的nii文件
这个脚本只能处理一个序列,每个序列都要执行一遍
'''
def dcm2nii(dcms_path, nii_path):
# 1.构建dicom序列文件阅读器,并执行(即将dicom序列文件“打包整合”)
reader = sitk.ImageSeriesReader()
dicom_names = reader.GetGDCMSeriesFileNames(dcms_path)
reader.SetFileNames(dicom_names)
image2 = reader.Execute()
# 2.将整合后的数据转为array,并获取dicom文件基本信息
image_array = sitk.GetArrayFromImage(image2) # z, y, x
origin = image2.GetOrigin() # x, y, z
spacing = image2.GetSpacing() # x, y, z
direction = image2.GetDirection() # x, y, z
# 3.将array转为img,并保存为.nii.gz
image3 = sitk.GetImageFromArray(image_array)
image3.SetSpacing(spacing)
image3.SetDirection(direction)
image3.SetOrigin(origin)
sitk.WriteImage(image3, os.path.join(nii_path, ))
dcms_root_path = r'C:\Users\Administrator\Desktop\Breast\ki67原始数据\低表达\T1' # dicom序列文件所在路径
nii_path = r'C:\Users\Administrator\Desktop\Breast\NIIdata\低表达\T1' # 所需.nii.gz文件保存路径
if os.path.exists(nii_path):
shutil.rmtree(nii_path) # 如果 save_path 存在,则删除
os.makedirs(nii_path) # 生成 save_path
patients = os.listdir(dcms_root_path)
for i , p in enumerate(patients):
dcm_xulie_path = os.path.join(dcms_root_path, p)
name = p.split("-")[0]
save_path = os.path.join(nii_path)
dcm2nii(dcm_xulie_path, os.path.join(save_path, f'{name}.nii'))
print(f'正在处理第{i+1}个文件')
print("finished")
这里在保存的路径时:nii_path = r'C:\Users\Administrator\Desktop\Breast\NIIdata\低表达\T1' # 所需.nii.gz文件保存路径,路径里有中文,所以导致了错误,修改为全英文就会好了