python-3.x - cv2.imshow 仅在第一个循环中有效
问题描述
我正在从相机读取图像数据并尝试使用 openCV2 显示它。
我的 GUI 执行以下操作:
- 带有两个按钮的 GUI:连接到相机并开始实时图像显示(在单独的线程中这样做)
- 停止按钮停止实时图像,加入线程并退出相机连接
不幸的是,只有第一次单击按钮时,才会显示实际的“实时视频”(超过 24 fps)。之后,单击开始按钮时没有任何反应(即没有打开任何窗口,甚至没有打开一个空窗口或带有旧图像数据),尽管没有出现错误。
到目前为止我做了什么:
- 检查程序是否正在运行所有必要的步骤 - 确实如此!
- 尝试显示硬盘驱动器中的图像(并在显示之前立即调用它) - 相同的行为
- 尝试在没有多线程的情况下显示图像 - 它有效!
cv2.imshow(...)
这使我得出结论,多线程肯定存在一些问题。你有什么想法,这个问题可能是什么?
以下是 GUI 的代码片段(.pack
不包括在内):
startLiveBtn = tk.Button(self, text = "start Live image", command = self.cam.startLiveImg)
stopBtn = tk.Button(self, text = "stop live img", command = self.cam.stopLiveImg)
实时图像显示开始和停止:
def showLiveImg(self):
while self.do_run:
self.extractImg()
self.showImg()
if cv2.waitKey(1) & 0xFF == ord('q'):
self.stopLiveImg()
def startLiveImg(self):
self.do_run = True
self.live_img_thread = threading.Thread(target = self.doLive, daemon = True)
self.live_img_thread.start()
def stopLiveImg(self):
self.do_run = False
try:
self.live_img_thread.join()
except:
print("could not join thread, assume you pressed q")
self.freeImgMem()
self.exitCam()
从相机获取数据并将其显示在 openCV 窗口中:
def extractImg(self):
self.array = ueye.get_data(self.pcImageMemory, self.width, self.height, self.nBitsPerPixel, self.pitch, copy=False)
self.frame = np.reshape(self.array,(self.height.value, self.width.value, self.bytes_per_pixel))
self.frame = cv2.resize(self.frame, (0, 0), fx = self.windowSize, fy = self.windowSize)
def showImg(self):
cv2.imshow("ueye camera", self.frame)
我不确定这段代码是否足以找到错误。如果您需要,我可以发布更多。
谢谢!
解决方案
推荐阅读
- java - 在 Java 中使用 doc4J 加载 .XML 文件时出现问题
- python - 如何在熊猫 barh 图中仅堆叠选定的列
- python - pandas.to_csv 不将浮点值显示为小数点后 2 位
- python - 为什么第 7 行中的变量 url 不显示为 varibal?
- python - 如何检测数据帧列中逻辑序列中的错误?
- php - 生成二维码并合并到编辑后的 JPG 文件中
- image - ASP.NET MVC 中的图像
- python-3.x - 如果我将从 db 获取的索引号存储在变量中并在按索引从列表中选择时使用,m 得到错误作为预期字符串,int found-Robot Framework
- iteration - 赛普拉斯:检查href列表,断言它们不包含某个字符串
- python - 如何将 pytest 固定装置拆解与多次调用的函数一起使用?