Win8.1下OpenCV2.4.9+Qt5.3.2开发环境搭建
最后更新于:2022-04-01 06:35:44
**准备工作:**
1、下载qt-opensource-windows-x86-mingw482_opengl-5.3.2.exe文件,执行该文件,安装过程可参考:[http://blog.csdn.net/liyuefeilong/article/details/43116303](http://blog.csdn.net/liyuefeilong/article/details/43116303)(建议安装在C盘的根目录下),该文件封装好了Qt libraries、Qt Creator,其中Qt Creator为3.2.1版本。
2、下载OpenCV for Windows2.4.9版本——opencv-2.4.9.exe。执行该文件,把它解压到适当的目录下。
3、在[http://www.cmake.org/download/](http://www.cmake.org/download/)下载cmake-3.1.1-win32-x86.exe文件并安装cmake。
4、设置系统的环境变量,即把C:\Qt\Qt5.3.2\Tools\mingw482_32\bin加入系统的Path环境变量中,因为在CMake编译过程中会用到该目录下的库。
**以下步骤对OpenCV进行编译:**
1、打开CMake(cmake-gui),在Where is the source code和Where to build thebinaries中分别设定为OpenCV解压后的源文件地址(我的解压目录为E:/opencv/opencv,其中包含build和sources两个文件夹,选择sources文件夹)和编译后库文件的存放位置,该目录需要自行创建。
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2015-12-30_5683a744e7c15.jpg)
2、选择好路径,点击configure按钮,打开新的窗口,在Specify the generator for this project下选择MinGW Makefiles,然后选择Specify native compilers,点击Next按钮,打开新的窗口,在C一栏中填写C:/Qt/Qt5.3.2/Tools/mingw482_32/bin/gcc.exe,在C++一栏中填写C:/Qt/Qt5.3.2/Tools/mingw482_32/bin/g++.exe。
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2015-12-30_5683a74503a60.jpg)
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2015-12-30_5683a74517b6f.jpg)
3、然后点击Finish按钮,开始配置。在Configuring done之后,我们还需要把QT和OpenGL的选项勾上:找到WITH_QT和WITH_OPENGL并选上,再次Configure,此时出现以下信息,检查各路径是否正确:
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2015-12-30_5683a74527610.jpg)
4、再次点击Configure,检查的路径是否正确,如果正确配置完成则不会出现任何错误提示,在此过程中会出现一些warning,但是使用至今没有发现任何问题。点击Generate。Generate完成后,关闭CMake窗口。
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2015-12-30_5683a74540b0a.jpg)
5、打开系统的命令终端,进入C:\opencv2目录,执行mingw32-make命令(可能需要几十分钟),该命令执行完后,再执行mingw32-make install命令。这样可用于Qt的opencv库文件就生成了。
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2015-12-30_5683a745560a6.jpg)
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2015-12-30_5683a74563b05.jpg)
6、最后添加环境变量:c:\opencv2\bin。
至此,OpenCV2.4.9+Qt5.3.2开发环境搭建完成。值得注意的是,若使用Qt Creator编译opencv相关代码,在创建一个新工程后,还需要在该工程的工程文件.pro文件中添加以下语句,当然,这些路径取决于自己选择OpenCV编译路径:
~~~
INCLUDEPATH+=C:\opencv2\install\include\opencv\
C:\opencv2\install\include\opencv2\
C:\opencv2\install\include
LIBS+=C:\opencv2\lib\libopencv_calib3d249.dll.a\
C:\opencv2\lib\libopencv_contrib249.dll.a\
C:\opencv2\lib\libopencv_core249.dll.a\
C:\opencv2\lib\libopencv_features2d249.dll.a\
C:\opencv2\lib\libopencv_flann249.dll.a\
C:\opencv2\lib\libopencv_gpu249.dll.a\
C:\opencv2\lib\libopencv_highgui249.dll.a\
C:\opencv2\lib\libopencv_imgproc249.dll.a\
C:\opencv2\lib\libopencv_legacy249.dll.a\
C:\opencv2\lib\libopencv_ml249.dll.a\
C:\opencv2\lib\libopencv_nonfree249.dll.a\
C:\opencv2\lib\libopencv_objdetect249.dll.a\
C:\opencv2\lib\libopencv_ocl249.dll.a\
C:\opencv2\lib\libopencv_video249.dll.a\
C:\opencv2\lib\libopencv_photo249.dll.a\
C:\opencv2\lib\libopencv_stitching249.dll.a\
C:\opencv2\lib\libopencv_superres249.dll.a\
C:\opencv2\lib\libopencv_ts249.a\
C:\opencv2\lib\libopencv_videostab249.dll.a
~~~
##### [ 来自CODE的代码片](https://code.csdn.net/snippets/601082)
Qt-OpenCV配置
最后感谢博客的一些指引:
[http://blog.csdn.net/jiebaoabcabc/article/details/17097765](http://blog.csdn.net/jiebaoabcabc/article/details/17097765)
[http://blog.csdn.net/qiurisuixiang/article/details/8665278#reply](http://blog.csdn.net/qiurisuixiang/article/details/8665278#reply)