0

首先,我要说的是,到目前为止,我已经使用这篇关于该主题的非常有趣的帖子来建立大部分内容。

在提到的帖子中,该示例使用网络摄像头和 UI 窗口来实时查看输出。我只是想使用类似的代码来比较两张图像(与一张图像和很多帧相对),但遇到了一些问题。

所以我有两个图像(cv::Mat 对象)

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,所以它根本没有进入循环。

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

4

1 回答 1

3

You need to detect keypoints in object_1 with detector.detect(object_1, kp_image );

And after that you can call extractor.compute( object_1, kp_object, des_object ); as seen HERE

于 2013-10-09T16:04:46.337 回答