【问题标题】:Extending a 3D array扩展 3D 阵列
【发布时间】:2019-06-28 07:12:12
【问题描述】:

我有一个形状为 200、130、131 (x,y,z) 的 3D 数组。这基本上是掩码文件。可以说它被称为“maskCoords”。我想将此扩展到我的原始数据形状 512*512*485 (x,y,z)。但保持掩码不变并将其余索引填充为零。

所以,我创建了一个空的 3D 数组,例如:

 mask3d = np.zeros_like(MainData3d)

但是现在我无法理解如何用我的掩码文件中保存的值填充这个 mask3d 数组。我试着这样做

mask3d[maskCoords]=1

但这不起作用。当我重叠 Maindata3d 和 mask3d 时,蒙版区域不可见。 任何帮助或想法将不胜感激。

【问题讨论】:

  • 你的掩码比原始数据小,两边的比例不同。请解释一下“扩展”是什么意思。是重新缩放还是其他什么?
  • 通过扩展我的意思是重塑掩码文件以匹配我的原始数据的形状。这样掩码文件在原始数据上的叠加就变得更容易了。抱歉,如果我将关键字混淆为新手。我不知道你说的重新缩放是什么意思。
  • @Valentino,我应该更澄清一下。掩码是一个 nfiti 文件。我的原始数据是核磁共振图像。 nifti 文件有一些肿瘤的边界框坐标..
  • 对不起,我还是不明白。如果你称它为“蒙版”,我希望蒙版的每个像素都可以是不透明的或透明的。所以你有一个由透明像素组成的蒙版图像。您只想在图像蒙版周围添加不​​透明像素以适应更大的形状,还是想拉伸蒙版图像以适应更大的形状?前者比后者容易得多。
  • 也许你正在寻找类似scipy.ndimage.zoom(mask3d, (512/200, 512/130, 485/131))的东西?

标签: python numpy mask masking


【解决方案1】:

我假设您的 maskCoords 是您的 3d 数组中的 3d 坐标以及我的回答,因为就像 cmets 在您的问题中所说的那样,很难准确说出您的意思。

这是方法。

mask3d = np.zeros((512, 512, 485))

# Create random coordinates
maskCoords = (np.random.random((200, 130, 131)) * 400).astype('int')

# Convert the coordinates to be converted by numpy.ravel_multi_index
maskCoords2 = np.hstack((maskCoords[:, :, 0].flatten()[:, None], maskCoords[:, :, 1].flatten()[:, None], maskCoords[:, :, 2].flatten()[:, None])).T

# Convert the coordinates into indices so we can access the locations in the original array
inds = np.ravel_multi_index(maskCoords2, [512, 512, 485])

# Set the array values at the locations of the indices to 1
mask3d.flat[inds] = 1

也许不是你想要的,但我正在尝试一些东西。

【讨论】:

  • 非常感谢您的方法。我认为你的想法引起了我想要做的事情的共鸣。我会尝试更新发生的事情。另外我认为我应该在提问之前投入一些时间。似乎人们不理解。
  • 扩展此任务的最佳方法似乎是插值+重采样。与插值一样,有机会在掩码图像中添加额外的值。我认为最近邻插值是安全的。我所做的是使用 Simple ITK,ResampleImageFilter() 来做到这一点。虽然,我不确定我是否做得正确。在不同的情况下可能不需要插值。
猜你喜欢
  • 1970-01-01
  • 2016-05-08
  • 1970-01-01
  • 2019-10-21
  • 1970-01-01
  • 2012-01-16
  • 1970-01-01
  • 2014-07-19
  • 2016-06-03
相关资源
最近更新 更多