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