废话不多说,在界面拖拽一个graphicsView
,代码片如下:
1. 头文件中声明QGraphicsScene
#include <QtGui/qpixmap.h>
QGraphicsScene* originalScene;
originalScene = new QGraphicsScene(this);
2. 控件关联
ui.graphicsView->setScene(originalScene);
3.显示QImage
scene->clear();
QImage qimage = mat2qim(cvImage); // cvImage(cv::Mat)
qimage = qimage.scaled(ui.graphicsView->width()-DIFF, ui.graphicsView->height()-DIFF, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
scene->addPixmap(QPixmap::fromImage(qimage));
ui.graphicsView->show();
4. cv::Mat -> QImage 转换函数
QImage mat2qim(Mat & mat)
{
cvtColor(mat, mat, COLOR_BGR2RGB);
QImage qim((const unsigned char*)mat.data, mat.cols, mat.rows, mat.step,
QImage::Format_RGB888);
return qim;
}
5. QImage -> cv::Mat 转换函数
Mat qim2mat(QImage & qim)
{
Mat mat;
switch(qim.format()){
case QImage::Format_RGB888:
mat = Mat(qim.height(), qim.width(),
CV_8UC3,(void*)qim.constBits(),qim.bytesPerLine());
break;
case QImage::Format_ARGB32_Premultiplied:
mat = Mat(qim.height(), qim.width(),
CV_8UC4,(void*)qim.constBits(),qim.bytesPerLine());
break;
}
return mat;
}