最近Opencv升級比較快,從2.4.0到2.4.1到2.4.2,使得我這個還在使用2.3.1的人很不好意思,而且聽說新版本里添加了tbb並行功能,急着想用這些功能的我趕緊下了2.4.2。
按部就班的解壓、設置c++目錄(我使用的是vs2008)、設置環境變量......一系列的完成之后,想用一下surf算法,就嘗試着把pdf文檔里的代碼復制到了vs里,運行一下,發現不行,報錯。。。瞬間有點被騙的感覺,這可是從官方發布的pdf里的最新代碼啊!!!
#include <iostream> #include "opencv2/core/core.hpp" #include "opencv2/features2d/features2d.hpp" #include "opencv2/highgui/highgui.hpp" #include "opencv2/nonfree/nonfree.hpp" #include "opencv2/nonfree/features2d.hpp" using namespace std; using namespace cv; char *path1="D:\\TestData\\cvtest\\src\\left01.jpg"; char *path2="D:\\TestData\\cvtest\\src\\left03.jpg"; int main() { Mat src1=imread(path1,0); /*namedWindow("image",CV_WINDOW_AUTOSIZE); imshow("image", src1); waitKey(0);*/ Mat src2=imread(path2,0); SurfFeatureDetector detector(400); vector<KeyPoint> keypoint1,keypoint2; detector.detect(src1,keypoint1); detector.detect(src2,keypoint2); SurfDescriptorExtractor extractor; Mat descriptor1,descriptor2; extractor.compute(src1,keypoint1,descriptor1); extractor.compute(src2,keypoint2,descriptor2); BruteForceMatcher<L2<float>> matcher; vector<DMatch>matches; matcher.match(descriptor1,descriptor2,matches); namedWindow("matches",1); Mat img_matches; drawMatches(src1,keypoint1,src2,keypoint2,matches,img_matches); imshow("matches",img_matches); cvWaitKey(0); return 0; }
到群里問,大家也剛用2.4.2不久,只有個達人告訴我是頭文件的問題。我試着去找了一下,發現2.3.1的include文件和2.4.2的差別很大,新版本里多了很多東西,對比了一下features2d.hpp這個文件,發現原先包含在features2d.hpp的BruteForceMatcher現在根本不在features2d.hpp中,我試着去查找其他有可能在的hpp文件,找了幾個,發現這不是解決問題的辦法,問google吧!在搜了一下之后發現,還真有人碰到了這個問題,也確實是頭文件的問題:缺少了#include<opencv2/legacy/legacy.hpp>,加了之后,發現之前的錯誤確實沒了。
但是新的問題出來了,說link出現問題,沒經驗的我還是只能問google(谷歌確實強大啊!!!),牛人一針見血的指出了問題所在:For those who would have the same problem, make sure you have ALL the right linker inputs (configuration -> linker -> inputs), included dlls such as opencv, highgui etc.在右鍵“屬性”->"鏈接器"->“輸入”->"附加依賴項"把新輸入的legacy的靜態文件opencv_legacy242d.lib加進來就ok了!
寫的比較亂,希望對用到的人有用吧。