OpenCV使用之-----BruteForceMatcher无法使用

最近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了!

写的比较乱,希望对用到的人有用吧。

posted @ 2012-07-23 22:00  ll2008  阅读(19349)  评论(3编辑  收藏  举报