不能跑偏了,虽然unity3d,blender还有刚了解的vulKan(下一代openGL)我还是很好奇的。偶尔可以随意玩玩,大好时光还是以图像算法及数学学习为主。所以先回到opencv。之前python及VS2017都已经玩过opencv了。QT Creator也是一样的。
一,QT使用opencv库
- 打开Qt,新建一个项目,打开.pro 文件,在里面添加:
INCLUDEPATH+=D:\opencv341_vc\opencv\build\include
D:\opencv341_vc\opencv\build\include\opencv
D:\opencv341_vc\opencv\build\include\opencv2
win32:CONFIG(debug, debug|release): {
LIBS += -LD:\opencv341_vc\opencv\build\x64\vc15\lib -lopencv_world341d
}
else:win32:CONFIG(release, debug|release): {
LIBS += -LD:\opencv341_vc\opencv\build\x64\vc15\lib -lopencv_world341
}
- 然后在main函数运行关键代码,试下
#include <opencv2/core/core.hpp>
#include <opencv2/highgui.hpp>
cv::Mat image = cv::imread("D:\\qtpro\\picdata\\tst.jpg", 1);
// create image window named "My Image"
cv::namedWindow("My Image");
// show the image on window
cv::imshow("My Image", image);
漂亮的图片显示出来了,环境搭建ok。有了之前的基础,用到QT上,so easy呀!
二,把opencv的图像显示嵌入到QT中
用了viewgraph,opencv是mat,不过qt是qImage,所以要转,另外opencv是BGR所以也找转为RGB。
三,代码
void MainWindow::on_pushButton_clicked()
{
QString fileName = QFileDialog::getOpenFileName(this,tr("Open Image"),".",tr("Image File(*.png *.jpg *.jpeg *.bmp)"));
if (fileName.isEmpty())
{
return;
}
cv::Mat srcImage = cv::imread(fileName.toUtf8().data());//读取图片数据
cv::cvtColor(srcImage, srcImage,CV_BGR2RGB);//图像格式转换
QImage disImage = QImage((const unsigned char*)(srcImage.data),srcImage.cols,srcImage.rows,QImage::Format_RGB888);
QGraphicsScene *scene = new QGraphicsScene;//图像显示
scene->addPixmap(QPixmap::fromImage(disImage));
ui->graphicsView->setScene(scene);
ui->graphicsView->show();
cv::Mat image = cv::imread("D:\\qtpro\\picdata\\tst.jpg", 1);
// create image window named "My Image"
cv::namedWindow("My Image");
// show the image on window
cv::imshow("My Image", image);
}
网友评论