【问题标题】:Finding the co-ordinates of image pixels within a specified range in python在python中查找指定范围内图像像素的坐标
【发布时间】:2015-03-22 07:32:06
【问题描述】:

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

np.where(image>lowerlim and image<upperlim)

但它给出了错误 - “具有多个元素的数组的真值不明确。”谁能指导我如何在 python 中做到这一点?

【问题讨论】:

  • 您可能正在寻找np.logical_and 而不是常规的and
  • 感谢@cel ...解决了这个问题。我使用的命令: np.where(np.logical_and(image>lowerlim,image

标签: python image-processing numpy logical-operators


【解决方案1】:

正如评论中所说,如果您想使用逻辑数组和 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])

【讨论】:

  • 附带说明,&amp; 运算符可以很好地代替 np.logical_and。例如。 (a &gt; 3) &amp; (a &lt; 10) 而不是 np.logical_and(a&gt;3,a&lt;10)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-07
  • 1970-01-01
  • 2021-12-28
  • 2013-08-07
  • 2020-05-19
相关资源
最近更新 更多