简体   繁体   中英

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. However ui does not start. After I debug my code I see that in while(true) it always looping but ui->lblProcessedVideo->setPixmap..... command does not shows any 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

Like Ezee said you need to delegate capturing image from camera to separate thread, next send image to GUI thread. heres sample code:

//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::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

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

signals:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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