使用Qt Creator GUI来播放视频

时间:2012-03-18 19:11:43

标签: c++ c opencv qt-creator

frame = cvQueryFrame(camera);

while(key!='q'){

    cvNamedWindow( "main",CV_WINDOW_AUTOSIZE);
    cvShowImage("main", frame);
    if(frame!=NULL){

        drawSquares( frame, findSquares4( frame, storage ) );

        /* wait for key. Also the function cvWaitKey takes care of event processing */
        key = cvWaitKey(33);
        IplImage *img = cvCloneImage(frame);
        if (img->origin){
            cvFlip(img);
            img->origin= 0;
        }

        QImage* qimg = IplImageToQImage(img)
        // qimg = IplImage2QImage (img);
        QLabel label;
        ui->label->setPixmap(QPixmap::fromImage(qimg));
        cvReleaseImage(&img);

我喜欢用Qt Creator播放视频。我使用上面的代码,但它不行。我收到以下错误:

  

未在此范围内声明IplImageToQImage

有谁知道如何在同一个窗口中使用OpenCV和Qt Creator播放视频?

1 个答案:

答案 0 :(得分:2)

嗯,这是有道理的,因为IplImage2QImage()不是Qt和OpenCV的一部分。

您可能已经看到此功能在Internet上的某处使用并复制/粘贴到您的代码中。

通过Google上的简单搜索,我找到了此功能的the implementation

static QImage IplImage2QImage(const IplImage *iplImage)
{
    int height = iplImage->height;
    int width = iplImage->width;

    if  (iplImage->depth == IPL_DEPTH_8U && iplImage->nChannels == 3)
    {
      const uchar *qImageBuffer = (const uchar*)iplImage->imageData;
      QImage img(qImageBuffer, width, height, QImage::Format_RGB888);
      return img.rgbSwapped();
    } else if  (iplImage->depth == IPL_DEPTH_8U && iplImage->nChannels == 1){
    const uchar *qImageBuffer = (const uchar*)iplImage->imageData;
    QImage img(qImageBuffer, width, height, QImage::Format_Indexed8);

    QVector<QRgb> colorTable;
    for (int i = 0; i < 256; i++){
        colorTable.push_back(qRgb(i, i, i));
    }
    img.setColorTable(colorTable);
    return img;
    }else{
      qWarning() << "Image cannot be converted.";
      return QImage();
    }
}

希望你知道如何处理它。

我写了这个最小的例子来说明如何成功使用IplImage2QImage()。它使用cvLoadImage()从磁盘加载名为test.jpg的文件,然后将其显示在QLabel上。这很简单,而且有效!

#include <cv.h>
#include <highgui.h>

#include <iostream>

#include <QtGui>
#include <QImage>

static QImage IplImage2QImage(const IplImage *iplImage)
{
    int height = iplImage->height;
    int width = iplImage->width;

    if  (iplImage->depth == IPL_DEPTH_8U && iplImage->nChannels == 3)
    {
      const uchar *qImageBuffer = (const uchar*)iplImage->imageData;
      QImage img(qImageBuffer, width, height, QImage::Format_RGB888);
      return img.rgbSwapped();
    }
    else if  (iplImage->depth == IPL_DEPTH_8U && iplImage->nChannels == 1)
    {
        const uchar *qImageBuffer = (const uchar*)iplImage->imageData;
        QImage img(qImageBuffer, width, height, QImage::Format_Indexed8);

        QVector<QRgb> colorTable;
        for (int i = 0; i < 256; i++)
        {
            colorTable.push_back(qRgb(i, i, i));
        }
        img.setColorTable(colorTable);
        return img;
    }
    else
    {
      std::cout << "Image cannot be converted.";
      return QImage();
    }
}

int main(int argc, char** argv)
{
    QApplication app(argc, argv);

    IplImage* img = cvLoadImage("test.jpg", 1);
    if (!img)
    {
        std::cout << "Failed to load test.jpg";
        return -1;
    }

    QImage qt_img = IplImage2QImage(img);

    QLabel label;
    label.setPixmap(QPixmap::fromImage(qt_img));
    label.show();

    return app.exec();
}

在我的Linux机器上,我编译它:

g++ qimage.cpp -o qimage -I/usr/local/include/opencv -I/usr/local/include -I/opt/qt_47x/include -I/opt/qt_47x/include/QtGui -L/usr/local/lib -L/opt/qt_47x/lib -lopencv_core -lopencv_imgproc -lopencv_highgui -lQtCore -lQtGui