2016-12-04 66 views
0

我被卡住了。我的代碼很糟糕。我的傻子也不行,但無限的形象窗戶讓我瘋狂。當我關閉namedWindow時,它會隨着圖像(無限地)打開一個新的顯示窗口。幫幫我?關閉圖像窗口將顯示另一個窗口。爲什麼?

import numpy as np 
import cv2 
from pylepton import Lepton 

#setup the Lepton image buffer 
def capture(device = "/dev/spidev0.0"): 
    with Lepton() as l: 
     a,_ = l.capture()  #grab the buffer 
    cv2.normalize(a, a, 0, 65535, cv2.NORM_MINMAX) # extend contrast 
    np.right_shift(a, 8, a) # fit data into 8 bits 
    return np.uint8(a) 


#Create a window and give it features 
def nothing(x): 
    pass 
cv2.namedWindow('flir', cv2.WINDOW_NORMAL) 
cv2.moveWindow('flir',1,1) 
cv2.createTrackbar('thresh','flir',50,100,nothing) 
cv2.createTrackbar('erode','flir',5,100,nothing) 
cv2.createTrackbar('dilate','flir',7,100,nothing) 

#process the buffer into an image on a continuous loop  
while True: 

    #update the image processing variables 
    thresh = cv2.getTrackbarPos('thresh', 'flir') 
    erodeSize = cv2.getTrackbarPos('erode', 'flir') 
    dilateSize = cv2.getTrackbarPos('dilate', 'flir') 

    image = capture() 

    #apply some image processing 
    blurredBrightness = cv2.bilateralFilter(image,9,150,150) 
    thresh = 50 
    edges = cv2.Canny(blurredBrightness,thresh,thresh*2, L2gradient=True) 
    _,mask = cv2.threshold(blurredBrightness,200,1,cv2.THRESH_BINARY) 
    erodeSize = 5 
    dilateSize = 14 
    eroded = cv2.erode(mask, np.ones((erodeSize, erodeSize))) 
    mask = cv2.dilate(eroded, np.ones((dilateSize, dilateSize))) 
    adjusted_image = cv2.resize(cv2.cvtColor(mask*edges, cv2.COLOR_GRAY2RGB) | image, (640, 4$ 
    final_image = cv2.applyColorMap(adjusted_image, cv2.COLORMAP_HOT) 

    #display the image 
    cv2.imshow('flir', final_image) 

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

cv2.waitKey() 
cv2.destroyWindow('flir') 
+0

你有一個'while True',所以它在循環結束時關閉窗口,然後從while塊的開始處重新開始。它會永遠做到這一點! – Ali

回答

3

首先,冷靜下來。其次,仔細看看你的代碼。關閉窗口不會給你帶來任何好,因爲線路:

cv2.imshow('flir', final_image)

cv2.destroyWindow('flir') 

什麼這2個正在做的串聯是,你在一個新窗口中顯示的幀,然後摧毀它,然後在imshow中重新創建該窗口,然後顯示下一幀並銷燬它......等等。

這應該解釋你的閃爍窗口。

要停止程序的執行,您添加以下代碼:

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

它所意味的是,當你按「Q」鍵盤上,而你的圖像窗口的重點是,while循環將會中斷,您的程序將終止。

因此,我建議您刪除cv2.destroyWindow並使用'q'鍵退出您的應用程序,而不是使用鼠標關閉它。

+1

謝謝你的明確答案。對不起這部戲。那時凌晨2點,我很沮喪。 –

相關問題