Thursday, August 01, 2013

cv2.waitKey bug

I was trying a Grabcut sample code (don't use this version) written in Python by abid rahman and stuck at a strange problem: the cv2.waitKey() didn't work normally!

Yesterday I searched the problem on Google and found nothing useful (according to my skill level, I might overlooked something that could be hints), so I decided to ask in the G+ Python community [1].

Based on Brett Ponsler's helpful suggestion, I tried to rewrite my test code and also went to download the Grabcut sample code again (this version is fine). This time, the sample code was running successfully.

Then I noticed a magic word ``0xFF'' in the new downloaded sample code. Using the hint, I finally found the bug report about cv2.waitKey() and came up with a tiny test code:

import cv2
import numpy as np

cv2.namedWindow('test')

while True:
    #key = cv2.waitKey(33) #this won't work
    #key = 0xFF & cv2.waitKey(33) #this is ok
    key = np.int16(cv2.waitKey(33)) #this is ok [2]

    if key == 27:
        break
    else:
        print key, hex(key), key % 256

cv2.destroyAllWindows()
---
[1] The question I posted in the Python community: https://plus.google.com/117911423781149907976/posts/CdvQ3vWVV19
[2] http://jiaxihu.blogspot.tw/2013/02/opencv-python-64bit-waitkey.html

No comments:

Post a Comment