简体   繁体   English

从cvMat到QImage

[英]from cvMat to QImage

i want to convert an image from cvMat type to Qimage , with my actual code the application does not work; 我想将图像从cvMat类型转换为Qimage,我的实际代码应用程序不起作用; i have did it the other way (Qimage to Mat it work fine) please tell me what is going on wrong with my code 我已经做了另一种方式(Qimage到Mat它工作正常)请告诉我我的代码出了什么问题

here is my code for Qimage to Mat : 这是我的Qimage to Mat的代码:

Mat qimage2mat(const QImage& qimage) {
    cv::Mat mat = cv::Mat(qimage.height(), qimage.width(), CV_8UC4, (uchar*)qimage.bits(), qimage.bytesPerLine());
    cv::Mat mat2 = cv::Mat(mat.rows, mat.cols, CV_8UC3 );
    int from_to[] = { 0,0,  1,1,  2,2 };
    cv::mixChannels( &mat, 1, &mat2, 1, from_to, 3 );
    return mat2;
}

and here is my code for Mat to Qimage 这是我的Mat to Qimage的代码

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

thank you by advance 提前谢谢你

as @SpamBot mentioned, the data is freed when Mat goes out of scope. 正如@SpamBot所提到的,当Mat超出范围时,数据被释放。 try deep copying the data: 尝试深度复制数据:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM