簡體   English   中英

NumPy 處理面罩形狀

[英]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_imgtrue_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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM