• MONAI怎么读取image,label不报错


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

    1. 不要慌

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

    1. #!/usr/bin/env python
    2. # coding: utf-8
    3. # In[21]:
    4. #separate labels from folder to dicom_label
    5. import os
    6. import shutil
    7. def find_file_starts(path):
    8. finds = []
    9. for i, j, k in os.walk(path):
    10. for file in k:
    11. if file.startswith("RS"):
    12. print(os.path.join(i, file))
    13. finds.append(os.path.join(i, file))
    14. return finds
    15. def movefile(oripath,tardir):
    16. filename = os.path.basename(oripath)
    17. tarpath = os.path.join(tardir, filename)
    18. #判断原始文件路劲是否存在
    19. if not os.path.exists(oripath):
    20. print('the dir is not exist:%s' % oripath)
    21. status = 0
    22. else:
    23. #判断目标文件夹是否存在
    24. if os.path.exists(tardir):
    25. #判断目标文件夹里原始文件是否存在,存在则删除
    26. if os.path.exists(tarpath):
    27. os.remove(tarpath)
    28. else:
    29. #目标文件夹不存在则创建目标文件夹
    30. os.makedirs(tardir)
    31. #移动文件
    32. shutil.move(oripath, tardir)
    33. status = 1
    34. return status
    35. ##for CTV
    36. path_2_all_patients = r'C:\Users\jinpe\Desktop\MONAI_SEG\dataset\sis_trial\CTV\dicom_CT'
    37. label_files = find_file_starts(path_2_all_patients)
    38. tardir_loc = r'C:\Users\jinpe\Desktop\MONAI_SEG\dataset\sis_trial\CTV\dicom_label'
    39. for file in label_files:
    40. movefile(file,tardir_loc)
    41. ##for OAR
    42. path_2_all_patients = r'C:\Users\jinpe\Desktop\MONAI_SEG\dataset\sis_trial\OAR\dicom_CT'
    43. patient_names = os.listdir(path_2_all_patients)
    44. label_files = find_file_starts(path_2_all_patients)
    45. tardir_loc = r'C:\Users\jinpe\Desktop\MONAI_SEG\dataset\sis_trial\OAR\dicom_label'
    46. #Due to different sort for label files, have to rename the labels and CTs
    47. for i,file in enumerate(label_files):
    48. new_name = file.replace("RS",str(i)+"RS")
    49. new_name = os.path.join(tardir_loc,os.path.basename(new_name))
    50. print(new_name)
    51. os.rename(file,new_name)#rename and copy the label
    52. #movefile(file,tardir_loc)
    53. for i,patient in enumerate(patient_names):
    54. os.rename(os.path.join(path_2_all_patients,patient),os.path.join(path_2_all_patients,str(i)+patient))#rename the dicom CT
    55. # In[ ]:
    56. '''
    57. #optional
    58. #transform dicom_CT to nii_CT
    59. !python -c "import dicom2nifti" || pip install -q dicom2nifti
    60. import os
    61. path_2_all_patients = r'C:\Users\jinpe\Desktop\MONAI_SEG\dataset\sis_trial\dicom_CT'
    62. patients_folders = os.listdir(path_2_all_patients)
    63. path_out_data = r'C:\Users\jinpe\Desktop\MONAI_SEG\dataset\sis_trial\nii_CT'
    64. print(patients_folders)
    65. for i, patient in enumerate(patients_folders):
    66. dicom2nifti.dicom_series_to_nifti(os.path.join(path_2_all_patients, patient), os.path.join(path_out_data, str(i)+"_"+pati
  • 相关阅读:
    1000道最新高频Java面试题,覆盖25个技术栈,从底层原理到架构
    pandas的使用
    【CSS】你还记得组合选择器怎么用吗?
    如何在虚拟专用服务器上配置 Nginx Web 服务器
    edge浏览器第三方插件网站
    SpringBoot多线程环境下,解决多个定时器冲突问题(荣耀典藏版)
    链路追踪标准
    力扣142 - 环形链表||【二重双指针+哈希表】
    【MySQL】 MySQL 死锁问题分析优化器特性及优化方案
    英诺森 “供应链智能数据平台”荣获“科技进步奖”
  • 原文地址:https://blog.csdn.net/Jinpeijie217/article/details/127640047