当前位置: 首页>编程语言>正文

QT 配置opencv undefined reference qt5配置opencv

安装配置


Windows10+Qt5.11+OpenCV3.3+MinGW+CMake3.13

下载和安装Qt


下载链接:Index of /archive/qt或者 http://download.qt.io/official_releases/qt/

QT 配置opencv undefined reference qt5配置opencv,QT 配置opencv undefined reference qt5配置opencv_qt,第1张

同时也可以到Qt官方网站去下载,只是需要注册一个账户,注册了就可以下载一个开源的Qt

官方下载链接:Qt | Cross-platform software development for embedded & desktop

 安装教程:

下载OpenCV


下载链接:Home - OpenCV

下载的应该是个exe文件,点击exe文件进行解压,得到一个opencv文件夹,包含build和sources。

下载和安装CMake


下载链接:Download | CMake

QT 配置opencv undefined reference qt5配置opencv,QT 配置opencv undefined reference qt5配置opencv_opencv_02,第2张

如果操作系统是64位的就选上面两个,如果操作系统是32位的就选下面两个,可以选择下载msi格式的,不需要解压,直接安装就行了。

添加系统环境变量


【我的电脑】->右键->【属性】->【高级系统设置】->【环境变量】->【系统变量】->找到【Path】

然后如下图所示添加bin:

QT 配置opencv undefined reference qt5配置opencv,QT 配置opencv undefined reference qt5配置opencv_qt_03,第3张

添加完了之后,最好是重启一下电脑,环境变量生效。 

编译OpenCV源码


在OpenCV目录下新建一个QtBuild空文件夹,用于保存编译好的文件。找到CMake的安装路径(参考D:\CMake\bin\cmake-gui.exe),点击运行cmake-gui.exe,打开CMake,用CMake编译OpenCV源码,分别设置OpenCV源码目录和build目录,设置完成后点击Configure

QT 配置opencv undefined reference qt5配置opencv,QT 配置opencv undefined reference qt5配置opencv_opencv_04,第4张

QT 配置opencv undefined reference qt5配置opencv,QT 配置opencv undefined reference qt5配置opencv_Qt_05,第5张

configure过程中需要下载opencv_ffmpeg.dll ,此时需要联网,可能下载有点慢。Configure完成后会出现很多红色的错误,勾选上WITH QT和WITH OPENGL,再次Configure,直到没有红色的错误为止,点击Generate生成编译文件。

安装编译文件


按win+X,选择如下,进入命令行界面,输入以下命令,先进入QtBuild目录,然后输入mingw32-make install进行安装。

PS C:\> d:
PS D:\> cd OpenCV3.3\QtBuild
PS D:\OpenCV3.3\QtBuild> mingw32-make install

QT 配置opencv undefined reference qt5配置opencv,QT 配置opencv undefined reference qt5配置opencv_qt_06,第6张

安装过程中可能会出现以下问题:

1、... windres.exe: unknown option -- W ...

解决方法:需要在CMake中取消勾选ENABLE_PRECOMPILED_HEADERS

2、opencv/sources/modules/videoio/src/cap_dshow.cpp

 解决方法:打开opencv/sources/modules/videoio/src/cap_dshow.cpp 文件,在头文件#include "DShow.h" 前添加 #define NO_DSHOW_STRSAF(如果报错,再往前放)

也可以添加这个试试,有些接口可能过时了#define STRSAFE_NO_DEPRECATE

以上只是通常出现的情况,其他情况请看Qt官网的帮助文档

如果不使用opencv_python,需要在CMake界面取消勾选BUILD_opencv_python以及其他和 opencv_python有关的选项,否则编译不通过。

新建Qt项目,在pro文件添加如下内容,如有路径不同,请稍做修改:

INCLUDEPATH += D:/OpenCV3.3/opencv/build/include
LIBS += D:\OpenCV3.3\QtBuild\install\x86\mingw\lib\libopencv_*.a

或者

INCLUDEPATH += D:/Qt5.11install/opencv/build/include

LIBS += D:\Qt5.11install\MinGW\bin\libopencv_calib3d340.dll
LIBS += D:\Qt5.11install\MinGW\bin\libopencv_core340.dll
LIBS += D:\Qt5.11install\MinGW\bin\libopencv_dnn340.dll
LIBS += D:\Qt5.11install\MinGW\bin\libopencv_features2d340.dll
LIBS += D:\Qt5.11install\MinGW\bin\libopencv_flann340.dll
LIBS += D:\Qt5.11install\MinGW\bin\libopencv_highgui340.dll
LIBS += D:\Qt5.11install\MinGW\bin\libopencv_imgcodecs340.dll
LIBS += D:\Qt5.11install\MinGW\bin\libopencv_imgproc340.dll
LIBS += D:\Qt5.11install\MinGW\bin\libopencv_ml340.dll
LIBS += D:\Qt5.11install\MinGW\bin\libopencv_objdetect340.dll
LIBS += D:\Qt5.11install\MinGW\bin\libopencv_photo340.dll
LIBS += D:\Qt5.11install\MinGW\bin\libopencv_shape340.dll
LIBS += D:\Qt5.11install\MinGW\bin\libopencv_stitching340.dll
LIBS += D:\Qt5.11install\MinGW\bin\libopencv_superres340.dll
LIBS += D:\Qt5.11install\MinGW\bin\libopencv_video340.dll
LIBS += D:\Qt5.11install\MinGW\bin\libopencv_videoio340.dll
LIBS += D:\Qt5.11install\MinGW\bin\libopencv_videostab340.dll

然后就可编写OpenCV程序了。 

注意:需要把dll文件和exe文件放在一起,最好把bin目录添加到环境变量,否则会报程序异常终止

测试代码

#include <iostream>
#include "opencv2/opencv.hpp"
using namespace std;
using namespace cv;

int main()
{
    cout << "Hello World!" << endl;
    Mat image=imread("E:\Qt_Project\OpenCVtest\1.jpg");
    if(image.empty())
    {
        cout<<"Picture loading failed !"<<endl;
        return -1;
    }
    imshow("src",image);
    waitKey(0);
    return 0;
}

QT 配置opencv undefined reference qt5配置opencv,QT 配置opencv undefined reference qt5配置opencv_qt_07,第7张


https://www.xamrdz.com/lan/5ef1935505.html

相关文章: