简体   繁体   English

如何手动快速地对数组元素/像素进行计算和检查?

[英]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,红色椭圆是错误标记的物体,绿色线条代表视角Occupancy 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.

相关问题 如何使这种相等数组快速(numpy)? - How to make this kind of equality array fast (in numpy)? 算法-如何快速确保每个组中的元素都是唯一的 - Algorithm - how to fast make sure elements unique from each group 如何快速计算 3D 张量(或数组)中不同元素的数量? - How to count the number of different elements in a 3D tensor (or array) fast? Numpy:考虑项目的邻居及其在阵列中的位置的快速计算 - Numpy: fast calculations considering items' neighbors and their position inside the array 如何对数据集进行计算? - How to make calculations on a data set? 如何为 dataframe 制作一个 function 来检查大元组是否包含小元组的所有元素? - How to make a function for dataframe that checks if big tuple contains all the elements of the small one? 如何使用一个函数检查一对加在一起的元素是否等于另一个整数并使它在线性时间内运行? - How to take a function that checks if a pair of elements added together equal another integer and make it run in linear time? 如何引用数组进行进一步的计算? - how to reference array for further calculations? 如何编写检查文件中的元素是否重复的 function? - How to write a function that checks if elements in a file are repeated? 如何在Tensorflow中将计算应用于多维矩阵的元素? - How to apply calculations to elements of a multidimensional matrix in Tensorflow?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM