简体   繁体   English

如何在QLabel中显示捕获图像

[英]How to show capture image in QLabel

I am trying to display live camera image to Qlabel.. When I start code it does not give any error and my camera light goes to blue which means working. 我正在尝试将实时摄像机图像显示到Qlabel。当我启动代码时,它没有给出任何错误,并且我的摄像机指示灯变为蓝色,这意味着工作正常。 However ui does not start. 但是ui无法启动。 After I debug my code I see that in while(true) it always looping but ui->lblProcessedVideo->setPixmap..... command does not shows any ui. 调试代码后,我发现在while(true)它总是循环运行,但是ui->lblProcessedVideo->setPixmap.....命令未显示任何ui。

Could you please kindly show me my mistake.. 请您告诉我我的错误。

Here is my partial code: 这是我的部分代码:

void MainWindow::getImageFromVideo()
{
    CvCapture* capture;
    cv::Mat frame;
    cv::Mat gray_frame;

    capture = cvCaptureFromCAM( 0 );

    if( capture )
    {
        while( true )
        {
            frame = cvQueryFrame( capture );

            if( !frame.empty() )
            {
                cvtColor( frame, gray_frame, CV_BGR2GRAY);

                equalizeHist( gray_frame, gray_frame );

                ui->lblProcessedVideo->setPixmap( QPixmap::fromImage( Mat2QImage( frame )));
            }
        }
    }
}

EDIT: Mat2QImage() is a function which convert Mat to QImage 编辑: Mat2QImage()是将Mat转换为QImage的函数

Like Ezee said you need to delegate capturing image from camera to separate thread, next send image to GUI thread. 就像Ezee所说的那样,您需要将从摄像机捕获的图像委托给单独的线程,然后将图像发送到GUI线程。 heres sample code: 这是示例代码:

//timer.h //timer.h

class Timer : public QThread
{
    Q_OBJECT
public:
    explicit Timer(QObject *parent = 0);
    void run();
signals:
    void updFrame(QPixmap);
public slots:

};

//timer.cpp //timer.cpp

Timer::Timer(QObject *parent) :
    QThread(parent)
{
}

void Timer::run()  {
    VideoCapture cap(0); // open the default camera
    for(;;){
        Mat frame;
        cap.read(frame);
        QPixmap pix = QPixmap::fromImage(IMUtils::Mat2QImage(frame));
        emit updFrame(pix);
        if( waitKey (30) >= 0){
            break;
        }
    }
}

//videoviewer.h //videoviewer.h

class VideoViewer : public QLabel
{
    Q_OBJECT
public:
    explicit VideoViewer(QObject *parent = 0);

signals:

public slots:
    void updateImage(QPixmap pix);
};

//videoviever.cpp //videoviever.cpp

VideoViewer::VideoViewer(QObject *parent) :
    QLabel()
{
    Timer* timer = new Timer();
    connect(timer,SIGNAL(updFrame(QPixmap)),this,SLOT(updateImage(QPixmap)));
    timer->start();
}

void VideoViewer::updateImage(QPixmap pix){
    this->setPixmap(pix);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM