本文主要是介绍dcm转numpy,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
from glob import glob
import os
import pydicom
import matplotlib.pyplot as plt
import numpy as npdef extract_voxel_data(files_path_list): # 将很多dcm切片存成npz数组arrays = []for file_path in files_path_list:ds = pydicom.dcmread(file_path)array = ds.pixel_arrayarrays.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,512arrayd = np.transpose(arrayd,[2,0,1])print(arrayd.shape)return arraydif __name__ == '__main__':workdir = "D:\lung_blood_delInfo/243\HKMTEVPH/3EIKEE1O"file_names = os.listdir(workdir) # 很多DCM文件序列名# 删掉VERSIONfor 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)# for i in range(5):aimg = voxel_ndarry[0]print(aimg.shape)plt.axis('off')plt.imshow(aimg,cmap='gray')plt.savefig("a.jpg",bbox_inches='tight',pad_inches=0)
这篇关于dcm转numpy的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!