我的数据存放大致如下所示
E:/105casesMask_Seg是总的数据文件夹,Cxxx文件夹存放的是每个病人mhd文件,文件名字就相当于后面的patient_name
代码语言:javascript复制#导入需要的包
import os
from PIL import Image
import SimpleITK as sitk
import matplotlib.pyplot as plt
from natsort import natsorted
from tqdm import tqdm
#获取文件内的的子文件夹名称
def get_file_name(filename):
for root, dirs, files in os.walk(filename):
array = dirs
if array:
return array
#创建空目录
def make_dir(save_path):
if not os.path.isdir(save_path):
os.makedirs(save_path)
#将一个病人的所有图片整合在一张图打印出来
def plot_ct_scan(scan, num_column=4, jump=1):
num_slices = len(scan)
num_row = (num_slices//jump num_column - 1) // num_column
f, plots = plt.subplots(num_row, num_column, figsize=(num_column*5, num_row*5))
for i in range(0, num_row*num_column):
plot = plots[i % num_column] if num_row == 1 else plots[i // num_column, i % num_column]
plot.axis('off')
if i < num_slices//jump:
plot.imshow(scan[i*jump], cmap=plt.cm.bone)
#读取图片并保存
def save_img(data_path,save_path):
root = os.path.join(os.getcwd(),data_path)
paths = os.listdir(root)
print(paths)
for path in paths:
if path.find('mhd')>=0:
data =sitk.ReadImage(os.path.join(root,path)) #读取mhd文件
#print(data)
spacing = data.GetSpacing()
scan = sitk.GetArrayFromImage(data)
print('scan.shape',scan.shape) #图像大小
print('spacing: ', spacing) #间隔
print('# slice: ', len(scan)) #切片数量
#plot_ct_scan(scan)
make_dir(save_path)
for i in range(scan.shape[0]):
im = Image.fromarray(scan[i,:,:]) #这里就是提取的图片数据
im.save(save_path '/' str(i) '.png') #保存图片
if __name__ == '__main__':
data_dir = 'E:/105casesMask_Seg/' #mhd数据存放位置
result_dir = 'E:/labels/' #读取的图片存放位置
patients_name = get_file_name( data_dir )
patients_name = natsorted(patients_name )
#print(data_dir)
for patient_name in tqdm(patients_name): #分别保存每个病人数据
data_path = data_dir patient_name
save_path = result_dir patient_name
make_dir(save_path)
save_img(data_path,save_path)