Open webcamera with OpenCV and show it with QLabel - white window

I am working on Win7 x64 with the OpenCV and Qt and VS 2010 libraries.

I would like to open my camera using OpenCV, and then show the captured frames using Qt, for example using QLabel, after converting from Mat to QImage.

I want to do this because to use the imshow ("camera", image) and waitKey () functions slow down the camera's streaming.

This is my code:

int main () {
 QApplication a(argc, argv);
 QLabel myLabel;
 VideoCapture cap(0);
 //namedWindow(c"camera", 1);

 for (;;) {

    cap >> image;
        //conversion from Mat to QImage
    Mat dest;
    cvtColor(image, dest,CV_BGR2RGB);
    QImage image1= QImage((uchar*) dest.data, dest.cols, dest.rows, dest.step, QImage::Format_RGB888);

        //show Qimage using QLabel
    myLabel.setPixmap(QPixmap::fromImage(image1));
    myLabel.show();
    //imshow("camera",image);
    //if (waitKey(30)>= 0)  break;
 }
return a.exec();
}   

The webcam opens correctly and works, but I see a white window, not captured frames, as you can see in this image enter image description here

If I uncomment: namedWindow (..), imshow(..), if(waitKey(..)it works (I see two windows with the same images), but I show captured frames from OpenCV, and I want to avoid this.

: - ? , Mat Qimage ??.. Qt?

!

+5
3

, , :

 for (;;) {

    cap >> image;
        //conversion from Mat to QImage
    Mat dest;
    cvtColor(image, dest,CV_BGR2RGB);
    QImage image1= QImage((uchar*) dest.data, dest.cols, dest.rows, dest.step, QImage::Format_RGB888);

        //show Qimage using QLabel
    myLabel.setPixmap(QPixmap::fromImage(image1));
    myLabel.show();
    //imshow("camera",image);
    //if (waitKey(30)>= 0)  break;
 }

- QLabel , . , waitKey , , QImage, - .

, a.exec() , , , , .

, QTimer x :

 class VideoWindow: public QWidget
 {
    Q_OBJECT
    public:
        VideoWindow(QWidget* parent = 0): QWidget(parent), cap(0)
        {
            timer = new QTimer(this);
            connect(timer, SIGNAL(timeout()), this, SLOT(updatePicture()));
            timer->start(20);
        }


    public slots:
        void updatePicture()
        {
            cap >> image;
            //conversion from Mat to QImage
            Mat dest;
            cvtColor(image, dest,CV_BGR2RGB);
            QImage image1 = QImage((uchar*) dest.data, dest.cols, dest.rows, dest.step, QImage::Format_RGB888);

            //show Qimage using QLabel
            setPixmap(QPixmap::fromImage(image1));
        }

    private:
        QTimer * timer;
        VideoCapture cap;
};

int main(int argc, char** argv)
{
    QApplication app(argc, argv);
    VideoWindow w;
    w.show();

    app.exec();
    return 0;   
}
+6

paintEvent() (/mainwindow), .

VideoCapture myVideo;
myVideo.open(0);
if(!myVideo.isOpened())
   cout<<"CANNOT OPEN CAMERA"<<endl; //or you can put some error message

paintEvent()

myVideo >> frame;
QImage image = QImage((const unsigned char*)frame.data,frame.cols,
               frame.rows,frame.step,QImage::Format_RGB888);

QRectF target(0.0,0.0,image.width(),image.height());
QRectF source(0.0,0.0,image.width(),image.height());
QPainter painter(this);
painter.drawImage(target,image,source)

, timeout SIGNAL update SLOT . 20-40 . , , clicked SLOT

QTimer *timer = new QTimer;
timer.setInterval(20);
connect(timer,SIGNAL(timeout()),this,SLOT(update()));
+1

Capture Video Label, , CPP: , , .

void <Class name Here>::on_button_clicked(){
captureVideoInLabel.open(0);
    if (captureVideoInLabel.isOpened() == false)
    {
        qDebug() << "Camera can't open";
        return;
    }
    timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, 
SLOT(processFrameAndUpdateGUI()));
    timer->start(20);
}

void <here you need to write class name>::processFrameAndUpdateGUI()
{
    Mat originalImage;
    captureVideoInLabel.read(originalImage);

    if (originalImage.empty() == true)
    {
        return;
    }

    QImage qOriginalImage((uchar*)originalImage.data, originalImage.cols, 
    originalImage.rows, originalImage.step, QImage::Format_RGB888);
    ui->label->setPixmap(QPixmap::fromImage(qOriginalImage));
}

:

private slots:
    void processFrameAndUpdateGUI();
    void on_button_clicked();
private:
    cv::VideoCapture captureVideoInLabel;
0

All Articles