R - 如何在图像的时间序列中找到蒙版内的图像元素?

R - how to find image elements inside a mask in a time series of images?

我的 R 脚本加载了一个 4 维数据集,它是 3D 医学图像的时间序列。我使用时间序列创建一个掩码,用于排除每个时间点的值为 0 的体素(3 维像素):

voxels[is.na(voxels)]=0;             # just get rid of unusable data
mask=rowSums(voxels,dims=3);         # 3D image that is the sum over time
mask=(mask!=0);                      # make binary

所以要在掩码内的索引处找到每个时间点的值,我想做的是:

indices=which(mask!=0);              # find the positions of nonzeroes in the mask
voxelsfound=voxels[indices];         # find the values in the images at those positions

但这给出了

 > length(indices)
 [1] 20483
 > length(voxelsfound)
 [1] 20483

所以只有第一个时间点的结果。是否有类似的方法来制定它,以便它也 returns 其他时间点(我的想法是 voxelsfound=voxels[indices,] 但那不起作用),或者它是否只能使用 for 循环或类似的?

在 Python 中我会做这样的事情(使用 m 表示 maskv 表示 voxels),使用 nonzero 用于立即访问索引的函数:

m = m.reshape(np.prod(m.shape));    
v = v.reshape(np.prod(m.shape),v.shape[3]);
v = v[:,np.nonzero(m)].squeeze(); 

我相信你要找的是:

mask <- which(rowSums(voxels, dims=3) != 0, arr.ind=T)
apply(voxels, 4, `[`, mask)