Skip to main content
 首页 » 编程设计

python之在python中查找指定范围内的图像像素的坐标

2025年05月04日9luoye11

我在 python 中将灰度图像加载为 numpy 数组。我想找到图像强度在指定范围内的坐标,比如 [lowerlim,upperlim]。我尝试使用 numpy.whereas

查找
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.extract 而不是 np.where :

>>> np.extract(np.logical_and(3<a,a<10),a) 
array([4, 5, 9])