Using other keys for the waitKey() function of opencv

前端 未结 11 699
天涯浪人
天涯浪人 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:37

    This works the best for me:

    http://www.asciitable.com/

    Sometimes it's the simple answers that are the best ;+)

    0 讨论(0)
  • 2020-11-29 19:38

    For C++:

    In case of using keyboard characters/numbers, an easier solution would be:

    int key = cvWaitKey();
    
    switch(key)
    {
       case ((int)('a')):
       // do something if button 'a' is pressed
       break;
       case ((int)('h')):
       // do something if button 'h' is pressed
       break;
    }
    
    0 讨论(0)
  • 2020-11-29 19:39

    If you want to pause the program to take screenshots of the progress

    (shown in let's say cv2.imshow)

    cv2.waitKey(0) would continue after pressing "Scr" button (or its combination), but you can try this

    cv2.waitKey(0)
    input('')
    

    cv2.waitkey(0) to give the program enough time to process everything you want to see in the imshow and input('')

    to make it wait for you to press Enter in the console window

    this works on python 3

    0 讨论(0)
  • 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:

    0 讨论(0)
  • 2020-11-29 19:48

    The answer that works on Ubuntu18, python3, opencv 3.2.0 is similar to the one above. But with the change in line cv2.waitKey(0). that means the program waits until a button is pressed.

    With this code I found the key value for the arrow buttons: arrow up (82), down (84), arrow left(81) and Enter(10) and etc..

    import cv2
    img = cv2.imread('sof.jpg') # load a dummy image
    while(1):
        cv2.imshow('img',img)
        k = cv2.waitKey(0)
        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
    
    0 讨论(0)
提交回复
热议问题