矩阵逆的最快方法

nbs*_*jan 3 algorithm performance opencv linear-algebra matrix-inverse

我想处理具有反函数和许多函数的图像.对于快速运行的代码,可以在3种反演方法中建议快速方法吗?

double cvInvert(const CvArr* src, CvArr* dst, int method=CV_LU)
Run Code Online (Sandbox Code Playgroud)
  • CV_LU高斯消除,选择最佳枢轴元素
  • CV_SVD奇异值分解(SVD)方法
  • CV_SVD_SYM用于对称正定义矩阵的SVD方法.

小智 7

在OpenCV2.x中,有一个新的接口被称为Mat::inv(int method)计算矩阵的逆.见参考文献.

C++:MatExpr Mat :: inv(int method = DECOMP_LU)const

参数:方法 -

   Matrix inversion method. Possible values are the following:
        DECOMP_LU is the LU decomposition. The matrix must be non-singular.
        DECOMP_CHOLESKY is the Cholesky LL^T decomposition for symmetrical positively defined matrices only. This type is about twice faster than LU on big matrices.
        DECOMP_SVD is the SVD decomposition. If the matrix is singular or even non-square, the pseudo inversion is computed.
Run Code Online (Sandbox Code Playgroud)

我用每个方法做了一个测试,它表明DECOMP_CHOLESKY对于测试用例来说是最快的,而LU给出了类似的结果.

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>

int main(void)
{
    cv::Mat img1 = cv::imread("2.png");
    cv::Mat img2, img3, img;
    cv::cvtColor(img1, img2, CV_BGR2GRAY);
    img2.convertTo(img3, CV_32FC1);
    cv::resize(img3, img, cv::Size(200,200));

    double freq = cv::getTickFrequency();

    double t1 = 0.0, t2 = 0.0;
    t1 = (double)cv::getTickCount();
    cv::Mat m4 = img.inv(cv::DECOMP_LU);
    t2 = (cv::getTickCount()-t1)/freq;
    std::cout << "LU:" << t2 << std::endl;

    t1 = (double)cv::getTickCount();
    cv::Mat m5 = img.inv(cv::DECOMP_SVD);
    t2 = (cv::getTickCount()-t1)/freq;
    std::cout << "DECOMP_SVD:" << t2 << std::endl;

    t1 = (double)cv::getTickCount();
    cv::Mat m6 = img.inv(cv::DECOMP_CHOLESKY);
    t2 = (cv::getTickCount()-t1)/freq;
    std::cout << "DECOMP_CHOLESKY:" << t2 << std::endl;

    cv::waitKey(0);
}
Run Code Online (Sandbox Code Playgroud)

这是正在运行的resutls:

卢:0.000423759

DECOMP_SVD:0.0583525

DECOMP_CHOLESKY:9.3453e-05