CV---Mat转QImage

QImage mat2QImage(const cv::Mat &mat)
{
	switch (mat.type()) {
	case CV_8UC1: {
					  QImage image(mat.cols, mat.rows, QImage::Format_Indexed8);
					  // 设置颜色表 (used to translate colour indexes to qRgb values)
					  image.setColorCount(256);
					  for (int i = 0; i < 256; i++) {
						  image.setColor(i, qRgb(i, i, i));
					  }

					  // 复制数据指针
					  uchar* pSrc = mat.data;
					  for (int row = 0; row < mat.rows; row++) {
						  uchar* pDest = image.scanLine(row);
						  memcpy(pDest, pSrc, mat.cols);
						  pSrc += mat.step;
					  }

					  return image;
	}
	case CV_8UC3: {
					  const uchar* pSrc = (const uchar*)mat.data;
					  QImage image(pSrc, mat.cols, mat.rows, mat.step, QImage::Format_RGB888);
					  return image.rgbSwapped();
	}
	case CV_8UC4: {
					  
					  const uchar* pSrc = (const uchar*)mat.data;
					  QImage image(pSrc, mat.cols, mat.rows, mat.step, QImage::Format_ARGB32);
					  return image.copy();
	}
	default:return QImage();
	}
}

 

你可能感兴趣的:(OPENCV)