Qt 编程指南10 QImage Mat QPixmap转换

 
//示例  pushButtonOpenPicBig按钮clicked单击动作触发
void  Qt_Window::on_pushButtonOpenPicBig_clicked()
{
        string filename =GetFileName() ; // 获取图像路径

	Mat picMat= imread(filename, 1);  // 读取保存为opencv Mat类图像       
	
	QPixmap picQPixmap =  MatToQT(picMat); //opencv Mat类图像转换QT图像
		//Text Browser QT控件 显示图像 
		ui.labelShow->setPixmap(picQPixmap);
		//设置标签的新大小,与像素图一样大
		ui.labelShow->setGeometry(picQPixmap.rect());

		waitKey();
	
}

//功能:以对话框的打开的模式获取图片目录
//输入:  无
//输出 :  字符型 路径
string GetFileName(){
QString strFileName;  //文件名
strFileName = QFileDialog::getOpenFileName(this, tr("打开静态图片"), "",
"Pictures (*.bmp *.jpg *.jpeg *.png *.xpm);;All files(*)");

if (strFileName.isEmpty())
{
//文件名为空,返回
return;
}

QTextCodec *code = QTextCodec::codecForName("GB2312");//解决中文路径问题  
string filename = code->fromUnicode(strFileName).data();
return filename;
}
// 功能: 将Mat图像 转换为 QT 的图像
//输入: Mat opencv类图像
//输出: QPixmap QT类图像
QPixmap MatToQT(Mat picMat){

QImage picQImage;
QPixmap picQPixmap;

cvtColor(picMat, picMat, CV_BGR2RGB);//三通道图片需bgr翻转成rgb

picQImage = QImage((uchar*)picMat.data, picMat.cols, picMat.rows, QImage::Format_RGB888);

picQPixmap = QPixmap::fromImage(picQImage);
return picQPixmap;
}

 

  

原文地址:https://www.cnblogs.com/kekeoutlook/p/7507182.html