opencv的cv::Mat类型与qt的QImage类型互转

void LicenseQRCode::LabelShowImage(const cv::Mat &srcImg, QLabel *label)
{
	if (srcImg.data == NULL || srcImg.empty() || label == NULL)
	{
		return;
	}

	cv::Mat newImg;
	cv::cvtColor(srcImg, newImg, CV_BGR2RGBA);//转换格式

	QImage img = QImage(newImg.data, newImg.cols, newImg.rows, newImg.step, QImage::Format_RGBA8888);
	QImage scaleimg = img.scaled(label->size(),Qt::KeepAspectRatio, Qt::SmoothTransformation);
	label->setPixmap(QPixmap::fromImage(scaleimg));
}

 

// qimage转成cv::Mat
cv::Mat VerifyImage::QImage2cvMat(const QImage &image)
{
	cv::Mat mat;
	switch (image.format())
	{
	case QImage::Format_ARGB32:
	case QImage::Format_RGB32:
	case QImage::Format_ARGB32_Premultiplied:
		mat = cv::Mat(image.height(), image.width(), CV_8UC4, (void*)image.constBits(), image.bytesPerLine());
		break;
	case QImage::Format_RGB888:
		mat = cv::Mat(image.height(), image.width(), CV_8UC3, (void*)image.constBits(), image.bytesPerLine());
		cv::cvtColor(mat, mat, CV_BGR2RGBA);
		break;
	case QImage::Format_Indexed8:
		mat = cv::Mat(image.height(), image.width(), CV_8UC1, (void*)image.constBits(), image.bytesPerLine());
		break;
	}
	return mat;
}


另:

 

QImage 与 cv::Mat 之间的相互转换

http://blog.csdn.net/liyuanbhu/article/details/46662115

 

 

 

QImage类型数据转成base64字符串:

// 通过将qimage保存成qbufer类型再转出base64字符串
QImage img(filepath);
QBuffer buf;
buf.open(QIODevice::WriteOnly);
img.save(&buf, “jpg”);
imgbase64str = buf.data().toBase64();
buf.close();

 

展开阅读全文

没有更多推荐了,返回首页