在预定义的子空间中创建具有固定大小的 3D 斑点

问题描述

我的问题类似于 this 帖子(可能还有相关的 to this one)。

我想在预定义的子空间中创建随机的 3D 斑点。具体来说,我的子空间由一个二进制 3D 蒙版图像定义,该图像“存在”于更大的 3D 立方体中。这个蒙版图像再次定义了斑点应该“存在”的空间。

应该满足一些限制条件:

1.) 应该可以定义 blob 的数量

2.) 应该可以将每个 blob 的大小定义为多个体素。换句话说,每个 blob 都应该占据我的子空间的一部分。

3.) 不应该允许 Blob 接触或重叠(这将违反第一条规则,因为它减少了 Blob 的定义数量

4.) 每个 blob 应该是一组连接的体素,即每个体素必须直接连接到另一个体素。

我想出了代码获取一个 mask_coords 数组,该数组包含定义子空间的所有坐标。现在我需要一个算法,将这些坐标中的一些分配给满足我的约束的不同 blob:

import numpy as np
from nilearn import masking
from nilearn.image.resampling import coord_transform
from nilearn.plotting import plot_roi
from nilearn.image import new_img_like
from nilearn.datasets import fetch_icbm152_brain_gm_mask

# get a grey matter mask image from nilearn. This is a binary 3D image that
# defines our subspace in our bigger 3D cube. 
mask_img = fetch_icbm152_brain_gm_mask()

## Get the x,y,z coordinates of our mask image. ###############################

# get mask data array and affine
mask_data,affine = masking._load_mask_img(mask_img)

# get data indices for all '1' voxels inside mask data array
mask_data_indices = np.asarray(np.nonzero(mask_data)).T.tolist()

# return coordinates for those '1' voxels. This gives us all coordinates that
# correspond to our subspace
mask_coords = np.asarray(list(zip(*mask_data_indices)))

mask_coords = coord_transform(mask_coords[0],mask_coords[1],mask_coords[2],affine)

mask_coords = np.asarray(mask_coords).T

这里还有一些代码,我们可以用它来返回一个 3D numpy 数组,我们可以用它来绘制我们的 3D 蒙版图像:

## We can convert the mask coordinates back to data array #####################

mask_indices = coord_transform(mask_coords[:,0],mask_coords[:,1],2],np.linalg.inv(affine))

mask_indices = np.asarray(mask_indices,dtype=int).T

empty_array = np.zeros(mask_data.shape)

for indices in mask_indices.tolist():
    empty_array[indices[0],indices[1],indices[2]] = True
    
mask_img_from_indices = new_img_like(mask_img,empty_array)

# show results
plot_roi(mask_img_from_indices,bg_img=None,draw_cross=False)

enter image description here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)