简体   繁体   English

NumPy 处理面罩形状

[英]NumPy Handling Mask Shape

I have an xyz point cloud ( pcd ) as a matrix of size (N, 3) and an image ( img ) as a matrix of size (H, W).我有一个 xyz 点云 ( pcd ) 作为大小 (N, 3) 的矩阵和一个图像 ( img ) 作为大小 (H, W) 的矩阵。 I want the points where the image is projecting so I have the reunion of the following masks:我想要图像投影的点,所以我有以下面具的重聚:

true_where_x_on_img = (0 < pcd[:, 0]) & ( pcd[:, 0] < img.shape[1]) 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_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 true_where_point_on_img = true_where_x_on_img & true_where_y_on_img

This mask is of size N and works as intended (using pcd[true_where_point_on_img]).此掩码的大小为 N 并按预期工作(使用 pcd[true_where_point_on_img])。 Now I filter these values using another mask that tells me whether the pixels in the image are background or not:现在我使用另一个掩码过滤这些值,该掩码告诉我图像中的像素是否为背景:

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

true_where_not_background is of size M because mask is H x W. Finally, I want to project these results into column 4 of a bigger matrix, aug_pcd , of size N x 4. This matrix was initialized with zeros and pcd was copied into the first 3 columns of it. true_where_not_background的大小为 M,因为 mask 是 H x W。最后,我想将这些结果投影到更大的矩阵aug_pcd的第 4 列,大小为 N x 4。这个矩阵用零初始化, pcd被复制到前 3它的列。 I now want to put the masked image (img[true_where_not_background]) into column 4. Something like aug_pcd[true_where_not_background, 3:] = img[true_where_not_background].我现在想将蒙版图像 (img[true_where_not_background]) 放入第 4 列。类似 aug_pcd[true_where_not_background, 3:] = img[true_where_not_background]。 The problem is true_where_not_background has size M, and aug_pcd has row size N and is already a full-fledge slice.问题是true_where_not_background的大小为 M,而aug_pcd的行大小为 N,并且已经是一个完整的切片。 Slicing the slice would make a copy to which I would not be able to assign values to.切片切片会生成一个我无法为其赋值的副本。 How can I blend true_where_point_on_img and true_where_not_background so I can have a mask of size N?如何混合true_where_point_on_imgtrue_where_not_background以便我可以拥有大小为 N 的蒙版?

SETUP设置

Init variables for reproducibility可重复性的初始变量

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

Mask to get points in the image蒙版获取图像中的点

# (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

Points in image图像中的点

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

The image mask that tells the relevant pixels in the first image告诉第一张图像中相关像素的图像掩码

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

Mask for points respective to relevant pixels对应于相关像素的点的掩码

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

PROBLEM问题

The problem was that the following operation cannot be done to collect the points in the pcd that fall into the region defined by true_where_not_zero :问题是无法执行以下操作来收集pcd中落入由true_where_not_zero定义的区域的点:

pcd[true_where_not_zero]
Out: Mismatched Index Error

SOLUTION解决方案

The solution was to merge the two masks into one as follows:解决方案是将两个掩码合并为一个,如下所示:

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,) 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,)

And pcd[true_where_inside_img_and_not_zero] would work because true_where_inside_img_and_not_zero has the same row shape as pcd并且 pcd[true_where_inside_img_and_not_zero] 会起作用,因为true_where_inside_img_and_not_zero具有与pcd相同的行形状

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM