numpy opencv matlab eigen SVD结果对比

Posted adong7639

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了numpy opencv matlab eigen SVD结果对比相关的知识,希望对你有一定的参考价值。

numpy与opencv

AB = np.array([[ 629.70374,  245.4427 ],[-334.8119 ,  862.1787 ]])
#AB = np.array([[ 629.70374,  245.4427 ],[334.8119 ,  862.1787 ]])
print(AB)
U, S, V = np.linalg.svd(AB)
S1, U1, V1 = cv2.SVDecomp(AB)
S1 = np.reshape(S1,(2))
print("np")
print("U")
print(U)
print(U1)
print("S")
print(S)
print(S1)
print("V")
print(V)
print(V1)

结果不一样,符号也不一样

[[-370.82513189 176.4954924 ]
[-331.78746884 -429.58294685]]
np
U
[[-0.3161301 -0.94871585]
[-0.94871585 0.3161301 ]]
[[-0.3161301 0.94871585]
[-0.94871585 -0.3161301 ]]
S
[557.09747058 391.06109274]
[557.09747058 391.06109274]
V
[[ 0.77544961 0.63140946]
[ 0.63140946 -0.77544961]]
[[ 0.77544961 0.63140946]
[-0.63140946 0.77544961]]

 

matlab 结果与numpy一致

AB = [[ 629.70374 245.4427 ],
[-334.8119 862.1787 ]]

[U,S,V] = svd(AB)

 

AB =

629.7037 245.4427
-334.8119 862.1787


U =

0.0020 1.0000
1.0000 -0.0020


S =

924.9068 0
0 675.8457


V =

-0.3607 0.9327
0.9327 0.3607

 

egine与opencv  egine与matlab以及numpy结果一致

#include <iostream>
#include <Eigen/SVD>
#include <Eigen/Dense>  
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
 
using namespace std;
using namespace cv;
  
//using Eigen::MatrixXf;  
using namespace Eigen;  
using namespace Eigen::internal;  
using namespace Eigen::Architecture;  
 
int Eigentest()
{
//-------------------------------svd测试    eigen
    Matrix2f A;
    float data[2][2] = {{629.70374, 245.4427 },{-334.8119 , 862.1787 }};
    A(0,0)=data[0][0];
    A(0,1)=data[0][1];
    A(1,0)=data[1][0];
    A(1,1)=data[1][1];

    JacobiSVD<Eigen::MatrixXf> svd(A, ComputeThinU | ComputeThinV );
    Matrix2f V = svd.matrixV(), U = svd.matrixU();
    Matrix2f  S = U.inverse() * A * V.transpose().inverse(); // S = U^-1 * A * VT * -1
    std::cout<<"A :
"<<A<<std::endl;
    std::cout<<"U :
"<<U<<std::endl;
    std::cout<<"S :
"<<S<<std::endl;
    std::cout<<"V :
"<<V<<std::endl;
    std::cout<<"U * S * VT :
"<<U * S * V.transpose()<<std::endl;
    //system("pause");
    //-------------------------------svd测试    eigen
 
    return 0;
}


 
void print(CvMat& m){
    for (int row = 0; row < m.rows; row++){
        float* ptr = (float*)(m.data.ptr + row * m.step);//第row行数据的起始指针
        for (int col = 0; col < m.cols; col++)
            cout<<*(ptr+3*col)<<"     ";
        std::cout<<std::endl;
    }
}

int opencv()
{
    // Mat img = imread("dazu.jpg");
    // if(img.empty())
    // {
    //     cout<<"error";
    //     return -1;
    // }
    // imshow("mypic",img);
    // waitKey();
    // # [[ 629.70374  245.4427 ]
    // #  [-334.8119   862.1787 ]]
    float data[2][2] = {{629.70374, 245.4427 },{-334.8119 , 862.1787 }};
    Mat A = Mat(2,2, CV_32FC1, data);
    cout << A << endl;
    //SVD::compute(InputArray src, OutputArray w, OutputArray u, OutputArray vt, int flags=0 )
    Mat S,U,V;
    SVD::compute(A, S,U,V);
    cout << U << endl;
    cout << S << endl;
    cout << V << endl;

    return 0;
}


int main()
{
    Eigentest();
    opencv();
    return 0;
}


A :
629.704 245.443
-334.812 862.179
U :
0.00196439 0.999998
0.999998 -0.00196439
S :
924.907 -3.05176e-05
0 675.846
V :
-0.360657 0.932698
0.932698 0.360657
U * S * VT :
629.704 245.443
-334.812 862.179
[629.70374, 245.4427;
-334.81189, 862.17871]
[0.0019644245, -0.99999809;
0.99999809, 0.0019644082]
[924.90686;
675.84564]
[-0.36065713, 0.93269849;
-0.93269849, -0.36065713]

 















































以上是关于numpy opencv matlab eigen SVD结果对比的主要内容,如果未能解决你的问题,请参考以下文章

Matlab中的eig函数和eigs函数的异同点

对于 Hermitian 矩阵,numpy.linalg.eig 给出的结果与 numpy.linalg.eigh 不同

numpy opencv matlab eigen SVD结果对比

转:numpy.linalg.eig() 计算矩阵特征向量

如何将 Numpy Eig 与 Python Lambda 函数一起使用?

Matlab eig 函数中的 nobalance 选项到底是啥?