为什么用MRIcroGL
参考
https://blog.csdn.net/qq_35172874/article/details/129161111
步骤
下载地址
https://github.com/rordenlab/MRIcroGL12/releases
环境配置
在用户变量和系统变量中同时添加名为dcm2niix的环境变量,在path路径也添加该目录:
打开powershell或者cmd,输入dcm2niix,如果出现以下界面,则环境变量添加成功:
python批处理
获取不同被试文件所在路径,保存为txt
# 当前目录下所有文件夹下的文件名(不带后缀)写入对应txt文件(以文件夹命名)中
import os
# 如果文件夹不存在创建文件夹
def Makedir(path):
folder = os.path.exists(path)
if (not folder):
os.makedirs(path)
# 利用os.listdir()、os.walk()获取文件夹和文件名
def GetFileName(fileDir, outDir):
list_name = []
Makedir(outDir)
for dir in os.listdir(fileDir): # 获取当前目录下所有文件夹和文件(不带后缀)的名称
filePath = os.path.join(fileDir, dir) #sub层
for dir2 in os.listdir(filePath): #
filePath = os.path.join(filePath, dir2) #
for dir3 in os.listdir(filePath): #
filePath = os.path.join(filePath, dir3) # 根据自己的文件夹有几层就几次
if os.path.isdir(filePath) and not (filePath == outDir):
txt = outDir + "list.txt"
# 获取根目录路径、子目录路径,根目录和子目录下所有文件名
for root, subDir, files in os.walk(filePath):
for subfilepath in subDir:
f = open(txt, 'a') # 以追加方式打开文件
subfilepath = os.path.join(filePath, subfilepath) # 最后一层
f.write(subfilepath + '\n') # 此处增加了一个换行符,方便txt文件的查看,后面读取的时候需要去掉换行符
f.close()
def main():
print("procesiing……\n")
fileDir = r"G:xxxxxxxxxxxxxx\beforesub" # 输入文件夹路径
outDir = r"G:\xxxxxxxxxxxxxxxxxx\nii\\"
files = GetFileName(fileDir, outDir)
print("done!\n")
# 判断是否是程序主入口而已,如果是程序主入口,则代码块执行,否则代码块不执行
# 主要用于别人调用此代码时,不要进入该代码的入口
if __name__ == "__main__":
main()
然后,读取txt文件,进行批处理转换
import sys #导入sys模块
import os
#from PIL import Image #PIL是python的第三方图像处理库
#dcm2niix.exe -f "outputname" -i y -m y -p y -x y -z y -b n -o "E:\datasets\liver_ct_process_output" "E:\datasets\LiverCT\庄健忠\20180105000198"
# system command line
d2n = 'dcm2niix.exe -f '
para = ' -i y -l y -p y -x y -v y -z y -o '
output_path = r"G:\xxxxxx\nii"
###产生随机命名
import random
def Makedir(path):
folder = os.path.exists(path)
if (not folder):
os.makedirs(path)
def ranstr(num):
#dictionary
H = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
salt = ''
for i in range(num):
salt += random.choice(H)
return salt
# read txt
filepaths = [] #输入的dicom文件目录
for line in open(r"G:\xxxxxxxxxxx\nii\list.txt", "r"): # 设置文件对象并读取每一行文件
filepaths.append(line)
# deal with format transform
for filepath in filepaths:
filepath = filepath[:-1] #去掉行位的换行符
a = filepath.split('\\')
outputname = "FLAIR"
output_path2 = os.path.join(output_path, a[6])
Makedir(output_path2)
cmd = 'D: & cd D:\\yan\\soft\\MRIcroGL\\Resources & '+d2n + outputname + para +'\"'+ output_path2 +'\" ' + '\"'+ filepath +'\"' #其中的双引号不可少
print(cmd)
os.system('chcp 65001') #避免乱码
r = os.system(cmd)
print("Congratulation, Done!")
结果举例
待解决:两个nii.gz的含义;命令参数的含义