栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

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

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

默认拥有后缀名为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-masterDATASET/nnUNet_raw/nnUNet_raw_dataTask666_KneelabelsTr/'
    output_path = 'H:Project/nnUNet-1-masterDATASET/nnUNet_raw/nnUNet_raw_dataTask666_Knee/1/'
    niito2D(input_path, output_path)









输出结果:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/589964.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号