我有一个灰度图像作为python中的numpy数组加载。我想找到图像强度在指定范围内的坐标,例如[lowerlim,upperlim]
。我试图找到使用numpy.where
as
np.where(image>lowerlim and image<upperlim)
但这给了错误-“具有多个元素的数组的真值是不明确的。” 谁能指导我如何在python中做到这一点?
如注释中所述,np.logical_and
如果要使用逻辑数组和numpy数组,则需要使用;如果要选择指定的元素,则可以将传递np.where
给数组:
>>> a
array([[[ 2, 3],
[ 4, 5]],
[[ 9, 10],
[20, 39]]])
>>> np.where(np.logical_and(3<a,a<10))
(array([0, 0, 1]), array([1, 1, 0]), array([0, 1, 0]))
>>> a[np.where(np.logical_and(3<a,a<10))]
array([4, 5, 9])
或者代替np.where
您可以直接使用np.extract
:
>>> np.extract(np.logical_and(3<a,a<10),a)
array([4, 5, 9])
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句