3d坐标x,y,z到3d numpy数组

问题描述 投票:0回答:2

我有一个椭圆体的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
2个回答
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

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

old mask and new mask


0
投票

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

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
© www.soinside.com 2019 - 2024. All rights reserved.