Windows上Qt配置OpenCV(最简单版本无需自己编译-避坑必看)

1 前言

博主为了在qt上配置OpenCV踩了很多坑,经过一番努力,终于找到了最容易配置的方法,在这里总结出了一份经验,给有需要的人提供一点帮助.文章结尾有福利.

2 软件安装

2.1 安装qt

需要一个账号,注册后方可下载

qt官网

在这里插入图片描述

2.2 安装OpenCV(分为vc版和mingw版)

注意32位还是64位

选择Windows下载,接着一路next即可,这里下载的是msvc编译的版本

OpenCV官网

在这里插入图片描述

对于MinGW版本的,在github上有,可以直接下载使用我这里下载的是3.4.8版本,下载较慢,文末有链接方便快速下载

MinGW版本已编译

2.2.1 配置环境变量

OpenCV的使用需要配置环境变量,我将msvc和mingw的版本都配置了,可以按照自己安装的路径来配置

将bin文件夹放环境变量

msvc:
C:\OpenCV_s\opencv_vc\opencv\build\bin
C:\OpenCV_s\opencv_vc\opencv\build\x64\vc15\bin
C:\OpenCV_s\opencv_vc\opencv\build\x64\vc15\lib
mingw:
C:\OpenCV_s\OpenCV-MinGW-Build-OpenCV-3.4.8-x64\x64\mingw\bin

在这里插入图片描述

3 Qt配置OpenCV

3.1 创建一个项目(注意选择的是MinGW编译器还是MSVC编译器)

在pro文件中添加一些东西,根据自己选择的编译器,来添加,这里换成自己安装的路径

msvc:
INCLUDEPATH +=C:\OpenCV_s\opencv_vc\opencv\build\include\
              C:\OpenCV_s\opencv_vc\opencv\build\include\opencv\
              C:\OpenCV_s\opencv_vc\opencv\build\include\opencv2

LIBS +=C:\OpenCV_s\opencv_vc\opencv\build\x64\vc15\lib\opencv_world3414.lib 
或 C:\OpenCV_s\opencv_vc\opencv\build\x64\vc15\lib\opencv_world3414d.lib
注意: opencv_world3414d.lib 为debug版,opencv_world3414.lib为release版
mingw:
INCLUDEPATH+= C:\OpenCV_s\OpenCV-MinGW-Build-OpenCV-3.4.8-x64\include\
              C:\OpenCV_s\OpenCV-MinGW-Build-OpenCV-3.4.8-x64\include\opencv\
              C:\OpenCV_s\OpenCV-MinGW-Build-OpenCV-3.4.8-x64\include\opencv2
LIBS+=C:\OpenCV_s\OpenCV-MinGW-Build-OpenCV-3.4.8-x64\x64\mingw\bin\libopencv_*.dll

4 简单快捷添加OpenCV库的方法

创建pri文件,将下方内容写入,同样设置为自己安装的路径,文件可以放在任何位置,但建议放在安装OpenCV的路径下,方便自己查找.

INCLUDEPATH += C:/OpenCV_s/opencv_vc/opencv/build/include

Debug: {
    LIBS += -lC:/OpenCV_s/opencv_vc/opencv/build/x64/vc15/lib/opencv_world3414d
}

Release: {
    LIBS += -lC:/OpenCV_s/opencv_vc/opencv/build/x64/vc15/lib/opencv_world3414
}

之后在项目pro文件中添加下面这一行即可,不需要再添加那么多行,我这里pri文件放在下面的路径里,设置为自己放的路径,添加后qtcreator会自动添加到项目中:

include(C:\OpenCV_s\opencv.pri)

5 简单案例,测试是否安装成功

本例子使用的MSVC编译的OpenCV

5.1 创建一个控制台窗口

在这里插入图片描述

5.2 注意选择编译器

我是自己设置的MSVC64位编译器,一般来说qt安装时选了MSVC编译器就可以使用,不需要自己再设置,所以直接选择MSVC相应的版本即可

在这里插入图片描述

5.3 添加pri文件路径

在这里插入图片描述

main.cpp文件内容:

#include <QCoreApplication>
#include "opencv2/opencv.hpp"
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    using namespace cv;

    Mat image=imread("C:/test/1.jpeg");//改成自己图片的路径,注意使用的是'/'
    imshow("Output",image);
    return a.exec();
}

5.4 运行结果

在这里插入图片描述
在这里插入图片描述

5.5 注意事项

如果切换为MinGW编译的版本,需要重启qtcreator

6 福利

相关安装包(含MinGW和MSVC版本)链接(免费下载):

下载

GitHub 加速计划 / opencv31 / opencv
142
15
下载
OpenCV: 开源计算机视觉库
最近提交(Master分支:3 个月前 )
d9a139f9 Animated WebP Support #25608 related issues #24855 #22569 ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [x] There is a reference to the original bug report and related work - [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [ ] The feature is well documented and sample code can be built with the project CMake 1 天前
09030615 V4l default image size #25500 Added ability to set default image width and height for V4L capture. This is required for cameras that does not support 640x480 resolution because otherwise V4L capture cannot be opened and failed with "Pixel format of incoming image is unsupported by OpenCV" and then with "can't open camera by index" message. Because of the videoio architecture it is not possible to insert actions between CvCaptureCAM_V4L::CvCaptureCAM_V4L and CvCaptureCAM_V4L::open so the only way I found is to use environment variables to preselect the resolution. Related bug report is [#25499](https://github.com/opencv/opencv/issues/25499) Maybe (but not confirmed) this is also related to [#24551](https://github.com/opencv/opencv/issues/24551) This fix was made and verified in my local environment: capture board AVMATRIX VC42, Ubuntu 20, NVidia Jetson Orin. ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [X] I agree to contribute to the project under Apache 2 License. - [X] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [X] The PR is proposed to the proper branch - [X] There is a reference to the original bug report and related work - [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [ ] The feature is well documented and sample code can be built with the project CMake 1 天前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐