【问题标题】:Estimate 2D transformation between two sets of points using RANSAC使用 RANSAC 估计两组点之间的二维变换
【发布时间】:2016-01-13 11:38:09
【问题描述】:

据我所知,OpenCV 使用 RANSAC 来解决findHomography 的问题,它会返回一些有用的参数,例如homograph_mask

但是,如果我只想估计 2D 变换,即仿射矩阵,有没有办法使用与 findHomography 相同的方法,它使用 RANSAC 并返回该掩码?

【问题讨论】:

  • Here 提到使用 estimateRigidTransform 似乎在内部使用 RANSAC,
  • 如果您知道如何从 3 个点对计算 2D 变换,您可以轻松编写自己的简单 RANSAC。 Afaik,estimateRigidTransform 使用 ransac,但不幸的是您无法对其进行参数化...
  • 好的,谢谢..所以没有现成的解决方案..

标签: c++ opencv affinetransform homography ransac


【解决方案1】:

您可以直接使用 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)

【讨论】:

    【解决方案2】:

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

    cv::Mat cv::estimateRigidTransform( InputArray src1, InputArray src2, bool fullAffine )
    {
        const int RANSAC_MAX_ITERS = 500;
        const int RANSAC_SIZE0 = 3;
        const double RANSAC_GOOD_RATIO = 0.5;
    
        // ...
    
        // RANSAC stuff:
        // 1. find the consensus
        for( k = 0; k < RANSAC_MAX_ITERS; k++ )
        {
            int idx[RANSAC_SIZE0];
            Point2f a[RANSAC_SIZE0];
            Point2f b[RANSAC_SIZE0];
    
            // choose random 3 non-complanar points from A & B
            for( i = 0; i < RANSAC_SIZE0; i++ )
            {
                for( k1 = 0; k1 < RANSAC_MAX_ITERS; k1++ )
                {
    

    【讨论】:

    • 代码在您复制粘贴后发生了变化。现在直接调用estimateAffinePartial2D。
    猜你喜欢
    • 2021-11-23
    • 2018-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-16
    • 2016-02-09
    相关资源
    最近更新 更多