在opencv中是否有解决xA = b的函数?

时间:2014-05-20 02:09:43

标签: opencv computer-vision linear-regression

我知道函数solve可以解决Ax=b。但我想要一个函数来解决x的xA = b? 有一些功能吗?

顺便说一下它应该像Matlab的mrdivide一样工作:

x = B/A solves the system of linear equations x*A = B for x. The matrices A and B must contain the same number of columns. MATLAB® displays a warning message if A is badly scaled or nearly singular, but performs the calculation regardless.

If A is a scalar, then B/A is equivalent to B./A.

If A is a square n-by-n matrix and B is a matrix with n columns, then x = B/A is a solution to the equation x*A = B, if it exists.

If A is a rectangular m-by-n matrix with m ~= n, and B is a matrix with n columns, then x = B/A returns a least-squares solution of the system of equations x*A = B.

1 个答案:

答案 0 :(得分:9)

使用矩阵求逆的方法:

如果xA = b,则x * A * inv(A)= b * inv(A)< => x = b * inv(A)< =&gt ;,所以在opencv中你会得到:

void mrdivide(const cv::Mat &A, const cv::Mat &b, cv::Mat &x) {
    x = b * A.inv();
}

然而,由于矩阵反演成本高,从数值方法论的角度来看,不建议这样做,因此精度会下降。此外,它只能求解非过度定义的线性方程组,系统矩阵A必须是可逆的。

使用矩阵转置的方法:

由于我们有xA = b,所以(xA)^ T = b ^ T< => A ^ T * x ^ T = b ^ T,所以我们可以使用cv :: solve(A.t(),b.t(),x),而结果是x.t():

void mrdivide(const cv::Mat &A, const cv::Mat &b, cv::Mat &x) {
     cv::solve(A.t(), b.t(), x);
     cv::transpose(x,x);
}

这是一般性和推荐的解决方案。它提供了solve()所具有的所有功能。