2016-04-09 212 views
1

我剛剛開始學習用於Raspberry Pi 3的Python OpenCV。 我爲namedWindow()編寫了一個簡單的代碼。問題是destroyWindow()不能像我期望的那樣工作。 「測試」窗口仍然存在。下面的代碼有什麼問題?cv2.destroyWindow()不能按預期工作

import cv2 
import sys 

if __name__ == '__main__': 
    cv2.namedWindow("TEST") 
    while True: 
     key = cv2.waitKey(5) 
     if key == 27: 
      print "ESC pressed..." 
      cv2.destroyWindow("TEST") 
      break 
    sys.exit() 
+0

你可能想看看[這個鏈接](http://stackoverflow.com/questions/6116564/destroywindow-does-not-close-window-on-mac-using-python-and-opencv)。 – Aenimated1

+0

將destroyWindow(「TEST」)移出循環。 –

+0

當我最後一次使用'destroyWindow'時,我不得不在'destroyWindow'前後兩次調用'waitKey'(前兩個,後五個),我還必須啓動一個'cvWindowThread'就在我創建'namedWindow'之前。然後,我在末尾調用了'std :: terminate()',因爲線程沒有正確關閉(注意,這是在C++中,python可能更好地處理線程)。 –

回答

0

這裏就是平時對我的作品儘可能的waitKey雲:

if cv2.waitKey(1) & 0xFF == 27: 
    break 
cv2.destroyWindow("TEST") 

這是一個位,並與waitkey輸入和11111111應該等於UTF-8值的關鍵。我在This Loop上測試了這個。希望這可以幫助。