QImage->cvMat:
Mat CameraThread::QImageTocvMat(QImage img)
{
Mat ToCvMat;
switch (img.format())
{
case QImage::Format_RGB888:
ToCvMat = Mat(img.height(), img.width(), CV_8UC3, (void*)img.bits(), img.bytesPerLine());
cv::cvtColor(ToCvMat, ToCvMat, CV_BGR2RGB);
break;
case QImage::Format_ARGB32:
case QImage::Format_RGB32:
case QImage::Format_ARGB32_Premultiplied:
ToCvMat = Mat(img.height(), img.width(), CV_8UC4, (void*)img.bits(), img.bytesPerLine());
break;
case QImage::Format_Indexed8:
ToCvMat = Mat(img.height(), img.width(), CV_8UC1, (void*)img.bits(), img.bytesPerLine());
break;
}
return ToCvMat;
}
cvMat->QImage:
QImage CameraThread::cvMat2QImage(const cv::Mat& cvImg)
{
QImage qImg;
if (cvImg.channels() == 3) //三通道彩色图像
{
//CV_BGR2RGB
cv::cvtColor(cvImg, cvImg, CV_RGB2BGR);
qImg = QImage((const unsigned char*)(cvImg.data), cvImg.cols, cvImg.rows, cvImg.cols*cvImg.channels(), QImage::Format_RGB888);
}
else if (cvImg.channels() == 1) //单通道(灰度图)
{
qImg = QImage((const unsigned char*)(cvImg.data), cvImg.cols, cvImg.rows, cvImg.cols*cvImg.channels(), QImage::Format_Indexed8);
QVector<QRgb> colorTable;
for (int k = 0; k<256; ++k)
{
colorTable.push_back(qRgb(k, k, k));
}
qImg.setColorTable(colorTable);//把qImg的颜色按像素点的颜色给设置
}
else
{
qImg = QImage((const unsigned char*)(cvImg.data), cvImg.cols, cvImg.rows, cvImg.cols*cvImg.channels(), QImage::Format_RGB888);
}
return qImg;
}
QPixmap->QImage:
QPixmap pixmap;
QImage image=pixmap.toImage();
QImage->QPixmap:
QImage image;
QPixmap pixmap=QPixmap::fromImage(image);
原文:https://blog.csdn.net/weixin_39485901/article/details/88078406