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

tid*_*idy 4 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.
Run Code Online (Sandbox Code Playgroud)

mar*_*rol 9

使用矩阵求逆的方法:

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

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

但是,由于矩阵求逆的成本较高,并且从数值方法的角度来看,精度会降低,因此不建议这样做.此外,它只能求解非过度定义的线性方程组,系统矩阵A必须是可逆的.

使用矩阵转置的方法:

由于我们有xA = b,所以(xA)^ T = b ^ T <=> A ^ T*x ^ T = b ^ T,所以我们可以使用cv :: solve(At(),bt(),x ),和xt()是一个结果:

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

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