使用Ransac估计两组点之间的2D变换

时间:2015-10-15 06:23:38

标签: c++ opencv affinetransform homography ransac

据我所知,OpenCV使用RANSAC来解决findhomograph的问题,并返回一些有用的参数,如homograph_mask。但是,如果我只想估计2D转换,这意味着一个仿射矩阵,有没有办法使用findHomography使用RANSAC并返回该掩码的相同方法逻辑?

2 个答案:

答案 0 :(得分:5)

estimateRigidTransform在内部使用RANSAC,虽然参数目前是固定的 - 请参阅此处的代码 - https://github.com/opencv/opencv/blob/master/modules/video/src/lkpyramid.cpp

public class BasicDesign extends AppCompatActivity 
    implements det.OnViewChangedListener {

    //...

    // Implement the interface in the activity.
    @Override
    public void onViewChanged(String value) {
        // Use the retrieved value, e.g. save this to a local
        // that you can show when you click your button.
    }
}

答案 1 :(得分:2)

您可以直接使用estimateAffinePartial2D: https://docs.opencv.org/4.0.0/d9/d0c/group__calib3d.html#gad767faff73e9cbd8b9d92b955b50062d

cv::Mat cv::estimateAffinePartial2D (   
    InputArray  from,
    InputArray  to,
    OutputArray     inliers = noArray(),
    int     method = RANSAC,
    double  ransacReprojThreshold = 3,
    size_t  maxIters = 2000,
    double  confidence = 0.99,
    size_t  refineIters = 10 
)   

例如:

        src_pts = np.float32([pic1.key_points[m.queryIdx].pt for m in matches]).reshape(-1, 1, 2)
        dst_pts = np.float32([pic2.key_points[m.trainIdx].pt for m in matches]).reshape(-1, 1, 2)

        # Find the transformation between points, standard RANSAC
        transformation_matrix, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0)

        # Compute a rigid transformation (without depth, only scale + rotation + translation) and RANSAC
        transformation_rigid_matrix, rigid_mask = cv2.estimateAffinePartial2D(src_pts, dst_pts)