3d医学图像nii.gz转换为png(单次处理单视窗) 您所在的位置:网站首页 怎么转换png 3d医学图像nii.gz转换为png(单次处理单视窗)

3d医学图像nii.gz转换为png(单次处理单视窗)

2024-06-26 19:08| 来源: 网络整理| 查看: 265

默认拥有后缀名为nii.gz的医学格式数据 使用ITK-SNAP查看数据为3个视窗,默认选取矢状面Sagittal即右上角视窗 在这里插入图片描述

在编译器中复制该程序,在环境中安装相应的包 在最下面的main函数中修改为自己的路径 保存的图像为默认名字+切片号,可根据自己数据的名字长度做调整 在文件中添加了float64->unit8的转换,可自行删除

import scipy, shutil, os import sys, getopt import imageio from tqdm import tqdm import nibabel as nib import numpy as np def niito2D(filepath, outputpath): inputfiles = os.listdir(filepath) # 遍历文件夹数据 outputfile = outputpath print('Input file is : ', inputfiles) print('Output folder is: ', outputfile) file_count = 0 # 文件计数器 for inputfile in inputfiles: image = nib.load(filepath + inputfile) image_array = image.get_fdata() # 数据读取 # print(len(image_array.shape)) file_count = file_count + 1 (x, y , z) = image_array.shape # 获得数据shape信息:(长,宽,维度-切片数量) # 不同3D体数据有用的切片数量不同,自行查看,自行设定起止数量 total_slices = 105 # 总切片数 slice_counter = 25 # 从第几个切片开始 loop = tqdm(range(slice_counter, slice_counter + total_slices)) # 单纯为了花哨的写法 for current_slice in loop: if (slice_counter % 1) == 0: data = image_array[current_slice, :, : ].astype(np.uint8) # 保存该切片,可以选择不同方向。当前保存Sag方向,可自行调整顺序 if (slice_counter % 1) == 0: # 切片命名 image_name = inputfile[:-6] + "{:0>3}".format(str(current_slice + 1)) + ".png" # 保存 imageio.imwrite(image_name, data) # 移动到输出文件夹 src = image_name shutil.move(src, outputfile) slice_counter += 1 loop.set_description(f'文件数:[{file_count}/{len(inputfiles)}]') print('Finished converting images') if __name__ == '__main__': input_path = 'H:\Project/nnUNet-1-master\DATASET/nnUNet_raw/nnUNet_raw_data\Task666_Knee\labelsTr/' output_path = 'H:\Project/nnUNet-1-master\DATASET/nnUNet_raw/nnUNet_raw_data\Task666_Knee/1/' niito2D(input_path, output_path)

在这里插入图片描述

输出结果: 在这里插入图片描述



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有