在python中查找指定范围内的图像像素坐标
Finding the co-ordinates of image pixels within a specified range in python
我在 python 中加载了一个灰度图像作为 numpy 数组。我想找到图像强度在指定范围内的坐标,比如 [lowerlim,upperlim]
。我尝试使用 numpy.where
as
查找
np.where(image>lowerlim and image<upperlim)
但出现错误 - 'The truth value of an array with more than one element is ambiguous.' 谁能指导我如何在 python 中执行此操作?
正如评论中所说,如果你想对 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.extract
代替 np.where
:
>>> np.extract(np.logical_and(3<a,a<10),a)
array([4, 5, 9])
我在 python 中加载了一个灰度图像作为 numpy 数组。我想找到图像强度在指定范围内的坐标,比如 [lowerlim,upperlim]
。我尝试使用 numpy.where
as
np.where(image>lowerlim and image<upperlim)
但出现错误 - 'The truth value of an array with more than one element is ambiguous.' 谁能指导我如何在 python 中执行此操作?
正如评论中所说,如果你想对 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.extract
代替 np.where
:
>>> np.extract(np.logical_and(3<a,a<10),a)
array([4, 5, 9])