gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 01:10:32 26 4
gpt4 key购买 nike

我正在使用这个 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 中绑定(bind)此 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;
}

关于python - 使用 OpenCV Camera Capture 在 PyQt4 中显示网络摄像头流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3001881/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com