如何在二维 numpy 数组中搜索特定 XY 对的位置?

How to search for the position of specific XY pairs in a 2 dimensional numpy array?

我将图像存储为 3 个 Numpy 数组:

# Int arrays of coordinates
# Not continuous, some points are omitted
X_image = np.array([1,2,3,4,5,6,7,9])
Y_image = np.array([9,8,7,6,5,4,3,1])

# Float array of RGB values.
# Same index
rgb = np.array([
    [0.5543,0.2665,0.5589],
    [0.5544,0.1665,0.5589],
    [0.2241,0.6645,0.5249],
    [0.2242,0.6445,0.2239],
    [0.2877,0.6425,0.5829],
    [0.5543,0.3165,0.2839],
    [0.3224,0.4635,0.5879],
    [0.5534,0.6693,0.5889],
])

RGB 信息无法转换为 int。所以它必须保持浮动

我有另一个数组定义图像中某些像素区域的位置:

X_area = np.array([3,4,6])
Y_area = np.array([7,6,4])

我需要找到这些像素的 RGB 信息,使用前 4 个数组作为参考。

我的想法是在整幅图像中搜索这些区域点的索引,然后使用该索引找回RGB信息。

index = search_for_index_of_array_1_in_array_2((X_area,Y_area),(X_image,Y_image))

# index shall be [3,4,6]
rgb_area = rgb[index]

search_for_index_of_array_1_in_array_2可以用for循环来实现。我试过了,这太慢了。我居然有百万积分

我知道它可能比 Python 更适合 Julia 使用,因为我们处理具有性能需求的低级数据操作,但我不得不使用 Python.因此,我看到的唯一性能技巧是使用 NumPy 的矢量化解决方案。

我不习惯操纵 NumPy。我试过了 numpy.where.

index = np.where(X_area in X_image and Y_area in Y_image )
index

给出:

<ipython-input-18-0e434ab7a291>:1: DeprecationWarning: elementwise comparison failed; this will raise an error in the future.
  index = np.where(X_area in X_image and Y_area in Y_image )
(array([], dtype=int64),)

因为我们有 3 个兼容点,所以它应该是空的。

我也测试了,结果一样:

XY_image = np.vstack((X_image,Y_image))
XY_area = np.vstack((X_area,Y_area))
index = np.where(XY_area == XY_image)

甚至:

np.extract(XY_image == XY_area, XY_image)

如果我明白了,问题是数组的长度不同。但这就是我所拥有的。

您知道如何进行吗?

谢谢

编辑:这是一个有效的循环,但...速度不快:

indexes = []
for i in range(XY_area.shape[1]):
    XY_area_b =  np.broadcast_to(XY_area[:,i],(9,2)).transpose()
    where_in_image = np.where(XY_area_b == XY_image)
    index_in_image = where_in_image[1][1]
    indexes.append(index_in_image)
indexes

解决这个问题的经典方法一般是使用hashmap。但是,Numpy 不提供这样的数据结构。话虽如此,另一种(通常较慢)的解决方案是 对值 进行排序,然后执行 二分搜索 。希望 Numpy 提供有用的函数来做到这一点。 O(n log(m)) 中的解决方案 运行(n 要搜索的值的数量和 m 搜索的值的数量)应该比线性搜索快得多 运行 宁 O(n m) 时间。这是一个例子:

# Format the inputs
valType = X_image.dtype
assert Y_image.dtype == valType and X_area.dtype == valType and X_image.dtype == valType
pointType = [('x', valType),('y', valType)]
XY_image = np.ravel(np.column_stack((X_image, Y_image))).view(pointType)
XY_area = np.ravel(np.column_stack((X_area, Y_area))).view(pointType)

# Build an index to sort XY_image and then generate the sorted points
sortingIndex = np.argsort(XY_image)
sorted_XY_image = XY_image[sortingIndex]

# Search each value of XY_area in XY_image and find the location in the unsorted array
tmp = np.searchsorted(XY_image, XY_area)
index = sortingIndex[tmp]

rgb_area = rgb[index]

感谢 Jérôme 的回答,我更加理解了使用 hashmap 的价值:

def hashmap(X,Y):
    return X + 10000*Y

h_area = hashmap(X_area,Y_area)
h_image = hashmap(X_image,Y_image)

np.where(np.isin(h_image,h_area))

这个 hashmap 有点残酷,但实际上 returns 索引:

(array([2, 3, 5], dtype=int64),)