本笔记来源于B站Up主: 有Li 的影像组学系列教学视频
本节(22)主要讲解: 从atlas抠出想要的ROI做成mask
0. Task: 将某个特定的脑功能区(这里以10为例)设置为mask
1. 导入包
# pip install nibabel ## for the first time
import numpy as np
import nibabel as nib
import os
from collections import Counter
2. 读入文件
roiIndex = 10
basePath = 'C:/Users/RONG/Desktop/roi2mask/'
atlasName = "aal.nii.gz"
atlasFile = os.path.join(basePath, atlasName)
atlas_nii = nib.load(atlasFile)
atlas_arr = atlas_nii.get_fdata()
3. 设置
mask_arr = atlas_arr.copy()
mask_arr[atlas_arr != roiIndex] = 0
mask_arr[atlas_arr == roiIndex] = 1
mask_affine = atlas_nii.affine.copy()
mask_hrd = atlas_nii.header.copy()
mask_hrd["cal_max"] = 1
# print(mask_hrd)
mask_nii = nib.Nifti1Image(mask_arr, mask_affine, mask_hrd)
nib.save(mask_nii,os.path.join(basePath, "roi_" + str(roiIndex) + ".nii.gz"))
# print(Counter(mask_arr.flatten()))
4. 查看结果(如图)