两个nii文件取交集

import numpy as np
import nibabel as nib

# 读取NIfTI文件
nii_file = 'D:/input1.nii'
output_file = 'D:/output.nii'
masknii_file = 'D:/input2.nii'

# 加载nifti图像并转换为numpy数组
img = nib.load(nii_file)
img_data = img.get_fdata()

mask_img = nib.load(masknii_file)
mask_data = mask_img.get_fdata()

# 取交集
intersection = np.logical_and(img_data, mask_data)

# 将交集生成新的nifti图像
intersection_img = nib.Nifti1Image(intersection.astype(np.uint8), img.affine, img.header)
nib.save(intersection_img, output_file)

posted on 2024-01-29 13:21  大海z16  阅读(6)  评论(0编辑  收藏  举报