[英]How to find the x and y coordinates of a given value using Numpy array
[英]Find X and Y Coordinates of a value in a python array
我正在尝试在numpy图像数组中获取给定值的x和y坐标。
我可以通过使用for语句手动遍历行和列来做到这一点,但这似乎相当慢,并且我认为有更好的方法可以做到这一点。
我正在尝试修改本文中找到的解决方案。 从存储在NumPy ndarrays中的图像中查找特定(R,G,B)颜色值的(x,y)索引
a = image
c = intensity_value
y_locs = np.where(np.all(a == c, axis=0))
x_locs = np.where(np.all(a == c, axis=1))
return np.int64(x_locs), np.int64(y_locs)
我有np.int64将值转换回int64。
我也在看numpy.where文档
我不太明白这个问题。 all()
的axis参数在颜色通道(第2
轴或-1
轴)上而不是在x
和y
索引上运行。 然后where()
将为您提供图像中匹配值的坐标:
>>> # set up data
>>> image = np.zeros((5, 4, 3), dtype=np.int)
>>> image[2, 1, :] = [7, 6, 5]
>>> # find indices
>>> np.where(np.all(image == [7, 6, 5], axis=-1))
(array([2]), array([1]))
>>>
这实际上只是重复您链接的答案。 但是评论太久了。 也许您可以再解释一下为什么您需要修改先前的答案? 似乎不需要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.