【问题标题】:R - how to find image elements inside a mask in a time series of images?R - 如何在时间序列图像中找到掩码内的图像元素?
【发布时间】:2018-07-04 14:16:53
【问题描述】:

我的 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

所以只有第一个时间点的结果。有没有类似的方法来制定这个,以便它也返回其他时间点(我的想法是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(); 

【问题讨论】:

    标签: arrays r multidimensional-array indexing nifti


    【解决方案1】:

    我相信您正在寻找的是:

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

    【讨论】:

    • 太棒了——就是这样!它看起来不像应该工作的东西,一个没有关闭的左括号?
    • 好吧,注意括号周围的反引号——它们很重要,表示括号被引用(因此不受括号匹配的通常语法规则的约束,但是而是作为符号处理)。基本上,`[` 是用于下标的原语的名称,在某种意义上,x[1]`[`(x, 1) 是等价的(`[[` 也是如此)。
    • 换句话说,你也可以写apply(voxels, 4, function (voxel) voxel[mask])这样的东西。
    • 感谢您抽出宝贵时间回答。在您的示例中是 4(不是 3),因为索引从 0 开始?掩码变量本身是一个 3d 数组。
    • 再次感谢——仍然习惯了这个符号。我想我是在rowSums 命令中被dims=3 抛出的,假设这表明要求和的维度。只需阅读 rowSums (dims+1)colSums (1:dims) 中对 dims 的解释即可。现在一切都清楚了。
    猜你喜欢
    • 1970-01-01
    • 2021-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-13
    • 2017-04-06
    • 1970-01-01
    相关资源
    最近更新 更多