现有的文章很多,但是自己编译的时候也遇到了问题,记录下来
参考:
http://blog.csdn.net/kaychangeek/article/details/52303949
http://blog.csdn.net/qiurisuixiang/article/details/8665278/
1、QT安装
各版本下载地址:
http://ftp.vim.org/languages/qt/archive/qt/
http://download.qt.io/archive/qt/ (官网)
2、opencv2.4.9安装
3、添加环境变量(设置完环境变量需要重启应用)
opencv环境变量:(我的路径)
D:\opencv2.4.9\build\include\opencv
D:\opencv2.4.9\build\include\opencv2
D:\opencv2.4.9\build\include
qt环境变量:(mingw)
D:\Qt\Qt5.7.0\5.7\mingw53_32\bin(cmake Generate用到)
D:\Qt\Qt5.7.0\Tools\mingw530_32\bin (cmake Configure用到)
4、CMake编译opencv
点击Configure,选择MinGW Makefiles,如果报错,说明D:\Qt\Qt5.7.0\Tools\mingw530_32\bin 没加到PATH中
这时,会在目标目录下生产一些文件。cmake界面出来了一些可选项勾选QT和OPENGL,其他不用改,再次点击Configure。如果报错,说明D:\Qt\Qt5.7.0\5.7\mingw53_32\bin 没加到PATH中
点击Generate,成功后,底下会提示Generating done
打开cmd,进入刚才CMAKE设置的目标路径,输入mingw32-make命令开始编译(注意没空格)
此过程较为漫长
执行mingw32-make install
这时会在目标目录下生成一个install文件夹,库和bin和头文件都在里面
进入install目录下面,找到bin文件夹,直接拷贝到opencv/build/x86/下以便以后配置,将其路径
D:\opencv2.4.9\build\x86\mingw\bin
加入系统环境变量
5、配置Qt + opencv
打开Qtcreator,创建一个GUI工程或者控制台也行。
打开.pro文件,加入以下配置:
INCLUDEPATH += D:\opencv2.4.9\build\include\opencv\
D:\opencv2.4.9\build\include\opencv2\
D:\opencv2.4.9\build\include
LIBS += -LD:\opencv2.4.9\build\x86\mingw\lib\
-llibopencv_core249 \
-llibopencv_highgui249 \
-llibopencv_imgproc249 \
-llibopencv_ml249 \
-llibopencv_video249 \
-llibopencv_features2d249 \
-llibopencv_calib3d249 \
-llibopencv_objdetect249 \
-llibopencv_contrib249 \
-llibopencv_legacy249 \
-llibopencv_flann249 \
-llibopencv_gpu249 \
-llibopencv_nonfree249 \
-llibopencv_ocl249 \
-llibopencv_photo249 \
-llibopencv_stitching249 \
-llibopencv_superres249 \
-llibopencv_ts249 \
-llibopencv_videostab249
LIBS += LD:\opencv2.4.9\build\x86\mingw\lib\*.lib
#include
#include
#include
using namespace cv;
int main()
{
Mat image;
image = cv::imread("K:/QTtest/untitled/1.bmp");
if(image.empty())
return 1;
imshow("image", image);
waitKey(0);
}
点击构建运行即可如果出现no such file or directory,点击构建->执行qmake,然后重新构建项目。最后就能成功运行程序了