【问题标题】:OpenCV functions won't work on OpenCV window converted in PyQt GUI windowOpenCV 函数在 PyQt GUI 窗口中转换的 OpenCV 窗口上不起作用
【发布时间】:2015-05-02 01:26:40
【问题描述】:

我正在使用 OpenCV 进行图像处理并将图像嵌入到 PyQt GUI 窗口中。我已经独立使用了 OpenCV 代码,它工作正常。我还可以在 OpenCV 中创建以下窗口和对象,并通过 cv2.cvtColor 成功地将它们转换为其他颜色空间,然后转换为 QPixmap 并显示它们。

当我尝试对 OpenCV 图像执行任何操作而不是转换颜色空间时,就会出现问题。我尝试过简单的 OpenCV 操作,例如 cv.Smooth,甚至使用 cv.CreateImage 创建阈值图像,但这些都不起作用。我不确定执行这些操作是否需要很长时间并且 QTGui 是否超时。 请帮帮我,我不明白出了什么问题。我在下面提供了我的代码的 sn-ps:

import cv2    
import cv2.cv as cv
import numpy as np
import from PyQt4 import QtGui, QtCore

class Video():
    def __init__(self,capture):
        self.capture = capture
        self.capture.open(1)
        self.currentFrame=np.array([])
        self.capture.set(3,800)
        self.capture.set(4,600)
        self.capture.set(5,30)

    def processImage(self):
        try:
            while(True):
                ret, readFrame=self.capture.read() # Grab next frame
                if(ret==True):
                    print "processImage grabbed a frame"

                    #readFrame=cv2.cvtColor(readFrame,cv2.COLOR_BGR2HSV) < -- This works fine
                    #readFrame=cv2.cvtColor(readFrame,cv2.COLOR_HSV2RGB) < -- This works fine
                    cv.Smooth(readFrame,readFrame,cv.CV_BLUR,3) < -- Any functions like this cause problems
                    print 'It made it this far' # < -- This never gets printed because cv.Smooth doesn't properly execute
        except TypeError:
            print "ERROR: processImage couldn't get a frame"

        def convertFrame(self):
    """     converts frame to format suitable for QtGui            """
        try:
            height,width=self.currentFrame.shape[:2]
            img=QtGui.QImage(self.currentFrame,width,height,QtGui.QImage.Format_RGB888)
            img=QtGui.QPixmap.fromImage(img)
            self.previousFrame = self.currentFrame
            return img
        except:
            return None

我什至尝试使用 cv.Smooth 然后使用 cv2.cvtColor 将 RGB 转换为 QT Gui,这没关系;一旦它碰到 cv.Smooth 或除 cvtColor 之外的任何其他 OpenCV 函数,它就不会越过它。

这是图形用户界面的代码:

class Gui(QtGui.QMainWindow):
    def __init__(self,parent=None):
        QtGui.QWidget.__init__(self,parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.video = Video(cv2.VideoCapture()) # Which device to capture frames from
        self._timer = QtCore.QTimer(self)
        self._timer.timeout.connect(self.play)
        self._timer.start(27)
        self.update()

    def play(self):
        try:
            self.video.processImage()
            self.ui.videoFrame.setPixmap(self.video.convertFrame())
            self.ui.videoFrame.setScaledContents(True)
        except TypeError:
            print "No frame"

def main():
    app = QtGui.QApplication(sys.argv)
    ex = Gui()
    ex.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

这可能与 Qt GUI 调用 processImage() 有关,并且在前一个函数返回之前继续下一次调用 processImage() 需要很长时间来处理图像和超时?

请帮忙!!!

【问题讨论】:

    标签: python qt opencv image-processing pyqt


    【解决方案1】:

    甚至没有人试图回答这个问题,真的吗?好吧,如果您遇到这种情况并需要弄清楚,这里是问题的解决方案:

    1. 始终使用 cv2 类而不是旧的 cv 类。

    2. 在我的代码中,我尝试使用返回 cvMat 的 cv.QueryFrame,而 pyqt 出于某种原因不喜欢这种数据类型。

    3. 您需要将所有旧的 cv 函数转换为新的 cv2 函数。调用 capture.read() 而不是 cv.QueryFrame(capture) 返回一个 VideoCapture 对象,这是 pyqt 可以处理的数据类型。

    4. 找出对应的函数使用 cv2 有点麻烦,但一旦你开始工作,就值得了。

    【讨论】:

      猜你喜欢
      • 2017-10-28
      • 1970-01-01
      • 2015-11-20
      • 1970-01-01
      • 1970-01-01
      • 2018-07-06
      • 1970-01-01
      • 2012-08-08
      • 2013-01-07
      相关资源
      最近更新 更多