7

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 ?

4
  • related: answers.opencv.org/question/6747/…
    – Miki
    Commented Oct 15, 2015 at 9:36
  • Here it is mentioned to use estimateRigidTransform that seems to use RANSAC internally,
    – Miki
    Commented Oct 15, 2015 at 9:41
  • 1
    if you know how to compute a 2D transformation from 3 point pairs, you can easily code your own simple RANSAC. Afaik, estimateRigidTransform uses ransac, but unfortunately you can't parametrize it...
    – Micka
    Commented Oct 15, 2015 at 15:27
  • OK Thanks.. So there is no ready solution.. Commented Oct 16, 2015 at 5:30

2 Answers 2

12

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)
7

estimateRigidTransform does use RANSAC internally, though the parameters are fixed at the moment - see the code here - 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++ )
            {
1
  • 1
    The code had changed since your copy-paste. It directly calls estimateAffinePartial2D now.
    – ZettaCircl
    Commented May 8, 2019 at 9:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.