【问题标题】:Display a webcam stream in PyQt4 using OpenCV Camera Capture使用 OpenCV Camera Capture 在 PyQt4 中显示网络摄像头流
【发布时间】:2010-06-08 22:45:17
【问题描述】:

我正在使用这个 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()

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

【问题讨论】:

标签: python opencv webcam pyqt4 phonon


【解决方案1】:

看来我们需要在 Python 中绑定这个 C++ 代码:

QImage& cvxCopyIplImage(const IplImage *pIplImage, QImage &qImage)
{
        if(!CV_IS_IMAGE(pIplImage)) return qImage;

        int w = pIplImage->width;
        int h = pIplImage->height;


        if(qImage.width() != w || qImage.height() != h)
        {
                qImage = QImage(w, h, QImage::Format_RGB32);
        }

        int x, y;
        for(x = 0; x < pIplImage->width; ++x)
        {
                for(y = 0; y < pIplImage->height; ++y)
                {
                        CvScalar color = cvGet2D(pIplImage, y, x);

                        if(pIplImage->nChannels == 1)
                        {
                                int v = color.val[0];

                                qImage.setPixel(x, y, qRgb(v,v,v));
                        }
                        else
                        {
                                int r = color.val[2];
                                int g = color.val[1];
                                int b = color.val[0];

                                qImage.setPixel(x, y, qRgb(r,g,b));
                        }
                }
        }

        if(pIplImage->origin != IPL_ORIGIN_TL)
        {
                qImage = qImage.mirrored(false, true);
        }

        return qImage;
}

【讨论】:

  • 我还没有这样做,如果你这样做了,请告诉我:P
【解决方案2】:

我使用下面的代码将 Iplimage 对象转换为 QImage。我花了一些时间来获得正确的格式。 Iplimage是3通道格式,BGR通道顺序,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)

【讨论】:

    【解决方案3】:

    这个对我有用。我使用 Python 2.6.5 在 Linux 上工作:

    import base64
    import Image
    import time
    import urllib2
    import cv
    
    # Basic HTTP Authentication...
    url = 'http://192.168.0.11:82/Videostream.cgi'
    ww = 'Username:Password'
    encodedstring = base64.encodestring(ww)[:-1]
    auth = "Basic %s" % encodedstring
    req = urllib2.Request(url,None, {"Authorization": auth })
    handle = urllib2.urlopen(req)
    
    def read_stream():
        buf = ''
        b = handle.readlines(45)
        for a in b:
            if a.startswith('Content-Length'):
                readlen = str(a).split()[1]
        b1 = handle.read(int(readlen)+4)
        return b1
    
    def test():
        pass
    
    def write_stream():
        imgc = read_stream()
        cv_img = cv.CreateImageHeader((640,480), cv.IPL_DEPTH_8U, 3)
        buf = Image.fromstring('RGB',(640,480),imgc[2:], 'jpeg', 'RGB', None)
        cv.SetData(cv_img, buf.tostring(), (640*3))
        return cv_img
    
    
    fps = 10.0
    
    if __name__ == "__main__":
        while True:
            frame = write_stream()
            cv.ShowImage('Camera', frame)
            k = cv.WaitKey(10)
            time.sleep(int(1.0/fps))
            if k == 0x10001b: # ESC
                cv.DestroyWindow("Camera")
                print 'ESC pressed. Exiting ...'
                break
    

    【讨论】:

    • 实际上这段代码并没有在 Qt Widget 中显示网络摄像头,是吗?
    猜你喜欢
    • 1970-01-01
    • 2016-09-24
    • 2020-03-06
    • 1970-01-01
    • 1970-01-01
    • 2011-02-05
    • 2012-06-16
    • 2012-08-08
    相关资源
    最近更新 更多