我有一个numpy数组:(例如:)
>>> pixels
array([[233, 233, 233],
[245, 245, 245],
[251, 251, 251],
[248, 248, 248],
[248, 248, 248],
[248, 248, 248],
[248, 248, 248],
[248, 248, 248],
[248, 248, 248],
[248, 248, 248]], dtype=uint8)
如何为大于230且小于240的值(例如)获取布尔数组? 当我写作
230<pixels<240
我得到了这个按摩:
Traceback (most recent call last):
File "<pyshell#78>", line 1, in <module>
100<pixels<300
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
如果我写
,它也不起作用230<pixels and 240>pixels
非常感谢!
答案 0 :(得分:0)
使用numpy.where
例程:
import numpy as np
a = np.array([[233, 233, 233],
[245, 245, 245],
[251, 251, 251],
[248, 248, 248],
[248, 248, 248],
[248, 248, 248],
[248, 248, 248],
[248, 248, 248],
[248, 248, 248],
[248, 248, 248]], dtype='uint8')
b = np.where((a > 230) & (a < 240), True, False)
print(b)
输出:
[[ True True True]
[False False False]
[False False False]
[False False False]
[False False False]
[False False False]
[False False False]
[False False False]
[False False False]
[False False False]]