如何在win11上安装freesurfer并获取头模型

一、安装ubuntu子系统

windows自带的ubuntu子系统挺好用的
推荐教程
https://blog.youkuaiyun.com/Lastvil/article/details/130687053

二、安装freesurfer

其实说是安装,只需要将文件下载并解压即可。
推荐教程
https://blog.youkuaiyun.com/zuzhiang/article/details/107562854

wget https://surfer.nmr.mgh.harvard.edu/pub/dist/freesurfer/7.1.0/freesurfer-linux-centos6_x86_64-7.1.0.tar.gz #下载安装包
tar -zxv -f freesurfer-linux-centos6_x86_64-7.1.0.tar.gz #解压缩
# 注册,得license.txt,并将license.txt拷贝到./freesurfer目录下
sudo chmod -R 777 ./freesurfer #改变freesurfer的权限

打开~/.bashrc

vi ~/.bashrc

在~/.bashrc文件中添加以下三行

# 在~/.bashrc文件中添加以下两行
export FREESURFER_HOME=/home/fxw/soft/freesurfer #freesurfer解压路径
source $FREESURFER_HOME/SetUpFreeSurfer.sh
export SUBJECTS_DIR=/mnt/d/code/--1-master/MRIT/data #处理结果保存路径(/mnt/d表示ubuntu子系统下win中的D盘)(推荐保存在win下便于之后的处理)

关闭~/.bashrc

更新~/.bashrc

source ~/.bashrc

安装tcsh

sudo apt-get install tcsh #安装tcsh,类似与bash,不安装运行的时候会报错

注意每次进入终端使用freesurfer时需要输入

export FREESURFER_HOME=/home/fxw/soft/freesurfer
source $FREESURFER_HOME/SetUpFreeSurfer.sh

FREESURFER_HOME路径根据freesurfer的文件夹所在位置自己修改

修改bashrc需要用到vim
vim使用手册
推荐教程
https://www.runoob.com/linux/linux-vim.html

三、freesurfer分割nii.gz

freesurfer安装完成后,分割只需

recon-all -i 12_t1_mprage_sag_p2.nii.gz -s fxwBrain -all

等待个几个小时后,在上面设置的输出路径/mnt/d/code/–1-master/MRIT/data/fxwBrain中会有如下文件夹

label mri scripts stats surf tmp touch trash
在/mnt/d/code/–1-master/MRIT/data/fxwBrain/surf中有如下文件
在这里插入图片描述
我们需要的【lh.seghead】文件此时还没有出现
需要使用 mkheadsurf

mkheadsurf -s fxwBrain

完成后得到 【lh.seghead】

在FreeSurfer中,mkheadsurf是一个用于创建头表面(head surface)的命令,而-s选项用于指定要处理的源文件。在你的问题中,-s fxwBrain指定了源文件为fxwBrain

头表面是FreeSurfer中用于描述大脑外形的表面模型。这个过程涉及将MRI数据进行分割、重建,并最终生成表示大脑表面的三维模型。mkheadsurf命令的目的是生成一个额外的头表面,通常用于配准(registration)和分析过程中。

根据你的问题,fxwBrain可能是你的数据的标识符或文件名。这意味着mkheadsurf命令将被应用于名为fxwBrain的源文件,以生成头表面。

请注意,具体的命令参数和功能可能会因FreeSurfer版本而异,因此最好查阅相关版本的文档以获取准确的信息。

另外如果使用的是DICOM文件可以使用下面的转换程序将DICOM转换为nii.gz

import dicom2nifti
# 设置DICOM文件路径
dicom_file = 'C:\\files\\MEG\\data\\dicom脱敏\\t1_mprage_sag_p2'
dicom2nifti.convert_directory(dicom_file,'./')

四、从分割文件中获得头模型.obj

使用下面的python程序自动从lh.seghead获得头表面.obj

import os
import meshio
import numpy as np
import matplotlib
import mne

matplotlib.use('Qt5Agg')
#  路径拼接
data_path = R"E:\MRI"
mri_name = 'mayujie'
subjects_dir = os.path.join(data_path, mri_name)
fname_lh = os.path.join(subjects_dir, 'surf', 'lh.seghead')
# mne读取
rr_mm_lh, tris_lh = mne.read_surface(fname_lh)
print(f'rr_mm.shape == {rr_mm_lh.shape}')
print(f'tris.shape == {tris_lh.shape}')
print(f'rr_mm.max() = {rr_mm_lh.max()}')  # just to show that we are in mm
# 可视化
renderer = mne.viz.backends.renderer.create_3d_figure(
    size=(600, 600), bgcolor='w', scene=False)
gray = (0.5, 0.5, 0.5)
renderer.mesh(*rr_mm_lh.T, triangles=tris_lh, color=gray)
# renderer.mesh(*rr_mm_rh.T, triangles=tris_rh, color=gray)
view_kwargs = dict(elevation=90, azimuth=0)
mne.viz.set_3d_view(
    figure=renderer.figure, distance=350, focalpoint=(0., 0., 40.),
    **view_kwargs)
renderer.show()
# 写入ply 或者 obj文件
mesh_path = "mesh\\" + mri_name + '_mri.obj'  # 可以选择保存ply 或者 obj文件
mesh = meshio.Mesh(points=rr_mm_lh, cells=[("triangle", tris_lh)])
mesh.points = mesh.points.astype(np.float32)
mesh.write(mesh_path)

使用下面的python程序获得大脑左右皮质.obj

import os
import meshio
import numpy as np
import matplotlib
import mne

matplotlib.use('Qt5Agg')
data_path = R"E:\MRI"
mri_name = 'mayujie'
subjects_dir = os.path.join(data_path, mri_name)
# 左半部分皮质
fname_lh = os.path.join(subjects_dir, 'surf', 'lh.pial')
rr_mm_lh, tris_lh = mne.read_surface(fname_lh)
print(f'rr_mm.shape == {rr_mm_lh.shape}')
print(f'tris.shape == {tris_lh.shape}')
print(f'rr_mm.max() = {rr_mm_lh.max()}')  # just to show that we are in mm
# 右半部分皮质
fname_rh = os.path.join(subjects_dir, 'surf', 'rh.pial')
rr_mm_rh, tris_rh = mne.read_surface(fname_rh)
print(f'rr_mm.shape == {rr_mm_rh.shape}')
print(f'tris.shape == {tris_rh.shape}')
print(f'rr_mm.max() = {rr_mm_rh.max()}')  # just to show that we are in mm
# 组合左右两部分
rr_mm = np.concatenate((rr_mm_lh, rr_mm_rh))
tris = np.concatenate((tris_lh, tris_rh + rr_mm_lh.shape[0]))
# 可视化
renderer = mne.viz.backends.renderer.create_3d_figure(
    size=(600, 600), bgcolor='w', scene=False)
gray = (0.5, 0.5, 0.5)
renderer.mesh(*rr_mm_lh.T, triangles=tris_lh, color=gray)
# renderer.mesh(*rr_mm_rh.T, triangles=tris_rh, color=gray)
view_kwargs = dict(elevation=90, azimuth=0)
mne.viz.set_3d_view(
    figure=renderer.figure, distance=350, focalpoint=(0., 0., 40.),
    **view_kwargs)
renderer.show()
# 写入ply文件
mesh_path = "mesh\\" + mri_name + '_mri.obj'  # 可以选择保存ply obj文件
mesh = meshio.Mesh(points=rr_mm_lh, cells=[("triangle", tris_lh)])
mesh.points = mesh.points.astype(np.float32)
mesh.write(mesh_path)

至此完成!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值