使用OpenCV Camera Capture在PyQt4中显示网络摄像头流

Nat*_*tim 5 python webcam opencv phonon pyqt4

我正在使用此Python脚本来显示我的网络摄像头:

from opencv.cv import *  
from opencv.highgui import *  

import sys

cvNamedWindow("w1", CV_WINDOW_AUTOSIZE)
camera_index = 0
capture = cvCreateCameraCapture(camera_index)

def repeat():
    global capture #declare as globals since we are assigning to them now
    global camera_index
    frame = cvQueryFrame(capture)
    cvShowImage("w1", frame)
    c = cvWaitKey(10)

    if c == "q":
        sys.exit(0)

if __name__ == "__main__":
    while True:
        repeat()
Run Code Online (Sandbox Code Playgroud)

它工作得很好,但我想在我的Qt应用程序中设置这个显示.如何将IplImageOpenCV图像用于Qt VideoWidget

Wil*_*ias 2

我使用下面的代码将 Iplimage 对象转换为 QImage。我花了一些时间才获得正确的格式。Iplimage 是具有 BGR 通道顺序的 3 通道格式,而 QImage 使用 RGB 通道顺序。

camcapture = cv.CaptureFromCAM(0)       
cv.SetCaptureProperty(camcapture,cv.CV_CAP_PROP_FRAME_WIDTH, 1280)
cv.SetCaptureProperty(camcapture,cv.CV_CAP_PROP_FRAME_HEIGHT, 720);

frame = cv.QueryFrame(camcapture)
image = QImage(frame.tostring(), frame.width, frame.height, QImage.Format_RGB888).rgbSwapped()
pixmap = QPixmap.fromImage(image)
Run Code Online (Sandbox Code Playgroud)