2012-01-13 63 views
4

我知道已經有幾個問題同題問在這裏,但我找不到任何幫助。OpenCV的衝浪和離羣值檢測

所以我想比較2張圖片,看看它們有多相似,我使用衆所周知的find_obj.cpp演示來提取海浪描述符,然後爲匹配使用flannFindPairs。

但你也知道這種方法不丟棄異常值,我想知道真正的積極匹配的號碼,以便我可以計算這兩個圖像的相似程度。

我已經看到這個問題:Detecting outliers in SURF or SIFT algorithm with OpenCV和那裏的傢伙建議使用findFundamentalMat,但是一旦你得到了基本矩陣,我怎麼能從矩陣中得到離羣值/真正的正數呢?謝謝。

回答

5

下面是從descriptor_extractor_matcher.cpp樣品可用一個片段從OpenCV的:

if(!isWarpPerspective && ransacReprojThreshold >= 0) 
    { 
     cout << "< Computing homography (RANSAC)..." << endl; 
     vector<Point2f> points1; KeyPoint::convert(keypoints1, points1, queryIdxs); 
     vector<Point2f> points2; KeyPoint::convert(keypoints2, points2, trainIdxs); 
     H12 = findHomography(Mat(points1), Mat(points2), CV_RANSAC, ransacReprojThreshold); 
     cout << ">" << endl; 
    } 

    Mat drawImg; 
    if(!H12.empty()) // filter outliers 
    { 
     vector<char> matchesMask(filteredMatches.size(), 0); 
     vector<Point2f> points1; KeyPoint::convert(keypoints1, points1, queryIdxs); 
     vector<Point2f> points2; KeyPoint::convert(keypoints2, points2, trainIdxs); 
     Mat points1t; perspectiveTransform(Mat(points1), points1t, H12); 

     double maxInlierDist = ransacReprojThreshold < 0 ? 3 : ransacReprojThreshold; 
     for(size_t i1 = 0; i1 < points1.size(); i1++) 
     { 
      if(norm(points2[i1] - points1t.at<Point2f>((int)i1,0)) <= maxInlierDist) // inlier 
       matchesMask[i1] = 1; 
     } 
     // draw inliers 
     drawMatches(img1, keypoints1, img2, keypoints2, filteredMatches, drawImg, CV_RGB(0, 255, 0), CV_RGB(0, 0, 255), matchesMask 
#if DRAW_RICH_KEYPOINTS_MODE 
        , DrawMatchesFlags::DRAW_RICH_KEYPOINTS 
#endif 
        ); 

#if DRAW_OUTLIERS_MODE 
     // draw outliers 
     for(size_t i1 = 0; i1 < matchesMask.size(); i1++) 
      matchesMask[i1] = !matchesMask[i1]; 
     drawMatches(img1, keypoints1, img2, keypoints2, filteredMatches, drawImg, CV_RGB(0, 0, 255), CV_RGB(255, 0, 0), matchesMask, 
        DrawMatchesFlags::DRAW_OVER_OUTIMG | DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS); 
#endif 
    } 
    else 
     drawMatches(img1, keypoints1, img2, keypoints2, filteredMatches, drawImg); 

關鍵線,用於濾波在這裏進行的:

if(norm(points2[i1] - points1t.at<Point2f>((int)i1,0)) <= maxInlierDist) // inlier 
       matchesMask[i1] = 1; 

其是測量之間的L2範數距離點(如果沒有指定,則爲3個像素,或用戶定義的像素重投影錯誤數)。

希望有幫助!

+0

謝謝你的回答......我自認爲是在C語言編寫的,這片段是C++不能直接在find_obj.cpp使用此代碼。然而,我已經在SURFFlannMatcher.cpp演示中嘗試過了,它並沒有給出好的結果。例如,如果我得到同一個對象,同一場景的兩張圖片,即幾乎相同的圖片但不同的分辨率,findHomography函數將只能找到異常值並且沒有內部值...這很奇怪。 – user1148222 2012-01-19 15:21:26

1

您可以使用名爲「ptpairs」矢量的大小,以決定如何在similiar照片是。 此向量包含匹配的關鍵點,所以他的大小/ 2是匹配的數量。 我認爲你可以使用ptpairs的大小除以關鍵點的總數來設置適當的閾值。 這可能會給你一個他們之間的相似的估計。