【问题标题】:NumPy Handling Mask ShapeNumPy 处理蒙版形状
【发布时间】:2021-05-14 09:18:16
【问题描述】:

我有一个 xyz 点云 (pcd) 作为大小 (N, 3) 的矩阵和一个图像 (img) 作为大小 (H, W)。我想要图像投影的点,所以我有以下面具的重聚:

true_where_x_on_img = (0

true_where_y_on_img = (0

true_where_point_on_img = true_where_x_on_img & true_where_y_on_img

此掩码的大小为 N 并按预期工作(使用 pcd[true_where_point_on_img])。现在我使用另一个掩码过滤这些值,该掩码告诉我图像中的像素是否为背景:

true_where_not_background = mask[pcd[:, 1], pcd[:, 0]] != 0

true_where_not_background 的大小为 M,因为掩码是 H x W。 最后,我想将这些结果投影到更大矩阵的第 4 列,aug_pcd,大​​小为 N x 4。这个矩阵用零初始化,pcd 被复制到它的前 3 列。我现在想将蒙版图像 (img[true_where_not_background]) 放入第 4 列。类似 aug_pcd[true_where_not_background, 3:] = img[true_where_not_background]。问题是 true_where_not_background 的大小为 M,而 aug_pcd 的行大小为 N 并且已经是完整的切片。切片切片会生成一个我无法为其赋值的副本。如何混合 true_where_point_on_imgtrue_where_not_background 以便我可以拥有 N 大小的蒙版?

【问题讨论】:

标签: python arrays numpy


【解决方案1】:

设置

可重复性的初始化变量

pcd = np.linspace((-9, -9, -9), (10, 10, 10), 20)  # (20, 3)
img = np.random.rand(4, 8)  # (4, 8)

蒙版获取图像中的点

# (20,)
true_where_x_on_img = (0 < pcd[:, 0]) & (pcd[:, 0] < img.shape[1])
true_where_y_on_img = (0 < pcd[:, 1]) & (pcd[:, 1] < img.shape[0])
true_where_point_on_img = true_where_x_on_img & true_where_y_on_img

图像中的点

masked_pcd = pcd[true_where_point_on_img].astype(int)  # (4, 3)

告诉第一张图像中相关像素的图像掩码

# (4, 8)
img_mask = np.full(shape=(4, 8), fill_value=False, dtype=np.bool)
img_mask[1:3, 1:4] = True 

掩码对应于相关像素的点

# (3,)
true_where_not_zero = img_mask[masked_pcd[:, 1], masked_pcd[:, 0]] != 0

问题

问题是无法进行以下操作来收集 pcd 中落入 true_where_not_zero 定义的区域的点:

pcd[true_where_not_zero]
Out: Mismatched Index Error

解决方案

解决方案是将两个掩码合并为一个,如下所示:

true_where_inside_img_and_not_zero = np.copy(true_where_point_on_img) true_where_inside_img_and_not_zero[true_where_inside_img_and_not_zero.nonzero()] = true_where_not_zero # (20,)

而 pcd[true_where_inside_img_and_not_zero] 会起作用,因为 true_where_inside_img_and_not_zero 具有与 pcd

相同的行形状

【讨论】:

    猜你喜欢
    • 2015-12-05
    • 1970-01-01
    • 1970-01-01
    • 2019-05-23
    • 2014-05-14
    • 2017-09-12
    • 2019-05-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多