2015-08-26 151 views
3

的頂部我已經從視頻處理輸入流,並顯示經處理的輸出一個OpenCV的項目。我已經使用PyQt按鈕從一個輸出切換到另一個。我的PyQt的窗口幾乎covvers整個屏幕,當我在我的按鈕點擊,OpenCV的窗口保持PyQt的窗口後面。另外,我已經將PyQt的主窗口作爲我的父窗口。我怎樣才能把PyCt窗口上的opencv窗口。我搜索了cvGetWindowHandle(),但沒有發現它是python的實現。顯示OpenCV的窗口上的PyQt的主窗口

我已經使用了PyQt4和opencv2,並且PyQt窗口沒有使用QtDesigner設計。

+0

你能嘗試一些技巧[這裏] (http://stackoverflow.com/questions/6312627/windows-7-how-to-bring-a-window-to-the-front-no-matter-what-other-window-has-fo)?我也爲此付出了努力,但這些解決方案最終爲我工作。 – KobeJohn

回答

3

您可以隨時在Qt部件包OpenCV窗口...

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(1) 
      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_()) 
+0

謝謝你的帖子。我已經包裝了我的OpenCV窗口在Qt小部件,它工作正常:) – user5106036