【问题标题】:How to show a webcam feed in a Qt GUI application using OpenCv?如何使用 OpenCv 在 Qt GUI 应用程序中显示网络摄像头提要?
【发布时间】:2013-08-02 09:10:09
【问题描述】:

我需要在 Qt GUI 应用程序中显示网络摄像头源。我如何使用 OpenCv 做到这一点?从早上开始,我就一直在为这件事头疼。如果有人可以提供示例代码,我将不胜感激。

【问题讨论】:

标签: linux qt opencv webcam


【解决方案1】:

这对我有用:

QImage MatToQImage(const Mat& mat)
{
    // 8-bits unsigned, NO. OF CHANNELS=1
    if(mat.type()==CV_8UC1)
    {
        // Set the color table (used to translate colour indexes to qRgb values)
        QVector<QRgb> colorTable;
        for (int i=0; i<256; i++)
            colorTable.push_back(qRgb(i,i,i));
        // Copy input Mat
        const uchar *qImageBuffer = (const uchar*)mat.data;
        // Create QImage with same dimensions as input Mat
        QImage img(qImageBuffer, mat.cols, mat.rows, mat.step, QImage::Format_Indexed8);
        img.setColorTable(colorTable);
        return img;
    }
    // 8-bits unsigned, NO. OF CHANNELS=3
    if(mat.type()==CV_8UC3)
    {
        // Copy input Mat
        const uchar *qImageBuffer = (const uchar*)mat.data;
        // Create QImage with same dimensions as input Mat
        QImage img(qImageBuffer, mat.cols, mat.rows, mat.step, QImage::Format_RGB888);
        return img.rgbSwapped();
    }
    else
    {
        qDebug() << "ERROR: Mat could not be converted to QImage.";
        return QImage();
    }
} // MatToQImage()
....
// Then use it in main code as follows
// Display frame in main window
frameLabel->setPixmap(QPixmap::fromImage(frame));
....

【讨论】:

    猜你喜欢
    • 2016-09-24
    • 1970-01-01
    • 1970-01-01
    • 2011-02-21
    • 1970-01-01
    • 2017-07-08
    • 2020-03-06
    • 1970-01-01
    • 2012-03-23
    相关资源
    最近更新 更多