Matrix inverse

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

### 使用Simulink中的Matrix Inverse模块 #### 1. 功能描述 `Matrix Inverse` 模块用于计算输入矩阵的逆矩阵。该模块适用于方阵,并假设输入矩阵是非奇异(即可逆)的。如果输入矩阵接近奇异,则可能会导致数值不稳定的结果。 #### 2. 参数设置 当放置 `Matrix Inverse` 模块到模型中后,可以通过双击打开其属性对话框来查看或修改参数选项。主要关注的是算法的选择,默认情况下采用LU分解方法来进行矩阵求逆操作[^1]。 #### 3. 实际应用案例 为了更好地理解这个模块的应用场景,考虑一个简单的例子——解决线性方程组 Ax=b 的问题。这里 A 是已知系数构成的 n×n 方阵;b 表示常数项组成的列向量;而未知变量 x 可以表示为 bA⁻¹ 形式,即先对 A 进行求逆再乘以 b 向量得到最终解。 ```matlab % MATLAB命令行代码片段展示如何构建上述关系对应的Simulink模型 sys = 'matrix_inverse_example'; new_system(sys); add_block('simulink/Math Operations/Matrix Multiply', [sys '/Multiply']); add_block('simulink/Math Operations/Matrix Inverse', [sys '/Inverse']); add_block('simulink/Sources/Constant',[sys '/B_Constant']); % 常数项b set_param([sys '/B_Constant'],'Value','[1;2]'); add_block('simulink/Sinks/Display',[sys '/X_Display']); % 显示结果x connect_lines; open_system(sys); ``` 此脚本创建了一个名为 `matrix_inverse_example` 的新 Simulink 模型文件,其中包括必要的组件连接以实现从给定矩阵 B 计算 X=A⁻¹*B 的过程[^2]。 #### 4. 注意事项 - 输入必须是一个有效的非零行列式的方阵。 - 对于大型稀疏矩阵或其他特殊情况,可能需要评估性能影响以及是否有更高效的替代方案存在。 - 如果不确定所使用的矩阵是否适合直接求逆运算,建议预先对其进行条件数分析,确保数值稳定性[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值