performance - 矩阵求逆的最快方法

标签 performance algorithm opencv linear-algebra matrix-inverse

我想用反函数和很多函数处理图像。为了使代码快速运行,可以在 3 种反演方法中推荐快速方法吗?

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

最佳答案

在 OpenCV2.x 中,有一个名为 Mat::inv(int method) 的新接口(interface)来计算矩阵的逆。参见 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.

我对每种方法都进行了测试,结果表明 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);
}

运行结果如下:

LU:0.000423759

DECOMP_SVD:0.0583525

DECOMP_CHOLESKY:9.3453e-05

关于performance - 矩阵求逆的最快方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11040333/

相关文章:

c++ - Opencv 程序退出,代码为 -1073741819

multithreading - 多线程能提高性能吗?如何?

algorithm - 查询以确定点是否位于多边形内

algorithm - 找出n个不同数组中共有的最大元素?

python - 获取图像中矩形的数量

python - 从扫描的工程图纸中提取文本

java - Java 风格的 Groovy 和 Java 一样快吗?

java - JSF 真的准备好迎接高性能社交网络项目了吗?

performance - 使用大量小文件改进 Fortran 格式的 I/O

algorithm - 是否有优化的方法来查找 3D 立方体的最远角