首页 > 解决方案 > numpy.ndarray 的 numpy.argwhere 的替代函数是什么?

问题描述

我想定位单色图像的局部最大值。该图像是一个 numpy.ndarray,dtype 为 uint8。

图像有几个点的最大值为 255。因此我打算取位置的平均值来找到最大值的有效位置。

对于普通的 numpy.array,以下代码将有效地返回值等于 255 的元素的位置。

positions = numpy.unravel_index(image.argwhere(), image.shape)

其中 image 是 numpy.array 对象。但是在我的代码中, image 是一个 numpy.ndarray 对象,导致我收到以下错误:

AttributeError: 'numpy.ndarray' object has no attribute 'argwhere'

'argwhere' 是否有适用于 numpy.ndarray 的替代功能?

标签: pythonimagenumpycv2

解决方案


argwhere是 numpy 库的一个函数,它应该像

np.argwhere(image)

无论如何,np.array它不是一个单独的类型,它只是另一种创建np.ndarray. 尝试type任何你喜欢的 numpy-array 变量,它总是一个np.ndarray.


推荐阅读