在矩阵的逆最快方法 [英] Fastest method in inverse of matrix
问题描述
我要处理与反逻辑功能和功能很多照片。对于code,以快速度可以运行任何一个建议中的3反演方法快速的方法?
双cvInvert(常量CvArr * SRC,CvArr * DST,诠释方法= CV_LU)
- CV_LU高斯消元法具有最佳的支点元素选择
- CV_SVD奇异值分解(SVD)方法
- CV_SVD_SYM SVD方法对称正定义的矩阵。
在OpenCV2.x,有一个所谓的新界面垫:: INV(INT方法)
来计算一矩阵的逆。请参见参考。
C ++:MatExpr垫:: INV(INT方法= DECOMP_LU)常量
参数: 方法 -
矩阵求逆方法。可能的值如下: DECOMP_LU是LU分解。基质必须是非奇异的。 DECOMP_CHOLESKY是乔里斯基LL ^ T分解仅供对称正定矩阵。这种类型的两倍左右,比卢在大型矩阵更快。 DECOMP_SVD是SVD分解。如果矩阵是奇异的,甚至非正方形,伪反演计算。
我与各该方法的试验中,它表明DECOMP_CHOLESKY是最快的测试的情况下,和卢给出类似的结果。
的#include< opencv2 /核心/ core.hpp>
#包括< opencv2 /一下HighGUI / highgui.hpp>
#包括< opencv2 / imgproc / imgproc.hpp>
#包括<的iostream>
INT主要(无效)
{
CV ::垫IMG1 = CV :: imread(2.png);
CV ::垫IMG2,IMG3,IMG;
CV :: cvtColor(IMG1,IMG2,CV_BGR2GRAY);
img2.convertTo(IMG3,CV_32FC1);
CV ::调整(IMG3,IMG,CV ::尺寸(200,200));
双频率= CV :: getTickFrequency();
双T1 = 0.0,T2 = 0.0;
T1 =(双)CV ::的GetTickCount();
CV ::垫M4 = img.inv(CV :: DECOMP_LU);
T2 =(CV ::的GetTickCount() - T1)/频率;
性病::法院<< 卢:<< T2<<的std :: ENDL;
T1 =(双)CV ::的GetTickCount();
CV ::垫M5 = img.inv(CV :: DECOMP_SVD);
T2 =(CV ::的GetTickCount() - T1)/频率;
性病::法院<< DECOMP_SVD:&其中;&其中; T2<<的std :: ENDL;
T1 =(双)CV ::的GetTickCount();
CV ::垫M6 = img.inv(CV :: DECOMP_CHOLESKY);
T2 =(CV ::的GetTickCount() - T1)/频率;
性病::法院<< DECOMP_CHOLESKY:&其中;&其中; T2<<的std :: ENDL;
CV :: waitKey(0);
}
下面是运行resutls:
LU:0.000423759
DECOMP_SVD:0.0583525
DECOMP_CHOLESKY:9.3453e-05
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.
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
这篇关于在矩阵的逆最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!