【问题标题】:How to detect colors one at a time如何一次检测一种颜色
【发布时间】:2020-05-26 21:05:30
【问题描述】:

我需要检测两种颜色,一种接一种。 所以这是我的程序工作流程的一个例子:
检测并找到具有特定颜色的对象,在该对象与相机足够近(指定多少)后,
程序应尝试开始寻找另一种颜色。

这是我迄今为止尝试过的

# All python's imports

vs = VideoStream(src=0).start()
# Defining the two colors bound
blueLower = np.array([110, 50, 50])
blueUpper = np.array([130, 255, 255])

greenLower = np.array([29, 86, 6])
greenUpper = np.array([64, 255, 255])

# Defining a function to start the loop so i can later rerun it with different color bounds
def loop(lower, upper):

    while True:
    ....
    ....

# If the object is close enough, change the loopj arguments to search for a new color
    if radius > 250:
      loop(greenLower, greenUpper)

会发生什么,当半径大于 250 时,它只是重新运行原来的

【问题讨论】:

  • 这是准确的代码吗?如果您每次都明确发送greenLowergreenUpper,它不会每次都搜索不同的颜色。您需要在每个函数调用中交替使用 greenblue
  • “你需要在每个函数调用中用绿色和蓝色交替”,我怎样才能做到这一点。

标签: python opencv object-detection


【解决方案1】:

您可以使用标准库中的itertools 来实现这一点(您无需安装,只需导入即可)。尽管还有其他方法可以在值之间切换,但这种方法很方便。我修改了你的部分代码,如果有不明白的地方可以告诉我。

import itertools

blueLower = [110, 50, 50]
blueUpper = [130, 255, 255]

greenLower = [29, 86, 6]
greenUpper = [64, 255, 255]

greenBounds = (greenLower, greenUpper)
blueBounds = (blueLower, blueUpper)


def loop(colorBounds, iterator):
    radius = 0
    lower, upper = colorBounds
    print(lower, upper)
    while True:
        radius += 1
        if radius > 250:
            loop(iterator(), iterator)

toggle = itertools.cycle([greenBounds, blueBounds]).__next__
loop(greenBounds, toggle)

为了澄清,我添加了radius=0radius += 1 用于我自己的测试目的。

【讨论】:

  • 这完全符合现在的预期,我用相应的更改更新了代码。非常感谢@Ahmet
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-18
  • 2019-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-22
相关资源
最近更新 更多