/** * @brief Mat2QImage Convert the cv::Mat to QImage while the cv::Mat is in BGR * color space or gray. * @param InputMat The mat used to be converted. * @return The QImage which deep copy the data of the cv::Mat. * * @author sheng * @date 2015-03-31 * @version 0.1 * * @history * <author> <date> <version> <description> * sheng 2015-03-31 0.1 build the function * */ QImage Mat2QImage(const cv::Mat& InputMat) { cv::Mat TmpMat; // convert the color space to RGB if (InputMat.channels() == 1) { cv::cvtColor(InputMat, TmpMat, CV_GRAY2RGB); } else { cv::cvtColor(InputMat, TmpMat, CV_BGR2RGB); } // construct the QImage using the data of the mat, while do not copy the data QImage Result = QImage((const uchar*)(TmpMat.data), TmpMat.cols, TmpMat.rows, QImage::Format_RGB888); // deep copy the data from mat to QImage Result.bits(); return Result; }
原文地址:http://blog.csdn.net/sheng_ai/article/details/44765065