简体   繁体   English

为什么在C ++中使用OpenCV似乎无法与knnMatch匹配?

[英]Why can't I seem to get any matches with knnMatch using OpenCV with C++?

First off I will say that so far I have based a large chunk of this using this very interesting post on the subject. 首先,我要说的是,到目前为止,我已经使用关于这个主题的非常有趣的帖子 ,将很大一部分作为基础。

In the mentioned post, the example uses a webcam and a UI window for the purposes of seeing the output in real time. 在提到的帖子中,该示例使用网络摄像头和UI窗口来实时查看输出。 I am simply just trying to use similar code to compare two images, (as appose to one image and lots of frames), but have ran into some problems. 我只是在尝试使用类似的代码来比较两个图像(适用于一个图像和许多帧),但是遇到了一些问题。

So I have two images (cv::Mat objects) 所以我有两个图像(cv :: Mat对象)

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

The following code isn't great, but this is the general idea: 以下代码不是很好,但这是一般的想法:

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]);
    }
}

The issue here is that because matches.size() is equal to 0 , it is not getting into the loop at all. 这里的问题是,因为matches.size()等于0 ,所以它根本没有进入循环。

My question is, (even if both original images are the same) why are there no matches? 我的问题是,(即使两个原始图像都相同)为什么没有匹配项?

You need to detect keypoints in object_1 with detector.detect(object_1, kp_image ); 您需要使用detector.detect(object_1, kp_image );object_1检测关键点detector.detect(object_1, kp_image );

And after that you can call extractor.compute( object_1, kp_object, des_object ); 然后,您可以调用extractor.compute( object_1, kp_object, des_object ); as seen HERE 这里所见

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM