首先,你要正确的设置环境变量。环境变量的设置方法参考这里。
然后,打开Qt项目中的.pro文件,添加如下代码
INCLUDEPATH+=D:\OpenCV\opencv\build\include\opencv2\ D:\OpenCV\opencv\build\include\opencv\ D:\OpenCV\opencv\build\include CONFIG(release,debug|release) { LIBS+=D:\OpenCV\opencv\build\x86\vc10\lib\opencv_calib3d231.lib\ D:\OpenCV\opencv\build\x86\vc10\lib\opencv_contrib231.lib\ D:\OpenCV\opencv\build\x86\vc10\lib\opencv_core231.lib\ D:\OpenCV\opencv\build\x86\vc10\lib\opencv_features2d231.lib\ D:\OpenCV\opencv\build\x86\vc10\lib\opencv_flann231.lib\ D:\OpenCV\opencv\build\x86\vc10\lib\opencv_gpu231.lib\ D:\OpenCV\opencv\build\x86\vc10\lib\opencv_highgui231.lib\ D:\OpenCV\opencv\build\x86\vc10\lib\opencv_imgproc231.lib\ D:\OpenCV\opencv\build\x86\vc10\lib\opencv_legacy231.lib\ D:\OpenCV\opencv\build\x86\vc10\lib\opencv_ml231.lib\ D:\OpenCV\opencv\build\x86\vc10\lib\opencv_objdetect231.lib\ D:\OpenCV\opencv\build\x86\vc10\lib\opencv_ts231.lib\ D:\OpenCV\opencv\build\x86\vc10\lib\opencv_video231.lib } CONFIG(debug,debug|release) { LIBS+=D:\OpenCV\opencv\build\x86\vc10\lib\opencv_calib3d231d.lib\ D:\OpenCV\opencv\build\x86\vc10\lib\opencv_contrib231d.lib\ D:\OpenCV\opencv\build\x86\vc10\lib\opencv_core231d.lib\ D:\OpenCV\opencv\build\x86\vc10\lib\opencv_features2d231d.lib\ D:\OpenCV\opencv\build\x86\vc10\lib\opencv_flann231d.lib\ D:\OpenCV\opencv\build\x86\vc10\lib\opencv_gpu231d.lib\ D:\OpenCV\opencv\build\x86\vc10\lib\opencv_highgui231d.lib\ D:\OpenCV\opencv\build\x86\vc10\lib\opencv_imgproc231d.lib\ D:\OpenCV\opencv\build\x86\vc10\lib\opencv_legacy231d.lib\ D:\OpenCV\opencv\build\x86\vc10\lib\opencv_ml231d.lib\ D:\OpenCV\opencv\build\x86\vc10\lib\opencv_objdetect231d.lib\ D:\OpenCV\opencv\build\x86\vc10\lib\opencv_ts231d.lib\ D:\OpenCV\opencv\build\x86\vc10\lib\opencv_video231d.lib }注意,你的代码中的路径要根据OpenCV的具体安装路径做改变。
测试程序
#include<QtCore/QCoreApplication> #include<opencv.hpp> usingnamespacestd; usingnamespacecv; intmain(intargc,char*argv[]) { QCoreApplicationa(argc,argv); constchar*imagename="E:\\library\\Pictures\\lena.jpg"; //从文件中读入图像 Matimg=imread(imagename); //如果读入图像失败 if(img.empty()) { fprintf(stderr,"Cannotloadimage%s\n",imagename); waitKey(); return-1; } //显示图像 imshow("image",img); //此函数等待按键,按键盘任意键就返回 waitKey(); returna.exec(); }用Qt Creator编译时,要选择MSVC2010的发布编译器。
运行结果
但我遇到了一个问题,当我选择Qt Creator的MSVC2010的调试编译器时,就会出错。希望有热心的朋友能帮我解决这个问题。