Using other keys for the waitKey() function of opencv

前端 未结 11 706
天涯浪人
天涯浪人 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条回答
  •  Happy的楠姐
    2020-11-29 19:29

    You can use ord() function in Python for that.

    For example, if you want to trigger 'a' key press, do as follows :

    if cv2.waitKey(33) == ord('a'):
       print "pressed a"
    

    See a sample code here: Drawing Histogram

    UPDATE :

    To find the key value for any key is to print the key value using a simple script as follows :

    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
    

    With this code, I got following values :

    Upkey : 2490368
    DownKey : 2621440
    LeftKey : 2424832
    RightKey: 2555904
    Space : 32
    Delete : 3014656
    ...... # Continue yourself :)
    

提交回复
热议问题