python - numpy,其中 RGB channel 大于 [0,0,0]

标签 python numpy where-clause rgb axis

试图找到不等于黑色的颜色,我需要最快的方法来做到这一点。 Strangle,下面的代码适用于条件!=。它不适用于 > 或 =。另外,我不想对 channel 求和并搜索大于 0 的值(那太慢了)

import numpy as np
x = np.zeros(shape=[2,10,3])
x[0,0]=np.array([0,0,255])
x[0,5]=np.array([255,0,0])
x[0,8]=np.array([255,0,0])
x[0,9]=np.array([255,0,0])
x[1,2]=np.array([0,0,255])
x[1,1]=np.array([255,0,0])
x[1,9]=np.array([255,0,0])
x[1,6]=np.array([255,0,0])

indices=(np.where(np.all(x[0,:]!=[0,0,0],axis=-1),))
print('values',x[i])

>> []

感谢您提前提出建议

最佳答案

IIUC,您可以这样做来查找数组中至少有一个非零值的位置:

x[np.any(x != 0, axis = 2)]
# Or, using np.where as you were:
# np.where(np.any(x!=0, axis=2))
# (array([0, 0, 0, 0, 1, 1, 1, 1]), array([0, 5, 8, 9, 1, 2, 6, 9]))

示例:

>>> x
array([[[  0.,   0., 255.],
        [  0.,   0.,   0.],
        [  0.,   0.,   0.],
        [  0.,   0.,   0.],
        [  0.,   0.,   0.],
        [255.,   0.,   0.],
        [  0.,   0.,   0.],
        [  0.,   0.,   0.],
        [255.,   0.,   0.],
        [255.,   0.,   0.]],

       [[  0.,   0.,   0.],
        [255.,   0.,   0.],
        [  0.,   0., 255.],
        [  0.,   0.,   0.],
        [  0.,   0.,   0.],
        [  0.,   0.,   0.],
        [255.,   0.,   0.],
        [  0.,   0.,   0.],
        [  0.,   0.,   0.],
        [255.,   0.,   0.]]])

>>> x[np.any(x != 0, axis = 2)]
array([[  0.,   0., 255.],
       [255.,   0.,   0.],
       [255.,   0.,   0.],
       [255.,   0.,   0.],
       [255.,   0.,   0.],
       [  0.,   0., 255.],
       [255.,   0.,   0.],
       [255.,   0.,   0.]])

关于python - numpy,其中 RGB channel 大于 [0,0,0],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53179606/

相关文章:

python - 如何在 numpy 中执行此操作(平铺操作的链接)?

python - 在python中根据时间对两个日志数据数组进行排序

python - 如何沿第一维在两个 3D 张量之间执行矩阵乘法?

sql - 按最大条件匹配排序

php - MYSQL WHERE 子句是错误的值

python - 有人有使用 Python Zeep 和 Mock 对 SOAP API 进行单元测试的示例吗?

python - 获得锁后检查 multiprocessing.Value 的值

python - pandas:重命名多索引 df 中的列标签

python - websocket 运行异步函数但返回错误 : asyncio. run() 无法从正在运行的事件循环调用

sql-server - T-SQL : Where xxx IN temporary table