当前位置:   article > 正文

dcm转numpy数组_dcm2arry

dcm2arry
  1. # 将很多dcm切片存成npz数组
  2. from glob import glob
  3. import os
  4. import pydicom
  5. import matplotlib.pyplot as plt
  6. import numpy as np
  7. def extract_voxel_data(files_path_list):
  8. arrays = []
  9. for file_path in files_path_list:
  10. ds = pydicom.dcmread(file_path)
  11. array = ds.pixel_array
  12. arrays.append(array)
  13. # print(array.shape)
  14. # plt.title(os.path.basename(file_path))
  15. # plt.imshow(array,cmap='gray')
  16. # plt.show()
  17. arrayd = np.dstack(arrays)
  18. # 512,512,72 -> 72,512,512
  19. arrayd = np.transpose(arrayd,[2,0,1])
  20. print(arrayd.shape)
  21. return arrayd
  22. if __name__ == '__main__':
  23. workdir = "D:/lung_blood/242/CIUYM10O/PJ1YNRAG"
  24. file_names = os.listdir(workdir) # 很多DCM文件序列名
  25. # 删掉VERSION
  26. for file_name in file_names:
  27. if file_name == 'VERSION':
  28. file_names.remove('VERSION')
  29. file_names.sort(key=lambda x: int(x[-7:])) # 按文件顺序排序
  30. files_path_list = [os.path.join(workdir,file_name) for file_name in file_names]
  31. voxel_ndarry = extract_voxel_data(files_path_list)
  32. aimg = voxel_ndarry[0]
  33. print(aimg.shape)
  34. plt.imshow(aimg,cmap='gray')
  35. plt.show()

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/106427
推荐阅读
相关标签
  

闽ICP备14008679号