首先,你要正確的設置環境變量。環境變量的設置方法參考這裡。
然後,打開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的調試編譯器時,就會出錯。希望有熱心的朋友能幫我解決這個問題。