OpenCV Surf和Outliers检测

use*_*222 4 opencv surf outliers

我知道这里有同样的主题已经有几个问题,但我找不到任何帮助.

所以我想比较2个图像,看看它们有多相似,我正在使用众所周知的find_obj.cpp演示来提取冲浪描述符,然后为了匹配我使用了flannFindPairs.

但是你知道这个方法不会丢弃异常值,我想知道真正的正匹配的数量,这样我就可以知道这两个图像的相似程度.

我已经看到了这个问题:使用OpenCV检测SURF或SIFT算法中的异常值,并且那里的人建议使用findFundamentalMat,但是一旦得到基本矩阵,我怎样才能从该矩阵得到异常值/真阳性的数量?谢谢.

mev*_*ron 5

以下是OpenCV提供的descriptor_extractor_matcher.cpp示例的片段:

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 );
Run Code Online (Sandbox Code Playgroud)

过滤的关键行在这里执行:

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

这是测量点之间的L2范数距离(如果没有指定任何3个像素,或者用户定义的像素重投影错误数).

希望有所帮助!