首页 > 解决方案 > cv2.imshow 仅在第一个循环中有效

问题描述

我正在从相机读取图像数据并尝试使用 openCV2 显示它。
我的 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)

我不确定这段代码是否足以找到错误。如果您需要,我可以发布更多。

谢谢!

标签: python-3.xopencv

解决方案


推荐阅读