cv2.setmousecallback() 和 cv2.waitkey() 有什么关系

时间:2021-06-12 13:53:32

标签: opencv-python

我试图在视频帧中画一个圆圈,但我遇到了问题,例如:

vid1=cv2.VideoCapture('animal_video.mp4')

if vid1.isOpened():
    print('is opened')

def mousedf(event,x,y,flags,params):
    global nx,ny
    if event == cv2.EVENT_LBUTTONDOWN:
        nx,ny=x,y
    if flags & cv2.EVENT_FLAG_LBUTTON:
        radius=int(math.sqrt((x-nx)**2+(y-ny)**2))
        cv2.circle(frame,(nx,ny),radius,(0,0,200),3)



cv2.namedWindow('frame')
cv2.setMouseCallback('frame',mousedf)

while True:
    ret,frame=vid1.read()
    if not ret:
        print('Not enough Frame, will break')
        break
    k=cv2.waitKey(30)
    if k == 27:
        break
    cv2.imshow('frame',frame)

vid1.release()
cv2.destroyAllWindows()

在上面的代码中,我可以看到每一帧中的圆圈。 但是,如果我更改 cv2.imshow()cv2.waitkey() 的顺序, 我看不到圆圈。

while True:
    ret,frame=vid1.read()
    if not ret:
        print('Not enough Frame, will break')
        break
    cv2.imshow('frame',frame)
    k=cv2.waitKey(30)
    if k == 27:
        break

我想知道问题的原因,以及cv2.setMouseCallback()cv2.waitkey()之间的关系。

1 个答案:

答案 0 :(得分:0)

事实上,当电影在时间上传递图像,而视频实际上是图像在时间上的传递,并在给定时间更新不同序列的cv2.waitKey(*time*)

如果您使用此代码,您将有机会在每次运行后和演出结束后 cv2.imshow('frame', frame)

while True:
    ret,frame=vid1.read()
    if not ret:
        print('Not enough Frame, will break')
        break

    k=cv2.waitKey(30)  --->stop image 
    cv2.imshow('frame', frame) --->show update image
    if k == 27:
        break
相关问题