c++opencvaffinetransformhomographyransac

Estimate 2D transformation between two sets of points using RANSAC


As I know, OpenCV uses RANSAC in order to solve the problem of findHomography and it returns some useful parameters like the homograph_mask.

However, if I want to estimate just 2D transformation which means an Affine Matrix, is there a way to use the same methodology of findHomography which uses RANSAC and return that mask ?


Solution

  • You can directly use 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 
    )   
    

    for example :

            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)