无误的Mat2QImage代码

QImage MainWindow::Mat2QImage(const cv::Mat& InputMat)
{

    cv::Mat TmpMat;


    // convert the color space to RGB

    if (InputMat.channels() == 1)

    {

        cv::cvtColor(InputMat, TmpMat, CV_GRAY2RGB);

        Result=QImage((const uchar*)(TmpMat.data), TmpMat.cols, TmpMat.rows,TmpMat.cols*TmpMat.channels(),
                      QImage::Format_Indexed8);

    }

    else

    {

        cv::cvtColor(InputMat, TmpMat, CV_BGR2RGB);
        // construct the QImage using the data of the mat, while do not copy the data

        Result = QImage((const uchar*)(TmpMat.data), TmpMat.cols, TmpMat.rows,TmpMat.cols*TmpMat.channels(),

                               QImage::Format_RGB888);


    }





    // deep copy the data from mat to QImage

    Result.bits();


    return Result;


}

原文地址:https://www.cnblogs.com/muyangshaonian/p/9650491.html