python - 在 PyQt 的主窗口之上显示 OpenCv 窗口

标签 python opencv pyqt4

我做了一个 opencv 项目,它处理来自视频的输入流并显示处理后的输出。我使用 PyQt 按钮从一个输出切换到另一个。我的 PyQt 窗口几乎覆盖了整个屏幕,当我点击我的按钮时,opencv 窗口仍然在 PyQt 窗口后面。此外,我已将 PyQt 的主窗口设为我的父窗口。如何将 opencv 窗口置于 PyQt 窗口之上。我搜索了 cvGetWindowHandle(),但没有找到它的 python 实现。

我用过PyQt4和opencv2,PyQt窗口没有用QtDesigner设计过。

最佳答案

您始终可以将 OpenCV 窗口包装在 Qt 小部件中...

class QtCapture(QtGui.QWidget):
    def __init__(self, *args):
        super(QtGui.QWidget, self).__init__()

        self.fps = 24
        self.cap = cv2.VideoCapture(*args)

        self.video_frame = QtGui.QLabel()
        lay = QtGui.QVBoxLayout()
        lay.setMargin(0)
        lay.addWidget(self.video_frame)
        self.setLayout(lay)

    def setFPS(self, fps):
        self.fps = fps

    def nextFrameSlot(self):
        ret, frame = self.cap.read()
        # OpenCV yields frames in BGR format
        frame = cv2.cvtColor(frame, cv2.cv.CV_BGR2RGB)
        img = QtGui.QImage(frame, frame.shape[1], frame.shape[0], QtGui.QImage.Format_RGB888)
        pix = QtGui.QPixmap.fromImage(img)
        self.video_frame.setPixmap(pix)

    def start(self):
        self.timer = QtCore.QTimer()
        self.timer.timeout.connect(self.nextFrameSlot)
        self.timer.start(1000./self.fps)

    def stop(self):
        self.timer.stop()

    def deleteLater(self):
        self.cap.release()
        super(QtGui.QWidget, self).deleteLater()

...随心所欲地使用它:

class ControlWindow(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.capture = None

        self.start_button = QtGui.QPushButton('Start')
        self.start_button.clicked.connect(self.startCapture)
        self.quit_button = QtGui.QPushButton('End')
        self.quit_button.clicked.connect(self.endCapture)
        self.end_button = QtGui.QPushButton('Stop')

        vbox = QtGui.QVBoxLayout(self)
        vbox.addWidget(self.start_button)
        vbox.addWidget(self.end_button)
        vbox.addWidget(self.quit_button)
        self.setLayout(vbox)
        self.setWindowTitle('Control Panel')
        self.setGeometry(100,100,200,200)
        self.show()

    def startCapture(self):
        if not self.capture:
            self.capture = QtCapture(0)
            self.end_button.clicked.connect(self.capture.stop)
            self.capture.setFPS(30)
            self.capture.setParent(self)
            self.capture.setWindowFlags(QtCore.Qt.Tool)
        self.capture.start()
        self.capture.show()

    def endCapture(self):
        self.capture.deleteLater()
        self.capture = None


if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    window = ControlWindow()
    sys.exit(app.exec_())

关于python - 在 PyQt 的主窗口之上显示 OpenCv 窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32226074/

相关文章:

python - Boost Threads 相当于Python 的threading.Event?

python-3.x - 无法将 cv_bridge 与 ROS Kinetic 和 Python3 一起使用

opencv - 如何从单个组件创建 2D 透视变换矩阵?

python - PyQt 自动重复表格

python - 如何使用 mraa 将 lsm9ds1 连接到 respeaker?

python - 如何加速 CSS 文件中的正则表达式 findall

c++ - OpenCV 在 Windows 和 Linux 上的不同结果

python - PyQt 允许枚举值和字符串

python - 在热图中将反向对角线设为白色

python - 无法从 Python 中的对象检查代码