MONAI怎么读取image,label不报错

 当运行MONAI框架一直报错的情况下。

1. 不要慌

2. 查看数据格式是否有问题,dicom转nii批处理代码如下,使用dcmrtstruct2nii包

#!/usr/bin/env python
# coding: utf-8

# In[21]:


#separate labels from folder to dicom_label
import os
import shutil

def find_file_starts(path):
    finds = []
    for i, j, k in os.walk(path):
        for file in k:
            if file.startswith("RS"):
                print(os.path.join(i, file))
                finds.append(os.path.join(i, file))
    return finds           

                
def movefile(oripath,tardir):
    filename = os.path.basename(oripath)
    tarpath = os.path.join(tardir, filename)
    #判断原始文件路劲是否存在
    if not os.path.exists(oripath):
        print('the dir is not exist:%s' % oripath)
        status = 0
    else:
     #判断目标文件夹是否存在
        if os.path.exists(tardir):
        #判断目标文件夹里原始文件是否存在,存在则删除
            if os.path.exists(tarpath):
                os.remove(tarpath)
        else:
         #目标文件夹不存在则创建目标文件夹
            os.makedirs(tardir)
          #移动文件
        shutil.move(oripath, tardir)

        status = 1

    return status

##for CTV
path_2_all_patients = r'C:\Users\jinpe\Desktop\MONAI_SEG\dataset\sis_trial\CTV\dicom_CT'
label_files = find_file_starts(path_2_all_patients)
tardir_loc = r'C:\Users\jinpe\Desktop\MONAI_SEG\dataset\sis_trial\CTV\dicom_label'
for file in label_files:
    movefile(file,tardir_loc)

##for OAR
path_2_all_patients = r'C:\Users\jinpe\Desktop\MONAI_SEG\dataset\sis_trial\OAR\dicom_CT'
patient_names = os.listdir(path_2_all_patients)

label_files = find_file_starts(path_2_all_patients)
tardir_loc = r'C:\Users\jinpe\Desktop\MONAI_SEG\dataset\sis_trial\OAR\dicom_label'
#Due to different sort for label files, have to rename the labels and CTs
for i,file in enumerate(label_files):
    new_name = file.replace("RS",str(i)+"RS")
    new_name = os.path.join(tardir_loc,os.path.basename(new_name))
    print(new_name)
    os.rename(file,new_name)#rename and copy the label
    #movefile(file,tardir_loc)
    
for i,patient in enumerate(patient_names):
    os.rename(os.path.join(path_2_all_patients,patient),os.path.join(path_2_all_patients,str(i)+patient))#rename the dicom CT


# In[ ]:


'''
#optional
#transform dicom_CT to nii_CT
!python -c "import dicom2nifti" || pip install -q dicom2nifti
import os
path_2_all_patients = r'C:\Users\jinpe\Desktop\MONAI_SEG\dataset\sis_trial\dicom_CT'
patients_folders = os.listdir(path_2_all_patients)
path_out_data = r'C:\Users\jinpe\Desktop\MONAI_SEG\dataset\sis_trial\nii_CT'
print(patients_folders)

for i, patient in enumerate(patients_folders):
    dicom2nifti.dicom_series_to_nifti(os.path.join(path_2_all_patients, patient), os.path.join(path_out_data, str(i)+"_"+pati
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值