与Qt Creator的GUI播放视频 [英] GUI with Qt Creator to play video

查看:181
本文介绍了与Qt Creator的GUI播放视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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的播放视频。我用的是code以上,但它不是好的。我收到以下错误:

I like to play video with Qt Creator. I use the code above, but it's not okay. I get the following error:

IplImageToQImage不是在这个范围内声明

IplImageToQImage was not declared in this scope

有谁知道我可以在同一个窗口中播放视频的OpenCV和Qt Creator?

Does anyone know how I can play video with OpenCV and Qt Creator in the same window?

推荐答案

好了,这是有道理的,因为 IplImage2QImage()不是Qt的,也不OpenCV中的一部分。

Well, it makes sense since IplImage2QImage() is not a part of Qt nor OpenCV.

您可能看到某处正在使用的上网本功能,并复制/粘贴到你的code。

You probably saw this function being used somewhere on the Internet and copied/pasted into your code.

随着谷歌一个简单的搜索,我发现实施这个功能:

With a simple search on Google I found the implementation of this function:

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();
    }
}

希望你会知道该怎么做。

Hopefully you'll know what to do with it.

我写了这个小例子来说明如何成功地使用 IplImage2QImage()。它采用 cvLoadImage()加载一个名为从磁盘test.jpg放在文件,然后将其显示在的QLabel。很简单,和它的作品!

I wrote this minimal example to show how to successfully use IplImage2QImage(). It uses cvLoadImage() to load a file named test.jpg from the disk and then displays it on a QLabel. It's simple, and it works!

#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机器,我编译它:

On my Linux box, I compiled it with:

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

这篇关于与Qt Creator的GUI播放视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆