一. 安装OpenCV库:
打开终端,输入以下命令安装OpenCV:
sudo apt-get update
sudo apt-get install libopencv-dev
二. 配置Qt项目
在Qt Creator中打开项目,然后编辑.pro
文件,添加以下内容:
INCLUDEPATH += /usr/include/opencv4
LIBS += -L/usr/lib/x86_64-linux-gnu -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_imgcodecs
CONFIG(use_opencv_4){
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
CV_CONFIG = "opencv4"
}
!isEmpty(CV_CONFIG):{
message("Using OpenCV version $$CV_CONFIG")
PKGCONFIG += $$CV_CONFIG
} else {
message("Neither OpenCV 3 nor OpenCV 4 found - will use OpenCV 3.")
LIBS += -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_imgcodecs
}
三. 测试
#include <opencv2/opencv.hpp>
#include <QImage>
#include <QLabel>
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
cv::Mat img = cv::imread("path_to_image.jpg");
if(img.empty()) {
return -1;
}
cv::cvtColor(img, img, cv::COLOR_BGR2RGB);
QImage qImg = QImage((const unsigned char*)(img.data), img.cols, img.rows, img.step, QImage::Format_RGB888);
QLabel label;
label.setPixmap(QPixmap::fromImage(qImg));
label.show();
return app.exec();
}