2013-10-08 38 views
0

首先,我会说到目前为止,我已经基于这个主题使用this very interesting post的一大块。为什么我看不出与使用OpenCV和C++的knnMatch匹配?

在上述文章中,该示例使用网络摄像头和UI窗口来实时查看输出。我只是试图使用类似的代码来比较两张图片(如同一张图片和很多帧),但遇到了一些问题。

所以,我有两个图像(CV ::垫对象)

Mat object_1 = imread("image1.jpg", CV_LOAD_IMAGE_GRAYSCALE); 
Mat object_2 = imread("image2.jpg", CV_LOAD_IMAGE_GRAYSCALE); 

下面的代码是不是很大,但这是总体思路:

int minHessian = 500; 

SurfFeatureDetector detector(minHessian); 
std::vector<KeyPoint> kp_object; 

SurfDescriptorExtractor extractor; 
Mat des_object; 

extractor.compute(object_1, kp_object, des_object); 

FlannBasedMatcher matcher; 

std::vector<Point2f> obj_corners(4); 

//Get the corners from the object 
obj_corners[0] = cvPoint(0,0); 
obj_corners[1] = cvPoint(object_1.cols, 0); 
obj_corners[2] = cvPoint(object_1.cols, object_1.rows); 
obj_corners[3] = cvPoint(0, object_1.rows); 

Mat des_image, img_matches; 
std::vector<KeyPoint> kp_image; 
std::vector<vector<DMatch > > matches; 
std::vector<DMatch > good_matches; 
std::vector<Point2f> obj; 
std::vector<Point2f> scene; 
std::vector<Point2f> scene_corners(4); 
Mat H; 

detector.detect(object_2, kp_image); 
extractor.compute(object_2, kp_image, des_image); 

matcher.knnMatch(des_object, des_image, matches, 2); 

for(int i = 0; i < min(des_image.rows-1,(int) matches.size()); i++) //THIS LOOP IS SENSITIVE TO SEGFAULTS 
{ 
    if((matches[i][0].distance < 0.6*(matches[i][1].distance)) && ((int) matches[i].size()<=2 && (int) matches[i].size()>0)) 
    { 
     good_matches.push_back(matches[i][0]); 
    } 
} 

这里的问题是,因为matches.size()等于0,它根本没有进入循环。

我的问题是,(即使两个原始图像都是相同的)为什么没有匹配?

+1

嗨哈利,我不知道这一点,因为我从来没有使用过它,但不应该您使用detector.detect(object_1,kp_image)检测object_1中的关键点;第一?之后你可以调用extractor.compute(object_1,kp_ob​​ject,des_object); ?正如所见[这里](http://docs.opencv.org/doc/user_guide/ug_features2d.html) – OpenMinded

+0

非常感谢!我从来没有发现过,我完全错过了!现在正在工作。干杯!作为答案提交,我将标记为解决方案。 –

回答

3

您需要检测的关键点在object_1detector.detect(object_1, kp_image);

之后,你可以调用extractor.compute(object_1, kp_object, des_object);所看到HERE

相关问题