Using other keys for the waitKey() function of opencv

前端 未结 11 722
天涯浪人
天涯浪人 2020-11-29 19:16

I\'m working on a program (python ,opencv) in which I use the spacebar to go to the next frame, and Esc to exit the program. These are the only two

11条回答
  •  生来不讨喜
    2020-11-29 19:42

    As to me, the below code does't work, when it runs,the image will step to the next quickly without your press:

    import cv2
    img = cv2.imread('sof.jpg') # load a dummy image
    while(1):
        cv2.imshow('img',img)
        k = cv2.waitKey(33)
        if k==27:    # Esc key to stop
            break
        elif k==-1:  # normally -1 returned,so don't print it
            continue
        else:
            print k # else print its value
    

    But this works:

    def test_wait_key():
        lst_img_path = [
            '/home/xy/yy_face_head/face_det_test/111.png',
            '/home/xy/yy_face_head/face_det_test/222.png'
            #.....more path ...
        ]
    
        for f_path in lst_img_path:
            img = cv2.imread(f_path)
            cv2.imshow('tmp', img)
            c = cv2.waitKey(0) % 256
    
            if c == ord('a'):
                print "pressed a"
            else:
                print 'you press %s' % chr(c)
    

    Output as below:

提交回复
热议问题