2012-04-04 63 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 
}