Qt + OpenCV - Displaying images on QLabel

18,086

Solution 1

you can try this :

QImage MainWindow::putImage(const Mat& mat)
{
    // 8-bits unsigned, NO. OF CHANNELS=1
    if(mat.type()==CV_8UC1)
    {
        // Set the color table (used to translate colour indexes to qRgb values)
        QVector<QRgb> colorTable;
        for (int i=0; i<256; i++)
            colorTable.push_back(qRgb(i,i,i));
        // Copy input Mat
        const uchar *qImageBuffer = (const uchar*)mat.data;
        // Create QImage with same dimensions as input Mat
        QImage img(qImageBuffer, mat.cols, mat.rows, mat.step, QImage::Format_Indexed8);
        img.setColorTable(colorTable);
        return img;
    }
    // 8-bits unsigned, NO. OF CHANNELS=3
    if(mat.type()==CV_8UC3)
    {
        // Copy input Mat
        const uchar *qImageBuffer = (const uchar*)mat.data;
        // Create QImage with same dimensions as input Mat
        QImage img(qImageBuffer, mat.cols, mat.rows, mat.step, QImage::Format_RGB888);
        return img.rgbSwapped();
    }
    else
    {
        qDebug() << "ERROR: Mat could not be converted to QImage.";
        return QImage();
    }
}

I call it with a QTimer. I got it from there : http://code.google.com/p/qt-opencv-multithreaded/

Hope this help.

Solution 2

VideoCapture cap("video.avi");
Mat frame;
QImage img;
QPixmap pixel;
while(cap.isOpened())
{
    cap >> frame;
    img= QImage((uchar*) frame.data, frame.cols, frame.rows, frame2.step, QImage::Format_RGB888);
    pixel = QPixmap::fromImage(img);
    ui->label->setPixmap(pixel);
}    
Share:
18,086
Jaydeep Solanki
Author by

Jaydeep Solanki

I'm a developer at heart with experience in both backend (Go | Node.js) and frontend (React.js) development, along with a keen interest in DevOps. I'm also a big believer in clean code &amp; automated testing! Also, checkout a few of the side-projects I work on in my free time (jaydp.com/resume#side-projects) You can learn more about me @ jaydp.com

Updated on June 27, 2022

Comments

  • Jaydeep Solanki
    Jaydeep Solanki almost 2 years

    I'm trying to capture live view from camera, and redirecting it to show up on QLabel. But only half view comes (see below): Screenshot

    The left hand side window, is shown using cv::imshow(), which works perfectly. I'm capturing the Mat in a different thread, and then emitting a signal with a Qimage as a parameter, and then setting the image to the QLabel in the slot.

    here's the code:

    while(true){
        cam >> mat;
        cv::imshow("name",mat);
        emit send_UIupdate(mat2qimage(mat));
    }
    

    and in the slot setting the image to Qlabel:

    void Dialog::updateUI(const QImage &img){
        label->setPixmap(QPixmap::fromImage(img));
    }
    

    using the below to convert Mat to QImage:

    QImage camera::mat2qimage(const cv::Mat& mat) {
        cv::Mat rgb;
        cv::cvtColor(mat, rgb, CV_BGR2RGB);
        return QImage((const unsigned char*)(rgb.data), rgb.cols, rgb.rows, QImage::Format_RGB888);
    }
    

    Any suggestions, to solve this problem ??