Qt + OpenCV - 在QLabel上显示图像

Jay*_*nki 3 qt multithreading opencv

我正试图从相机捕捉实时视图,并重定向它以显示在QLabel上.但只有一半的观点出现(见下文): 截图

左侧窗口使用cv :: imshow()显示,完美运行.我在另一个线程中捕获Mat,然后以Qimage作为参数发出信号,然后将图像设置为插槽中的QLabel.

这是代码:

while(true){
    cam >> mat;
    cv::imshow("name",mat);
    emit send_UIupdate(mat2qimage(mat));
}
Run Code Online (Sandbox Code Playgroud)

并在插槽中将图像设置为Qlabel:

void Dialog::updateUI(const QImage &img){
    label->setPixmap(QPixmap::fromImage(img));
}
Run Code Online (Sandbox Code Playgroud)

使用下面的方法将Mat转换为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);
}
Run Code Online (Sandbox Code Playgroud)

有什么建议,解决这个问题??

CTZ*_*tef 8

你可以试试这个:

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

我用QTimer称呼它.我是从那里得到的:http://code.google.com/p/qt-opencv-multithreaded/

希望这有帮助.