本文转自:http://blog.csdn.net/panda1234lee/article/details/8611363
最近Opencv升级比较快,从2.4.0到2.4.1到2.4.2,使得我这个还在使用2.3.1的人很不好意思,而且听说新版本里添加了tbb并行功能,急着想用这些功能的我赶紧下了2.4.2。
按部就班的解压、设置c++目录(我使用的是vs2008)、设置环境变量......一系列的完成之后,想用一下surf算法,就尝试着把pdf文档里的代码复制到了vs里,运行一下,发现不行,报错。。。瞬间有点被骗的感觉,这可是从官方发布的pdf里的最新代码啊!!!
[cpp] view plain copy print ?
- #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);
-
-
-
-
-
- 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;
- }
#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了!
遇到的问题及解决:
1、大量的.dll文件(如msvcr100d.dll)未加载,这个可能是一些包含的相关的.dll文件的路径没加入到PATH中,可以找到位置后加入PATH,或者在工程调试选项中 设置对 源服务器的支持 与 microsoft符号服务器 的支持(需联网)。
2、参考 http://www.cnblogs.com/ll2008swu/archive/2012/07/23/2605639.html 在做2维特征提取时,发现SurfFeatureDetector并不在opencv2/features2d/features2d.hpp 而是在opencv2/nonfree/features2d.hpp
BruteForceMatcher 需加入opencv2/nonfree/features2d.hpp
包含上面文件最终还需加入 opencv2/legacy/legacy.hpp
//同时为正常link,须Linker下加入-lopencv_nonfree240(不然会出现error LNK2019: 无法解析的外部符号的问题)。
使用VC者依版本将opencv_nonfree242d.lib所在位置加入Linker路径设定中即可顺利执行