QImage 转 Mat
Mat QImage2Mat(QImage &img) { cv::Mat mat; switch (img.format()) { case QImage::Format_RGB32: //一般Qt读入彩色图后为此格式 mat = cv::Mat(img.height(), img.width(), CV_8UC4, (void*)img.constBits(), img.bytesPerLine()); cv::cvtColor(mat,mat,cv::COLOR_BGRA2BGR); //转3通道 break; case QImage::Format_RGB888: mat = cv::Mat(img.height(), img.width(), CV_8UC3, (void*)img.constBits(), img.bytesPerLine()); cv::cvtColor(mat,mat,cv::COLOR_RGB2BGR); break; case QImage::Format_Indexed8: mat = cv::Mat(img.height(), img.width(), CV_8UC1, (void*)img.constBits(), img.bytesPerLine()); break; } return mat; }
Mat 转 QImage
QImage Mat2QImage(Mat &img) { if(img.type()==CV_8UC1 || img.type()==CV_8U) { QImage image((const uchar *)img.data, img.cols, img.rows, img.step, QImage::Format_Grayscale8); return image; } else if(img.type()==CV_8UC3) { QImage image((const uchar *)img.data, img.cols, img.rows, img.step, QImage::Format_RGB888); return image.rgbSwapped(); } }
转:https://www.cnblogs.com/zzzsj/p/16511888.html
标签:Mat,img,CV,互转,cv,QImage,mat From: https://www.cnblogs.com/shiyixirui/p/18123854