【问题标题】:3d coordinates x,y,z to 3d numpy array3d 坐标 x,y,z 到 3d numpy 数组
【发布时间】:2019-03-12 12:10:18
【问题描述】:

我有一个椭圆体的 3d 蒙版。我已经使用np.argwhere 提取了掩码的坐标。坐标可以指定为 x、y、z,如示例代码中所示。我的问题是如何从坐标 x、y、z 取回我的面具(以 3d numpy 或相同形状的布尔数组的形式)?

import numpy as np
import scipy
import skimage
from skimage import draw

mask = skimage.draw.ellipsoid(10,12,18)
print mask.shape 

coord = np.argwhere(mask)

x = coord[:,0]
y = coord[:,1]
z = coord[:,2]

上面的代码给了我形状 (23, 27, 39) 的布尔掩码,现在我想使用 x、y、z 坐标构造完全相同形状的相同掩码。怎么办?

我想稍微修改一下上面的问题。现在,如果我使用四元数旋转我的坐标,这将给我一组新的坐标,然后用新的坐标 x1,y1,z1 我想将形状 (23,27,39) 的布尔掩码构造为原始掩码的布尔掩码?怎么办?

import quaternion
angle1 = 90
rotation = np.exp(quaternion.quaternion(0,0, 1) * angle1*(np.pi/180) / 2)
coord_rotd = quaternion.rotate_vectors(rotation, coord)
x1 = coord_rotd[:,0]
y1 = coord_rotd[:,1]
z1 = coord_rotd[:,2]

【问题讨论】:

  • 请去掉字体的粗体 :-)

标签: python numpy mask quaternions scikit-image


【解决方案1】:

您可以直接使用 x、y 和 z 来重建您的蒙版。首先,使用与您的蒙版形状相同的新数组。我用零预先填充了所有内容(即False)。接下来,将x、y和z定义的每个坐标设置为True

new_mask = np.zeros_like(mask)
new_mask[x,y,z] = True

# Check if mask and new_mask is the same
np.allclose(mask, new_mask)
# True

如果你问,如果你可以知道 x、y 和 z 来重建你的面具,这是不可能的。因为您丢失了填充的信息。想象一下你的椭球在一个巨大的立方体的一角。你怎么知道(只知道椭圆体的样子)立方体有多大?

关于你的第二个问题:

你必须修正你的坐标,因为它们可能不在你的风景中。所以我定义了一个函数来处理这个问题:

def fixCoordinates(coord, shape):
    # move to the positive edge
    #  remove negative indices
    #  you can also add now +1 to 
    #  have a margin around your ellipse
    coord -= coord.min(0)

    # trim coordinates outside of scene
    for i, s in enumerate(shape):
        coord[coord[:,i] >= s] = s-1

    # Return coordinates and change dtype
    return coord.astype(np.int)

如果你稍微修改你的代码,你可以使用和以前一样的策略:

# your code
import quaternion
angle1 = 90
rotation = np.exp(quaternion.quaternion(0,0, 1) * angle1*(np.pi/180) / 2)
coord_rotd = quaternion.rotate_vectors(rotation, coord_rotd)

# Create new mask
new_mask2 = np.zeros_like(new_mask)
# Fix coordinates
coord_rotd = fixCoordinates(coord_rotd, mask.shape)

x1 = coord_rotd[:,0]
y1 = coord_rotd[:,1]
z1 = coord_rotd[:,2]

# create new mask, similar as before
new_mask2[x1, y1, z1] = True

鉴于您的示例旋转,您现在可以并排绘制两个蒙版(具有相同形状):

【讨论】:

  • 感谢您的回答,该方法仅在知道 x,y,z 时才有效。但是我想旋转坐标,这将为我提供一组新的坐标 x1,y1,z1 并根据我在问题中编辑的内容构造 new_mask 。正如你所解释的,这给了我一个错误。
  • @Astro,我回答了你的第二个问题。可能代码还不完善,因为可以看到角落里有一些伪影,所以也许你可以稍微调整一下。
  • 与我的原始数据集成时可能会出现一些问题。例如,椭圆体的形状看起来不像预期的完整椭圆体(在 paraview 中视觉上看)。但我认为上述问题得到了很好的回答。我可以尝试修改它。再次感谢。
【解决方案2】:

如果你知道旧面具的形状,试试这个:

new_mask = np.full(old_mask_shape, True) # Fill new_mask with True everywhere
new_mask[x,y,z] = False                  # Set False for the ellipsoid part alone

注意:

  1. old_mask_shape 应与您打算在其上应用蒙版的图像的形状相同。
  2. 如果你想要一个True 掩码而不是False 一个(如果你想要椭球部分是True 和其他任何地方False)只需在上述两个中交换TrueFalse代码行。

【讨论】:

  • 这确实适用于 mask 的 x,y,z 坐标。实际上,我想要 x1,y1,z1 ,它们是将 x,y,z 旋转 90 度时的坐标,正如我在问题中编辑的那样。因此该方法仅适用于 x,y,z 而不适用于 x1,y1,z1。
  • 嗯,您已在问题中添加了第二部分,但第二部分的要求或问题尚不清楚。你有什么错误吗?您是否尝试在旋转之后 绘制椭圆体(我不确定这是否可能,如果可能,是否与您面临的实际问题/错误相关)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-05-30
  • 1970-01-01
  • 2016-09-07
  • 2022-12-10
  • 1970-01-01
  • 2016-02-10
  • 2021-06-28
相关资源
最近更新 更多