What is relationship between cv2.setmousecallback() and cv2.waitkey()

Viewed 170

I am trying to draw a circle in a video frame but I have problem, for example:

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()

In the above code, I can see the circle in each frame. However, if I change the order of cv2.imshow() and cv2.waitkey(), I cannot see the circle.

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

I would like to know the reason of the problem, and the relationship between cv2.setMouseCallback() and cv2.waitkey().

1 Answers

In fact, when a movie is passing images in time and In fact, a video is the passage of images in time and updates the cv2.waitKey(*time*) of different sequences with the given time.

If you use this code, you will be given the opportunity to update the new image after each run and after the show by 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
Related