[英]NumPy Handling Mask Shape
我有一个 xyz 点云 ( pcd ) 作为大小 (N, 3) 的矩阵和一个图像 ( img ) 作为大小 (H, W) 的矩阵。 我想要图像投影的点,所以我有以下面具的重聚:
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
此掩码的大小为 N 并按预期工作(使用 pcd[true_where_point_on_img])。 现在我使用另一个掩码过滤这些值,该掩码告诉我图像中的像素是否为背景:
true_where_not_background = 掩码[pcd[:, 1], pcd[:, 0]] != 0
true_where_not_background的大小为 M,因为 mask 是 H x W。最后,我想将这些结果投影到更大的矩阵aug_pcd的第 4 列,大小为 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_img和true_where_not_background以便我可以拥有大小为 N 的蒙版?
可重复性的初始变量
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相同的行形状
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.