代码如下:
之前死活就是没有Mat,后来发现是因为路径里面有中文!!!!
切记切记!!!改了两天居然因为这种错误,我服了!!!
//打开图片
void Test01::on_open_clicked() {
QString filename;
filename = QFileDialog::getOpenFileName(this,
tr("选择图像"),
"",
tr("Images (*.png *.bmp *.jpg *.tif *.GIF )"));
if (filename.isEmpty())
{
return;
}
else
{
QImage* img2 = new QImage;
if (!(img2->load(filename))) //加载图像
{
QMessageBox::information(this,
tr("打开图像失败"),
tr("打开图像失败!"));
delete img2;
return;
}
//QImage i = *img2;
//image = QImage2cvMat(i);
string str = filename.toStdString(); // 将filename转变为string类型;
image = imread(str);//读取图片
QString qstr = QString::fromStdString(str);
if (image.empty()) {
ui.label_4->setText("open error");
ui.label_3->setText(qstr);
}
*img2 = img2->scaled(img2->width() / 3, img2->height() / 3, Qt::KeepAspectRatio);
QGraphicsScene *scene = new QGraphicsScene;
scene->addPixmap(QPixmap::fromImage(*img2));
ui.graphicsView->setScene(scene);
ui.graphicsView->resize(img2->width() + 10, img2->height() + 10);
ui.graphicsView->show();
//Mat image2=image.clone();
//QImage img = MatToQImage(image2);//转化为QImage
// QGraphicsScene *scene = new QGraphicsScene;
// scene->addPixmap(QPixmap::fromImage(img));
//label = new QLabel();
//label->setPixmap(QPixmap::fromImage(img));
//label->resize(QSize(img.width(), img.height()));
//ui.scrollArea->setWidget(label);
}
}