• 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
  • 相关阅读:
    LVS+KeepAlived高可用负载均衡集群
    LeetCode-81. 搜索旋转排序数组 II-Java-medium
    如何高效且优雅地使用Redis
    SpringCore 完整学习教程1,入门级别
    Spark - 一文搞懂 parquet
    【Spring使用三级缓存解决循环依赖的过程】
    Docker:容器
    【数据结构】10道经典面试题目带你玩转链表
    clickhouse分布式之弹性扩缩容的故事
    webGIS学习【一】
  • 原文地址:https://blog.csdn.net/Jinpeijie217/article/details/127640047