Clion+Qt+Opencv详细配置

1、下载opencv

github

这里下载3.4版本的,下载4.0版本容易出错

2、安装opencv

D:\opencv\opencv-3.4.16\opencv

3、CMake构建

3.1 添加源文件和目标文件

image-20230217225022516

3.2 选择mingw构建

image-20230217225102608

3.3 选择c和c++编译工具

image-20230217225201535

D:/Qt/Qt5.14.2/Tools/mingw730_64/bin/gcc.exe

D:/Qt/Qt5.14.2/Tools/mingw730_64/bin/g++.exe

3.4 编译中

image-20230217225322425

3.5 第一次编译完

image-20230217225518739

3.6 添加文件ffpeg

D:\opencv\opencv-3.4.16\opencv\mingw32-build\3rdparty\ffmpeg

复制到:D:\opencv\opencv-3.4.16\opencv\sources.cache\ffmpeg

之后重新configure

3.7 勾选/不勾选

不勾选:

(1)ENABLE_PRECOMPILED_HEADERS
(2)WITH_MSMF
(3)WITH_IPP
(4)WITH_TBB
(5)OPENCV_ENABLE_ALLOCATOR_STATS

勾选:

  1. with_qt
  2. with_opengl
  3. enable_cxx

点击configure

3.8 选择QT文件

QT_QMAKE_EXECUTABLE D:/Qt/Qt5.14.2/5.14.2/mingw73_64/bin/qmake.exe

Qt5Dir D:/Qt/Qt5.14.2/5.14.2/mingw73_64/lib/cmake/Qt5

点击configure

image-20230217230629661

点击configure

3.9 生成

没有红色则生成

image-20230217230715525

4、编译安装

image-20230217230952103

mingw32-make -j8

image-20230217231648933

mingw32-make install

image-20230217231712252

5、配置环境变量

D:\opencv\opencv-3.4.16\opencv\mingw32-build\install\x64\mingw\bin

6、CMakeLists

set(OpenCV_DIR D:/opencv/opencv-3.4.16/opencv/mingw32-build/install)
find_package(OpenCV REQUIRED)
target_link_libraries(qt_demo1 ${OpenCV_LIBS})

image-20230217232627964

7、测试代码

#include "Headers/mainwindow.h"
#include "Forms/ui_MainWindow.h"
#include <opencv2/opencv.hpp>
#include <string>

using namespace std;
using namespace cv;

MainWindow::MainWindow(QWidget *parent) :
        QWidget(parent), ui(new Ui::MainWindow) {
    ui->setupUi(this);
    Mat image = imread("D:\\Desktop\\1.jpg",1);
    namedWindow("Display window",WINDOW_AUTOSIZE);
    imshow("Display window",image);

}

MainWindow::~MainWindow() {
    delete ui;
}

这样写点击运行没有反应,缺少相应的库,讲Qt/mingw下的两个文件移到debug目录(Qt5OpenGL.dll Qt5Test.dll)

image-20230217235531828

再次运行

image-20230217235428500

posted @ 2023-03-07 12:22  摩天仑  阅读(319)  评论(0编辑  收藏  举报