Fastest method in inverse of matrix

一曲冷凌霜 提交于 2019-11-30 18:33:51

问题


I want to process Images with Inverse function and lot of functions. For code to run fastly can any one suggest fast method among the 3 inversion methods ?

double cvInvert(const CvArr* src, CvArr* dst, int method=CV_LU)
  • CV_LU Gaussian elimination with optimal pivot element chosen
  • CV_SVD Singular value decomposition (SVD) method
  • CV_SVD_SYM SVD method for a symmetric positively-defined matrix.

回答1:


In OpenCV2.x, there's a new interface called Mat::inv(int method) to compute the inverse of a matrix. See reference.

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

Parameters: method –

   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.

I made a test with each of the method, it shows that DECOMP_CHOLESKY is the fastest for the test case, and LU gives the similar result.

#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);
}

Here is the running resutls:

LU:0.000423759

DECOMP_SVD:0.0583525

DECOMP_CHOLESKY:9.3453e-05



来源:https://stackoverflow.com/questions/11040333/fastest-method-in-inverse-of-matrix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!