2016-09-07 64 views
0

在python 2.7.12中使用opencv3.1作爲cv2。我現在遇到的問題是,雖然我遵循了多套指令,似乎都使用與我自己相同的設置,或者至少有一個非常類似的指令。我主要是通過這兩個例子:openCV.orgCodeGenerater's Blogspot tutorial。我不忘記做一個回調函數或使用cv2.getTrackbarPos。我覺得在我做的或圖像顯示循環中的特定順序一定有問題。這裏是我有什麼,它diaplays與最初的TrackBar門檻的形象,但不更新與跟蹤條回調的圖像:cv2 getTrackbarPos不工作

import cv2 


#write simple callback function to pass trackbar position as *arg  
def callback(*arg): 
    pass 

#create display window for image 
cv2.namedWindow('frame') 

#read in image 
img = cv2.imread(r'/home/Usr/Documents/Aerial-Images/images_with_targets/Flight_4/target_10.jpg',0) 

#instantiate trackbar that goes in our named window and uses callback function 
cv2.createTrackbar('thresh2','frame',5,15,callback) 

#initialize thresholds 
thresh1=11 
thresh2=5 

#loop really just runs until the escape key causes a break 
while(True): 

    #sets threshold 2 to trackbar position 
    thresh2=cv2.getTrackbarPos('thresh2','frame') 
    #apply laplacian filter to ehance edge gradients 
    th = cv2.Laplacian(img,cv2.CV_8UC1) 
    #binarize image with adaptive threshold 
    th = cv2.adaptiveThreshold(th,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY_INV,thresh1,thresh2) 

    #show filtered image 
    cv2.imshow('frame',th) 
    #waits for escape key then breaks out of loop 
    if cv2.waitKey(0) & 0xFF == ord('q'): 
     break 


#close our display window  
cv2.destroyallwindows() 

回答

0

答案是真的很簡單。在查看一些舊的代碼我寫的,我意識到我需要改變從0到1的等待鍵:

if cv2.waitKey(0) & 0xFF == ord('q'): 
    break 

成爲

if cv2.waitKey(1) & 0xFF == ord('q'): 
    break 

我沒有看到的是,我忘了駝峯cv2.destroyAllWindows什麼,這讓我覺得顯示器循環還在運行,但實際上並沒有。

相關問題