2015-04-04 240 views
0

我想要在numpy图像数组中获取给定值的x和y坐标。在python数组中查找值的X和Y坐标

我可以通过使用for语句手动运行行和列来完成此操作,但这看起来相当慢,而且我正面有更好的方法来执行此操作。

我试图修改我在本文中找到的解决方案。 Finding the (x,y) indexes of specific (R,G,B) color values from images stored in NumPy ndarrays

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 documentation

+0

那么到底什么是你现在的问题? – Kasramvd 2015-04-04 21:17:31

回答

1

我不太明白的问题。 all()中的轴参数在颜色通道(轴2-1)上运行,而不是xy索引。然后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])) 
>>> 

这其实只是重复您链接到答案。但是评论太长了。也许你可以解释一下为什么你需要修改以前的答案?看起来你并不需要。

+0

所以我想了解np.where。我需要做的是查看行并将所有匹配放入数组中,然后查看列并将列放入数组中。 – 2015-04-06 13:34:27

+0

'where'一次返回行和列。阅读文档http://docs.scipy.org/doc/numpy/reference/generated/numpy.where.html – YXD 2015-04-06 13:44:30