gpt4 book ai didi

python - 关闭图像窗口会显示另一个窗口。为什么?

转载 作者:行者123 更新时间:2023-12-02 16:49:30 25 4
gpt4 key购买 nike

我被卡住了。我的代码很烂。我的贴图机也不起作用,但是无限的图像窗口让我发疯。当我关闭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')

最佳答案

首先,冷静点。

其次,仔细查看您的代码。由于以下几行,关闭窗口对您没有任何好处:
cv2.imshow('flir', final_image)

cv2.destroyWindow('flir')

这两个 Action 是在一个新窗口中显示一个框架,然后销毁它,然后在 imshow中重新创建该窗口,然后显示下一个框架并销毁它,依此类推。

那应该可以解释您闪烁的窗口。

为了停止执行程序,您添加了以下代码:
 if cv2.waitKey(1) == ord('q'):
break

这意味着当您在图像窗口处于焦点状态时按键盘上的“q”时,while循环将中断并且程序将终止。

因此,我建议您删除 cv2.destroyWindow并使用“q”键退出应用程序,而不要使用鼠标关闭它。

关于python - 关闭图像窗口会显示另一个窗口。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40956018/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com