【C++笔记】OpenCV图像Mat格式转换为QT中QImage并显示zai Qlabel上

	Mat rgb;
	QImage imgGray_img;
	if (imgGray.channels() == 1)
	{
		imgGray_img = QImage((const unsigned char*)(imgGray.data), imgGray.cols, imgGray.rows, /*imgGray.cols*imgGray.channels(),*/ QImage::Format_Indexed8);
		QVector<QRgb> colorTable;
		for (int k = 0; k < 256; ++k)
		{
			colorTable.push_back(qRgb(k, k, k));
		}
		imgGray_img.setColorTable(colorTable);
	}
	else
	{
		cv::cvtColor(imgGray, rgb, CV_BGR2RGB);
		imgGray_img = QImage((const unsigned char*)(rgb.data), rgb.cols, rgb.rows, /*rgb.cols*rgb.channels(),*/ QImage::Format_RGB888);
	}
	QImage imgGray_imgShow = imgGray_img.scaled(300, 300, Qt::KeepAspectRatio, Qt::FastTransformation);
	ui.testLabel->setPixmap(QPixmap::fromImage(imgGray_imgShow));

ui.testLabel为显示的Label,区分三通道和已经是erzh二值化图像代码处理方式不同!

猜你喜欢

转载自blog.csdn.net/hellozex/article/details/81144958
今日推荐