[英]How to make calculations and checks on array elements/pixels manually and fast?
To be more clear, I produced a 2-D occupancy map, where obstacles marked with white pixels, from 3-D depth data using Python (NumPy) as given in the following figure.为了更清楚,我使用 Python (NumPy) 从 3-D 深度数据生成了 2-D occupancy map,其中障碍物用白色像素标记,如下图所示。
Occupancy Map入住Map
However, occurrence of the objects that are located behind the detected obstacles are also marked but I don't want them to be marked since an obstacle already exist at the front.然而,位于检测到的障碍物后面的物体的出现也会被标记,但我不希望它们被标记,因为前面已经存在障碍物。 The following figure shows the case.
下图显示了这种情况。
Occupancy Map, red ellipses are wrongly marked objects, green lines represent angle of vision占用Map,红色椭圆是错误标记的物体,绿色线条代表视角
If I simply iterate over the pixels and search for the first occurrence of an obstacle and clear the marks that are located behind it, it would be very slow for real time applications in Python. The iteration concept is given in the following.如果我简单地遍历像素并搜索第一次出现的障碍物并清除位于它后面的标记,对于 Python 中的实时应用程序来说会非常慢。迭代概念在下面给出。
Start from left, search for an obstacle from down to up从左边开始,从下往上寻找障碍物
Also, for some filtering/optimization purposes on depth data, I want to check all the neighbors (8-pixels) of a pixel but not know how to do it with NumPy or any other library.此外,对于深度数据的某些过滤/优化目的,我想检查一个像素的所有邻居(8 像素),但不知道如何使用 NumPy 或任何其他库进行检查。 Again, doing with for loops is cumbersome.
同样,使用 for 循环很麻烦。
Thanks in advance,提前致谢,
Yavuz Selim亚武兹塞利姆
'Manual iteration with loops' concept. “带循环的手动迭代”概念。 Fast calculations are expected but not achieved.
预计会进行快速计算,但并未实现。
I can't see without a for loop in some place, but you can use itertools.groupby
:在某些地方没有 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.