`cv2.WaitKey`升级; OpenCV代码的例外
假设我有此代码:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论