如果我在estimateRigidTransform或getAffineTransform中提供更多输入,会发生什么情况? [英] What happens if I give more inputs in estimateRigidTransform or getAffineTransform?

查看:267
本文介绍了如果我在estimateRigidTransform或getAffineTransform中提供更多输入,会发生什么情况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有两个大约100点的向量的estimateRigidTransform,并且正在运行FINE. 但是以某种方式getAffineTransform无法正常工作.

I am using estimateRigidTransform with about two vectors of 100 points and is working FINE. But somehow getAffineTransform doesn't work.

我知道findHomography使用RANSAC可以找到最佳矩阵,而getPerspectiveTransform仅需要4点.

I know that findHomography finds the best matrix using RANSAC and getPerspectiveTransform needs only 4 points.

我的问题是,如果我在EstimateRigidTransform或getAffineTransform中提供更多输入会发生什么?

My question is what happens if I give more inputs in estimateRigidTransform or getAffineTransform?

从输入矩阵中仅获取4个点吗?还是做某种RANSAC?

Does it take only 4 points from input matrix? Or do some kind of RANSAC?

推荐答案

您提到的函数可以分为3种类型:

The functions you mentioned can be split into 3 different types:

类型1:getAffineTransform和getPerspectiveTransform.在一个平面上给定3个点,在另一个平面上给定3个匹配点,您可以计算这些平面之间的仿射变换.给定4个点,您可以找到透视变换.这就是getAffineTransform和getPerspectiveTransform可以做的所有事情:它们需要3和4对点,不少于不多,并计算相关的变换.唯一的.

Type 1: getAffineTransform and getPerspectiveTransform. Given 3 points on one plane and 3 matching points on another you can calculate affine transform between those planes. And given 4 points you can find perspective transform. This is all what getAffineTransform and getPerspectiveTransform can do: they require 3 and 4 pairs of points, no more no less, and calculate relevant transform. The one and the only.

类型2:estimateRigidTransform.如果无法以绝对精度获取点(通常是从图像获取点的情况),则需要3对以上的点来减少误差.越多越好(即更好的准确性).定义错误要减少的方法不止一种,还有使用哪种方法来查找最小错误. estimateRigidTransform最小化最小平方误差(我认为是最流行的误差定义).它通过求解方程组来实现.如果您精确地提供3分,那么结果当然将与getAffineTransform的结果相同.您可以问一下,如果estimateRigidTransform可以完成其工作,为什么在OpenCV中需要getAffineTransform. las,这不是OpenCV中唯一的冗余.

Type 2: estimateRigidTransform. If you can't get your points with absolute precision (which is normally the case when you get them from an image) then you need more than 3 pairs of points to reduce the error. The more the merrier (i.e. better accuracy). There more than one way to define the error you want to reduce, as well as what approach you use to find the minimal error. estimateRigidTransform is minimizing least square error (I think the most popular error definition). It does this by solving system of equations. If you provide 3 point exactly then the result, of course, will be the same as result of getAffineTransform. You can ask why we need getAffineTransform in OpenCV if estimateRigidTransform can do its work. Alas, this is not the only redundancy in OpenCV.

类型3:findHomography.这是更高级的.它不仅可以处理点位置的错误,而且还可以处理异常值的存在.如果点之间存在某些错误匹配,则将它们用于最小二乘方误差估计将导致非常差的精度.它可以使用RANSAC或LMeD来测试可能的匹配并消除那些异常值.它的工作方式类似于EstimateRigidTransform的多次迭代:查找不同点子集的最小二乘匹配.如果您知道不存在离群值,则可以将"method"参数设置为0,它会像估计RigidTransform一样工作-通过尝试最小化由所有点的匹配产生的最小平方误差.

Type 3: findHomography. This one is more advanced. Not only it can deal with errors in locations of points, but it can also deal with existence of the outliers. If there some wrong matches between points then using them for least square error estimation will lead to very bad accuracy. It can use RANSAC or LMeDs to test possible matches and eliminate those outliers. It works like multiple iterations of estimateRigidTransform: find least square match for different sub-sets of points. If you know that no outliers are present then you can set the 'method' argument to 0, and it will work like estimateRigidTransform - by trying to minimize least square error created from match of all points.

编辑.感谢Micka的评论.

我想我的记忆正在欺骗我.我记得估计RigidTransform是通过OpenCV中的方程组实现的,但是现在我检查了一下,发现米卡是对的.它确实在其中使用了一些硬编码的RANSAC ...很抱歉误导您.

I guess my memory is playing tricks on me. I remembered that estimateRigidTransform was implemented through system of equations in OpenCV, but now I checked it and saw that Micka is right. It does uses some hard coded RANSAC in it... Sorry for misguiding you.

对于那些仍然对拥有封闭式解决方案而不是RANSAC感兴趣的人,这里是:

For those who are still interested in having closed form solution instead of RANSAC, here it is:

// find affine transformation between two pointsets (use least square matching)
static bool computeAffine(const vector<Point2d> &srcPoints, const vector<Point2d> &dstPoints, Mat &transf)
{
    // sanity check
    if ((srcPoints.size() < 3) || (srcPoints.size() != dstPoints.size()))
        return false;

    // container for output
    transf.create(2, 3, CV_64F);

    // fill the matrices
    const int n = (int)srcPoints.size(), m = 3;
    Mat A(n,m,CV_64F), xc(n,1,CV_64F), yc(n,1,CV_64F);
    for(int i=0; i<n; i++)
    {
        double x = srcPoints[i].x, y = srcPoints[i].y;
        double rowI[m] = {x, y, 1};
        Mat(1,m,CV_64F,rowI).copyTo(A.row(i));
        xc.at<double>(i,0) = dstPoints[i].x;
        yc.at<double>(i,0) = dstPoints[i].y;
    }

    // solve linear equations (for x and for y)
    Mat aTa, resX, resY;
    mulTransposed(A, aTa, true);
    solve(aTa, A.t()*xc, resX, DECOMP_CHOLESKY);
    solve(aTa, A.t()*yc, resY, DECOMP_CHOLESKY);

    // store result
    memcpy(transf.ptr<double>(0), resX.data, m*sizeof(double));
    memcpy(transf.ptr<double>(1), resY.data, m*sizeof(double));

    return true;
}

这篇关于如果我在estimateRigidTransform或getAffineTransform中提供更多输入,会发生什么情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆