Numpy.where uses

放肆的年华 提交于 2021-01-29 19:32:18

问题


Use numpy.where to get all (R, G,B) in a numpy.array with a definite value of R, G and B

The problem is i'm not sure i can use numpy.where to get what i want :

i tried the following code :

L = numpy.array([[1,2,3],[1,1,1],[1,1,1]])
print(numpy.where(L==(1,1,1)))

(array([0, 1, 1, 1, 2, 2, 2], dtype=int64), array([0, 0, 1, 2, 0, 1, 2], dtype=int64))

and i understand it's returning me the coordinates of every element == 1 but i would like it to return the index in L of the element equal to (1,1,1) :

array([1,2])


回答1:


You are looking for numpy.nonzero together with np.all (to ensure that each of RGB matches):

>>> numpy.nonzero(numpy.all(L == (1, 1, 1), axis=1))[0]
array([1, 2])


来源:https://stackoverflow.com/questions/55526150/numpy-where-uses

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!