2012-04-04 60 views
8

我正试图实现一个程序,它将输入两个图像,一个是单独的一个盒子的图像,另一个包含场景中的盒子。基本上,该程序应该找到这两个图像中的关键点,并将显示匹配关键点的图像。最后,我期望看到两个输入图像的附加图像及其匹配的关键点连接在一起。我的代码如下:试图在OpenCv中使用筛选来匹配两个图像,但匹配太多

#include <opencv2\opencv.hpp> 
#include <iostream> 

int main(int argc, const char* argv[]) { 
    cv::Mat input1 = cv::imread("input.jpg", 1); //Load as grayscale 
    //cv::cvtColor(input1,input1,CV_BGR2GRAY); 
    //second input load as grayscale 
    cv::Mat input2 = cv::imread("input2.jpg",1); 
    cv::SiftFeatureDetector detector; 
    //cv::SiftFeatureDetector 
    detector(
     1, 1, 
     cv::SIFT::CommonParams::DEFAULT_NOCTAVES, 
     cv::SIFT::CommonParams::DEFAULT_NOCTAVE_LAYERS, 
     cv::SIFT::CommonParams::DEFAULT_FIRST_OCTAVE, 
     cv::SIFT::CommonParams::FIRST_ANGLE); 
    std::vector<cv::KeyPoint> keypoints1; 
    detector.detect(input1, keypoints1); 
    // Add results to image and save. 
    cv::Mat output1; 
    cv::drawKeypoints(input1, keypoints1, output1); 
    cv::imshow("Sift_result1.jpg", output1); 
    cv::imwrite("Sift_result1.jpg",output1); 
    //keypoints array for input 2 
    std::vector<cv::KeyPoint> keypoints2; 
    //output array for ouput 2 
    cv::Mat output2; 
    //Sift extractor of opencv 
    cv::SiftDescriptorExtractor extractor; 
    cv::Mat descriptors1,descriptors2; 
    cv::BruteForceMatcher<cv::L2<float>> matcher; 
    cv::vector<cv::DMatch> matches; 
    cv::Mat img_matches; 
    detector.detect(input2,keypoints2); 
    cv::drawKeypoints(input2,keypoints2,output2); 
    cv::imshow("Sift_result2.jpg",output2); 
    cv::imwrite("Sift_result2.jpg",output2); 
    extractor.compute(input1,keypoints1,descriptors1); 
    extractor.compute(input2,keypoints2,descriptors2); 
    matcher.match(descriptors1,descriptors2,matches); 
    //show result 
    cv::drawMatches(input1,keypoints1,input2,keypoints2,matches,img_matches); 
    cv::imshow("matches",img_matches); 
    cv::imwrite("matches.jpg",img_matches); 
    cv::waitKey(); 
    return 0; 
} 

问题是有两个许多匹配比预期。我试图调试程序,看看什么是关键点向量内,等等,一切看起来都很好,至少我认为他们是,关键点检测方向等。

我使用的是OpenCV v2.3并检查了它的文档,以了解我正在使用的类的类型,并试图解决这个问题,但这并不奏效。我在这工作了3天并没有取得很大的进步。

这是我从我的程序中得到的输出。

我应该删除图像。

我知道不应该给我太多的匹配,因为我已经测试了完全相同的图像与matlab中的另一个实现,这是相当不错的。

回答

6

而不是使用BruteForceMatcher尝试使用FlannBasedMatcher也计算关键点之间的最大和最小距离仅保持良好的匹配。一个例子见“Feature Matching with FLANN”。

+0

谢谢。问题正如你所说,画出所有的比赛,而不仅仅是好的比赛。现在我正在排序匹配并选择最小距离的15%。 – bahti 2012-04-05 08:06:36

+0

我希望你解决这个问题。 – MMH 2012-04-06 05:37:15

+0

[New link](http://docs.opencv.org/2.4/doc/tutorials/features2d/feature_flann_matcher/feature_flann_matcher.html) – 2017-01-15 07:42:57

0

我面临着SIFT的同样的问题。 我使用了knn matcher(K = 3)。并按照以下步骤反复进行:

{ 
Calculated best affine transform with least square method. 

Found out the transform for all keypoints in source image. 

Checked out MaxError and MinError. 

Points with Error near MaxError are removed from the matching list 
}