前言
上一章节介绍了如何使用 VS2017 和 Qt 来编译 OpenCV,具体请参考:使用VS2017和Qt编译OpenCV库,编译完成之后就可以应用了。
OpenCV 3.0 以后,图像采用Mat格式进行存储,而Qt中图像存储采用的是QImage类,因此如果需要在Qt中显示 OpenCV 处理过程中的图像,需要将Mat类对象转换为QImage类对象。Mat格式采用BGR的存储顺序,而QImage类采用的是RGB存储顺序,因此需要将存储格式从 BGR 转换成 RGB。完成转换后,在Qt界面中显示QImage类对象,可以采用 QLabel 或者 QGraphicsView。
注意:博主编译的OpenCV是64位的,所以编译器需要选用 arm64,否则链接库时会失败;
导入库文件
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11 debug_and_release
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h
FORMS += \
mainwindow.ui
# Default rules for deployment.
qnx: target.path = /tmp/${TARGET}/bin
else: unix:!android: target.path = /opt/${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
INCLUDEPATH += $PWD/OpenCV_msvc2017_64/include
DEPENDPATH += $PWD/OpenCV_msvc2017_64/include
windows {
CONFIG(debug, debug|release) {
LIBS += -L$PWD/OpenCV_msvc2017_64/lib/debug/ -lopencv_calib3d420d
LIBS += -L$PWD/OpenCV_msvc2017_64/lib/debug/ -lopencv_core420d
LIBS += -L$PWD/OpenCV_msvc2017_64/lib/debug/ -lopencv_dnn420d
LIBS += -L$PWD/OpenCV_msvc2017_64/lib/debug/ -lopencv_features2d420d
LIBS += -L$PWD/OpenCV_msvc2017_64/lib/debug/ -lopencv_flann420d
LIBS += -L$PWD/OpenCV_msvc2017_64/lib/debug/ -lopencv_gapi420d
LIBS += -L$PWD/OpenCV_msvc2017_64/lib/debug/ -lopencv_highgui420d
LIBS += -L$PWD/OpenCV_msvc2017_64/lib/debug/ -lopencv_imgcodecs420d
LIBS += -L$PWD/OpenCV_msvc2017_64/lib/debug/ -lopencv_imgproc420d
LIBS += -L$PWD/OpenCV_msvc2017_64/lib/debug/ -lopencv_ml420d
LIBS += -L$PWD/OpenCV_msvc2017_64/lib/debug/ -lopencv_objdetect420d
LIBS += -L$PWD/OpenCV_msvc2017_64/lib/debug/ -lopencv_photo420d
LIBS += -L$PWD/OpenCV_msvc2017_64/lib/debug/ -lopencv_stitching420d
LIBS += -L$PWD/OpenCV_msvc2017_64/lib/debug/ -lopencv_video420d
LIBS += -L$PWD/OpenCV_msvc2017_64/lib/debug/ -lopencv_videoio420d
} else {
LIBS += -L$PWD/OpenCV_msvc2017_64/lib/release/ -lopencv_calib3d420
LIBS += -L$PWD/OpenCV_msvc2017_64/lib/release/ -lopencv_core420
LIBS += -L$PWD/OpenCV_msvc2017_64/lib/release/ -lopencv_dnn420
LIBS += -L$PWD/OpenCV_msvc2017_64/lib/release/ -lopencv_features2d420
LIBS += -L$PWD/OpenCV_msvc2017_64/lib/release/ -lopencv_flann420
LIBS += -L$PWD/OpenCV_msvc2017_64/lib/release/ -lopencv_gapi420
LIBS += -L$PWD/OpenCV_msvc2017_64/lib/release/ -lopencv_highgui420
LIBS += -L$PWD/OpenCV_msvc2017_64/lib/release/ -lopencv_imgcodecs420
LIBS += -L$PWD/OpenCV_msvc2017_64/lib/release/ -lopencv_imgproc420
LIBS += -L$PWD/OpenCV_msvc2017_64/lib/release/ -lopencv_ml420
LIBS += -L$PWD/OpenCV_msvc2017_64/lib/release/ -lopencv_objdetect420
LIBS += -L$PWD/OpenCV_msvc2017_64/lib/release/ -lopencv_photo420
LIBS += -L$PWD/OpenCV_msvc2017_64/lib/release/ -lopencv_stitching420
LIBS += -L$PWD/OpenCV_msvc2017_64/lib/release/ -lopencv_video420
LIBS += -L$PWD/OpenCV_msvc2017_64/lib/release/ -lopencv_videoio420
}
}
显示图片
QT开发交流+赀料君羊:714620761
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <opencv2/opencv.hpp>
using namespace cv;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_showBtn_clicked()
{
QString page_1 = "D:/Qt/MWD/MyOpenCVDemo/1.jpg";
QString page_2 = "D:/Qt/MWD/MyOpenCVDemo/2.jpg";
Mat srcImage_1 = imread(page_1.toLatin1().data()); // 读取图片数据
Mat srcImage_2 = imread(page_2.toLatin1().data());
cvtColor(srcImage_1, srcImage_1, COLOR_BGR2RGB); // 图像格式转换
cvtColor(srcImage_2, srcImage_2, COLOR_BGR2RGB);
QImage disImage_1 = QImage((const unsigned char*)(srcImage_1.data), srcImage_1.cols, srcImage_1.rows, QImage::Format_RGB888);
QImage disImage_2 = QImage((const unsigned char*)(srcImage_2.data), srcImage_2.cols, srcImage_2.rows, QImage::Format_RGB888);
ui->label->setPixmap(QPixmap::fromImage(disImage_1.scaled(ui->label->size(), Qt::KeepAspectRatio))); // label 显示图像
QGraphicsScene *scene = new QGraphicsScene; // graphicsView 显示图像
scene->addPixmap(QPixmap::fromImage(disImage_2));
ui->graphicsView->setScene(scene);
ui->graphicsView->show();
}
运行结果: