在python中,我有一个灰度图像作为numpy数组加载。我想找到像强度在指定范围内的坐标,[lowerlim,upperlim]
说。我尝试使用numpy.where
作为
np.where(image>lowerlim and image<upperlim)
但它却产生了错误--“包含多个元素的数组的真值是模糊的。”有人能指导我怎么用蟒蛇做这件事吗?
发布于 2015-03-21 23:46:13
正如注释中所述,如果要使用逻辑和numpy数组,并且要选择指定的元素,则需要使用np.logical_and
,并且可以将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])
https://stackoverflow.com/questions/29192025
复制相似问题