win8 64bit/win7 32bit/winxp 32bit +QT+opencv环境配置教程,亲测成功

时间:2021-03-13 06:36:42

!!本教程在两台win8 64bit和win7 32bit 和winxp 32bit上都亲测成功!!
使用的软件 版本:
1.QT creator 2.5.2
2.QT 4.8.6
3.mingw32
4.gdb
5.cmake 3.3.0
6.opencv 2.4.10

win8 64bit/win7 32bit/winxp 32bit +QT+opencv环境配置教程,亲测成功

安装步骤:
1.安装qt creator ,本人装到c:/QT (即默认地址,换别的地址也行)
2.解压MinGW32和gdb至c:/QT 下
3.安装QT4.8.6,安装中当提示选择mingw时,选中第二步中的解压的 mingw32即可。
4.安装成功后,开始配置QT+QT creator;
打开qt creator ,选择工具—>选项
win8 64bit/win7 32bit/winxp 32bit +QT+opencv环境配置教程,亲测成功
win8 64bit/win7 32bit/winxp 32bit +QT+opencv环境配置教程,亲测成功
5.上述步骤完成,新建一个gui工程,直接F5运行,无报错,则配置成功。
win8 64bit/win7 32bit/winxp 32bit +QT+opencv环境配置教程,亲测成功

6.开始配置QT+opencv2.4.10
(1)双击opencv.exe解压至c:即可(别的盘也可以)
(2)安装cmake,安装过程中,有提示,选择添加到系统环境变量中(Path),即第二个选项。
(3)打开cmake,源文件为source文件夹,生成到opencv/MinGW
win8 64bit/win7 32bit/winxp 32bit +QT+opencv环境配置教程,亲测成功
(4)设置环境变量,因为我们用的是mingw来编译,所以要将C:\Qt\mingw32\bin加入到path下,注意切换到英文模式(我们配置时由于打成了中文的;而造成了很多问题)。
(5)点击configure按钮,打开新的窗口,在Specify the generator for this project下选择MinGW Makefiles,然后选择Specify native compilers,点击Next按钮,打开新的窗口,在C一栏中选择:c:/QT/mingw32/bin/gcc.exe,在C++一栏中填写C:/Qt/mingw32/bin/g++.exe,Fortran不填写。然后点击Finish按钮,开始配置,这时会出现如下的类似错误提示:(不一定出现)

**CMake Error: CMake was unable to find abuild program corresponding to "MinGW Makefiles".  CMAKE_MAKE_PROGRAM is not set.  You probably need to select a different buildtool.
CMake Error: CMake was unable to find abuild program corresponding to "MinGW Makefiles". CMAKE_MAKE_PROGRAM is not set. You probably need to select a different buildtool.
CMake Error: Error required internal CMakevariable not set, cmake may be not be built correctly.
Missing variable is:
CMAKE_CXX_COMPILER_ENV_VAR
CMake Error: Could not find cmake modulefile: C:/opencv/MinGW/CMakeFiles/3.0.1/CMakeCXXCompiler.cmake
CMake Error: Error required internal CMakevariable not set, cmake may be not be built correctly.
Missing variable is:
CMAKE_C_COMPILER_ENV_VAR
CMake Error: Could not find cmake modulefile: C:/opencv/MinGW/CMakeFiles/3.0.1/CMakeCCompiler.cmake
Configuring incomplete, errors occurred!**

解决方法:找到Name为CMAKE_MAKE_PROGRAM的一项,把它的Value填写为C:/Qt/mingw32/bin/mingw32-make.exe即可。再次点击Configure,这时不会提示任何错误,表明配置成功。
win8 64bit/win7 32bit/winxp 32bit +QT+opencv环境配置教程,亲测成功

(6)我们还需要把QT加上,找到WITH,把它下面的WITH_QT和WITH_OPENGL选上,再次Configure。这时会出现类似下面的错误提示:

CMake Error atC:/CMake/share/cmake-3.0/Modules/FindQt4.cmake:1316
(message): Found unsuitable Qt version "" from NOTFOUND, this
coderequires Qt 4.x Call Stack (most recent call first):
cmake/OpenCVFindLibsGUI.cmake:34 (find_package) CMakeLists.txt:466
(include)

有很多地方提示变红,亲测只要找到QT_QMAKE_EXECUTABLE,它的值填写为:C:/Qt/bin/qmake.exe即可。再次configure,成功,然后generate。到此成功了一大半。
参考博客

http://blog.csdn.net/zhaocj/article/details/38944037

(7)win+r,输入cmd,打开系统命令行终端,输入(cd c:\)进入到c盘根目录下,输入cd opencv/mingw进入此目录下,执行mingw32-make命令,这会需要很长时间,有时候会出错,如果使用的cmake版本低了,会出现错误:(本人亲测使用opencv.4.10+cmake2.8.5时,出现)

Generating opencl_kernels.cpp, opencl_kernels.hpp CMake Error
at D:/Program Files/opencv2.4.9/sources/cmake/cl2cpp.cmake:50
(string): string does not recognize sub-command MD5

解决办法:使用高版本的cmake(3.3.0)或 C:/opencv/sources/cmake/cl2cpp.cmake文件,将第五十行的
string(MD5 hash “${lines}”) 删除后,重新输入mingw32-make编译。成功。

最后提示build target opencv transcode之后,接着输入mingw32-make install,完事之后关闭命令行即可。

(8)成功后,mingw文件夹下会出现我们需要的很多文件,但我们只需要编译后MINGW目录下的bin和lib文件夹,不过别的文件你删不删无所谓。添加:C:\opencv\MinGw\bin至系统的环境变量path,
win8 64bit/win7 32bit/winxp 32bit +QT+opencv环境配置教程,亲测成功
今后我们在用Qt创建opencv工程的时候,都要在工程名称.Pro上添加以下目录和库。

INCLUDEPATH += c:\opencv\build\include\
c:\opencv\build\include\opencv\
c:\opencv\build\include\opencv2


LIBS += c:\opencv\MinGw\lib\libopencv_calib3d2410.dll.a\
c:\opencv\MinGw\lib\libopencv_contrib2410.dll.a\
c:\opencv\MinGw\lib\libopencv_core2410.dll.a\
c:\opencv\MinGw\lib\libopencv_features2d2410.dll.a\
c:\opencv\MinGw\lib\libopencv_flann2410.dll.a\
c:\opencv\MinGw\lib\libopencv_gpu2410.dll.a\
c:\opencv\MinGw\lib\libopencv_highgui2410.dll.a\
c:\opencv\MinGw\lib\libopencv_imgproc2410.dll.a\
c:\opencv\MinGw\lib\libopencv_legacy2410.dll.a\
c:\opencv\MinGw\lib\libopencv_ml2410.dll.a\
c:\opencv\MinGw\lib\libopencv_objdetect2410.dll.a\
c:\opencv\MinGw\lib\libopencv_video2410.dll.a

(9)测试一下吧。新建控制台工程。

#include <QCoreApplication>
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>

int main()
{
cv::Mat image = cv::imread("C:\\1.jpg");
cv::namedWindow("My image",CV_WINDOW_AUTOSIZE);
cv::imshow("My image",image);
cv::waitKey(0);
cv::destroyWindow("My image");
return 0;
}

win8 64bit/win7 32bit/winxp 32bit +QT+opencv环境配置教程,亲测成功

亲测还发现一个问题,就是如果在一台电脑上用cmake已经编译好了opencv,(不论两台电脑的系统差别),直接把mingw下的bin和lib文件夹拷过去就能用了,而不需要再次配置opencv,而把opencv直接当文件来用了,很神奇!!
明天研究linux下qt+opencv的配置!