如何在SURF,SIFT和ORB匹配结果上应用RANSAC

时间:2013-04-14 18:26:35

标签: opencv feature-extraction orb ransac feature-descriptor

我正在进行图像处理。我想匹配2D功能,我在SURF,SIFT,ORB上做了很多测试。
如何在OpenCV中对SURF / SIFT / ORB应用RANSAC?

2 个答案:

答案 0 :(得分:23)

OpenCV具有函数cv::findHomography,它可以选择使用RANSAC来查找与两个图像相关的单应矩阵。您可以在操作here中查看此功能的示例。

具体而言,您感兴趣的代码部分是:

FlannBasedMatcher matcher;
std::vector< DMatch > matches;
matcher.match( descriptors_object, descriptors_scene, matches );

for( int i = 0; i < good_matches.size(); i++ )
{
    //-- Get the keypoints from the good matches
    obj.push_back( keypoints_object[ good_matches[i].queryIdx ].pt );
    scene.push_back( keypoints_scene[ good_matches[i].trainIdx ].pt );
}

Mat H = findHomography( obj, scene, CV_RANSAC ); 

然后,您可以使用函数cv::perspectiveTransform根据单应矩阵扭曲图像。

cv::findHomography以外的CV_RANSAC的其他选项是使用每个点的0和使用最少中值方法的CV_LMEDS。有关详细信息,请参阅OpenCV摄像机校准文档here

答案 1 :(得分:2)

这是在获得的 SIFT / SURF 关键点上使用 ransacskimageProjectiveTransform(即单应性)模型应用 AffineTransform 的 Python 实现。此实现首先对获得的关键点进行 Lowe's ratio 测试,然后对来自 Lowe's ratio 测试的过滤后的关键点进行ransac。

import cv2
from skimage.measure import ransac
from skimage.transform import ProjectiveTransform, AffineTransform
import numpy as np


def siftMatching(img1, img2):
    # Input : image1 and image2 in opencv format
    # Output : corresponding keypoints for source and target images
    # Output Format : Numpy matrix of shape: [No. of Correspondences X 2] 

    surf = cv2.xfeatures2d.SURF_create(100)
    # surf = cv2.xfeatures2d.SIFT_create()

    kp1, des1 = surf.detectAndCompute(img1, None)
    kp2, des2 = surf.detectAndCompute(img2, None)

    FLANN_INDEX_KDTREE = 0
    index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
    search_params = dict(checks = 50)
    flann = cv2.FlannBasedMatcher(index_params, search_params)
    matches = flann.knnMatch(des1,des2,k=2)

    # Lowe's Ratio test
    good = []
    for m, n in matches:
        if m.distance < 0.7*n.distance:
            good.append(m)

    src_pts = np.float32([ kp1[m.queryIdx].pt for m in good ]).reshape(-1, 2)
    dst_pts = np.float32([ kp2[m.trainIdx].pt for m in good ]).reshape(-1, 2)

    # Ransac
    model, inliers = ransac(
            (src_pts, dst_pts),
            AffineTransform, min_samples=4,
            residual_threshold=8, max_trials=10000
        )

    n_inliers = np.sum(inliers)

    inlier_keypoints_left = [cv2.KeyPoint(point[0], point[1], 1) for point in src_pts[inliers]]
    inlier_keypoints_right = [cv2.KeyPoint(point[0], point[1], 1) for point in dst_pts[inliers]]
    placeholder_matches = [cv2.DMatch(idx, idx, 1) for idx in range(n_inliers)]
    image3 = cv2.drawMatches(img1, inlier_keypoints_left, img2, inlier_keypoints_right, placeholder_matches, None)

    cv2.imshow('Matches', image3)
    cv2.waitKey(0)

    src_pts = np.float32([ inlier_keypoints_left[m.queryIdx].pt for m in placeholder_matches ]).reshape(-1, 2)
    dst_pts = np.float32([ inlier_keypoints_right[m.trainIdx].pt for m in placeholder_matches ]).reshape(-1, 2)

    return src_pts, dst_pts