`cv2.WaitKey`升级; OpenCV代码的例外

发布于 2025-01-29 08:12:23 字数 1038 浏览 3 评论 0原文

假设我有此代码:

import cv2
import numpy as np

img = np.zeros((1080, 1920, 3), np.uint8)

def click_event(event, x, y, flags, params):
    global myx, myy
    if event == cv2.EVENT_LBUTTONDOWN:
        myx = x
        myy = y
        print(myx, myy)
        key = cv2.waitKey(0)
        if key == 13:
            cv2.destroyAllWindows()
            cv2.imshow('abc', img)
            print(myx * myy)
        if key == 97:
            cv2.destroyAllWindows()
            return

cv2.imshow('freeze frame', img)
cv2.setMouseCallback('freeze frame', click_event)
cv2.waitKey(0)
cv2.destroyAllWindows()

print(myx + myy)

此代码显示一个黑色图像,并打印出您单击图像的位置的X和Y值。而且,如果按键盘上的输入,它假设关闭窗口并再次显示图像,然后打印X的结果乘以y。而且,如果您按键盘a,则代码停止,窗口已关闭。

但是有一个问题,当我用鼠标单击屏幕并单击Enter之后,它给出了一个错误:

cv2.error: Unknown C++ exception from OpenCV code

The Trackback指的是第23行,这是:

cv2.waitKey(0)

看起来像Waitkey不起作用。使用Destractallwindows ...但是有能力解决吗?

Let's say I have this code:

import cv2
import numpy as np

img = np.zeros((1080, 1920, 3), np.uint8)

def click_event(event, x, y, flags, params):
    global myx, myy
    if event == cv2.EVENT_LBUTTONDOWN:
        myx = x
        myy = y
        print(myx, myy)
        key = cv2.waitKey(0)
        if key == 13:
            cv2.destroyAllWindows()
            cv2.imshow('abc', img)
            print(myx * myy)
        if key == 97:
            cv2.destroyAllWindows()
            return

cv2.imshow('freeze frame', img)
cv2.setMouseCallback('freeze frame', click_event)
cv2.waitKey(0)
cv2.destroyAllWindows()

print(myx + myy)

This code displays a black image, and prints out the x and y values of where you click on the image. And, if you press enter on your keyboard, it suppose to close the window and display the image again, and print the result of x multiplied by y. And, if you press on your keyboard a, the code stops and the window is closed.

But there is a problem, after I click the screen with my mouse, and click enter, it gives an error:

cv2.error: Unknown C++ exception from OpenCV code

And the traceback refers to line 23, which is:

cv2.waitKey(0)

It seems like waitKey doesn't work with destroyAllWindows... But is there a workaround for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文