【问题标题】:OpenCV: wait for different keys?OpenCV:等待不同的键?
【发布时间】:2016-09-26 17:09:57
【问题描述】:

我正在使用 OpenCV,我希望 OpenCV 等待 any 键被按下(默认行为为cv2.waitKey()),而是等待特定的键我定义(并执行后续操作)。

我目前对这个问题的解决方案是以下递归解决方案:

def opencv_wait():
    # wait for keypress; capture it
    k = cv2.waitKey(0)

    if k == 27:  # this should be ESC
        return   # e.g. end the program
    elif k == some_key:      # some other keys...
        do_some_function()   # ...and actions to do after key is pressed
    else:
        opencv_wait()        # recursively call opencv_wait() for looping

我的问题是:这个解决方案是让 OpenCV 等待不同键的便捷方式吗?

有没有更快/更好的方法来实现我想做的事情?

基本上,我希望 OpenCV 无限长地等待(尽可能少地浪费资源),直到按下应该触发后续操作的特定键。

【问题讨论】:

  • 我也在找这个。不幸的是,我认为没有更好的方法来等待特定的密钥。

标签: python python-2.7 opencv wait keypress


【解决方案1】:

如果没有必要,您可以尝试非递归方法:

def is_pressed(key)
    # if statement

def opencv_wait():
    key = 0

    while is_pressed(key) :
        key = cv2.waitKey(0)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-09
    • 1970-01-01
    • 1970-01-01
    • 2018-07-17
    相关资源
    最近更新 更多