2017-09-15 482 views
1

我檢查其他問題在谷歌或stackoverflow,他們正在談論腳本中運行cv2.imshow,但我的代碼運行在jupyter筆記本。opencv.imshow會導致jupyter筆記本崩潰

這裏是我的配置:

  1. Ubuntu的16.4x64

  2. 蟒蛇3.5

  3. 的OpenCV 3.1.0

我開始jupyter筆記本:這裏是我把它放在筆記本上的代碼:

%pylab notebook 
import cv2 

cvim2disp = cv2.imread('data/home.jpg') 
cv2.imshow('HelloWorld', cvim2disp) 
cv2.waitKey() #image will not show until this is called 
cv2.destroyWindow('HelloWorld') #make sure window closes cleanly 

當我執行這些代碼。圖像會顯示在彈出窗口中,但我不能通過單擊右上角的x關閉此窗口,稍後,系統會提示我該窗口沒有響應,它會給我2個選擇:「等待「,」退出「。如果我碰到等待,那麼稍後它會顯示相同的提示,如果我點擊「退出」,那麼jupyter筆記本內核就會死機,我必須重新開始。

我Google的中心,許多解決方案建議我應該加入這樣的代碼

cv2.startWindowThread() 

imshow過,但情況變得更糟,內核永遠掛!任何人都有一些想法是怎麼回事。

這裏是我的錯誤的PIC: enter image description here

+0

'cv.imshow'並沒有真正意義的客戶機/服務器環境下,如Jupyter。你需要一些東西來顯示圖像作爲筆記本的一部分(在客戶端),而不是在服務器端運行GUI窗口 - 當客戶端和服務器在同一臺機器上時,這可能會「起作用」,但就是這樣。 |你已經使用matplotlib中的'imshow'函數(不像你似乎認爲的那樣numpy),所以堅持這一點(記住它使用RGB而不是顏色平面的BGR順序)。 –

回答

0

API documentation for cv2.waitKey()注意事項如下:

This function is the only method in HighGUI that can fetch and handle events, so it needs to be called periodically for normal event processing unless HighGUI is used within an environment that takes care of event processing.

因此,也許調用函數中無限循環就會使窗口響應?我沒有測試過這一點,但也許你想嘗試以下操作:

import cv2 

cvim2disp = cv2.imread('data/home.jpg') 
cv2.imshow('img', cvim2disp) 
while(True): 
    k = cv2.waitKey(33) 
    if k == -1: # if no key was pressed, -1 is returned 
     continue 
    else: 
     break 
cv2.destroyWindow('img')