赞
踩
- # 将很多dcm切片存成npz数组
-
- from glob import glob
- import os
- import pydicom
- import matplotlib.pyplot as plt
- import numpy as np
-
-
- def extract_voxel_data(files_path_list):
- arrays = []
- for file_path in files_path_list:
- ds = pydicom.dcmread(file_path)
- array = ds.pixel_array
- arrays.append(array)
- # print(array.shape)
- # plt.title(os.path.basename(file_path))
- # plt.imshow(array,cmap='gray')
- # plt.show()
-
- arrayd = np.dstack(arrays)
- # 512,512,72 -> 72,512,512
- arrayd = np.transpose(arrayd,[2,0,1])
- print(arrayd.shape)
-
- return arrayd
-
-
- if __name__ == '__main__':
- workdir = "D:/lung_blood/242/CIUYM10O/PJ1YNRAG"
- file_names = os.listdir(workdir) # 很多DCM文件序列名
-
- # 删掉VERSION
- for file_name in file_names:
- if file_name == 'VERSION':
- file_names.remove('VERSION')
-
- file_names.sort(key=lambda x: int(x[-7:])) # 按文件顺序排序
- files_path_list = [os.path.join(workdir,file_name) for file_name in file_names]
-
- voxel_ndarry = extract_voxel_data(files_path_list)
- aimg = voxel_ndarry[0]
- print(aimg.shape)
- plt.imshow(aimg,cmap='gray')
- plt.show()
-
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。