[英]How to make calculations and checks on array elements/pixels manually and fast?
為了更清楚,我使用 Python (NumPy) 從 3-D 深度數據生成了 2-D occupancy map,其中障礙物用白色像素標記,如下圖所示。
入住Map
然而,位於檢測到的障礙物后面的物體的出現也會被標記,但我不希望它們被標記,因為前面已經存在障礙物。 下圖顯示了這種情況。
占用Map,紅色橢圓是錯誤標記的物體,綠色線條代表視角
如果我簡單地遍歷像素並搜索第一次出現的障礙物並清除位於它后面的標記,對於 Python 中的實時應用程序來說會非常慢。迭代概念在下面給出。
從左邊開始,從下往上尋找障礙物
此外,對於深度數據的某些過濾/優化目的,我想檢查一個像素的所有鄰居(8 像素),但不知道如何使用 NumPy 或任何其他庫進行檢查。 同樣,使用 for 循環很麻煩。
提前致謝,
亞武茲塞利姆
“帶循環的手動迭代”概念。 預計會進行快速計算,但並未實現。
在某些地方沒有 for 循環我看不到,但你可以使用itertools.groupby
:
from itertools import groupby
import numpy as np
# Generate data
np.random.seed(34)
data = np.random.choice([0, 1], p=[0.8, 0.2], size=(32, 32))
# Rotate the array because we want to search from "bottom"
# and we will use the fact that np.where sort indices on axis 0
# but axis 0 is the one on the "left"
result = data.copy()
result = np.rot90(result, k=-1)
# Get indices of obstacle
idxs_true = np.where(result == 1)
# Get only one pair for each value column
idxs = [next(grp) for _, grp in groupby(zip(*idxs_true), key=lambda x: x[0])]
idxs = tuple(zip(*idxs))
# Hihglight them
result[idxs] = 2
# Come back to orignal orientation
result = np.rot90(result, k=1)
# Plot
fig, (ax1, ax2) = plt.subplots(ncols=2)
ax1.imshow(data, cmap="gray")
ax2.imshow(result, cmap="gray")
plt.show()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.