赞
踩
如题,使用库函数进行svd分解,形如 A = U * S * VT.
Eigen 库:
- #include <iostream>
- #include <Eigen/SVD>
- #include <Eigen/Dense>
-
- //using Eigen::MatrixXf;
- using namespace Eigen;
- using namespace Eigen::internal;
- using namespace Eigen::Architecture;
-
- int main()
- {
- //-------------------------------svd测试 eigen
- Matrix3f A;
- A(0,0)=1,A(0,1)=0,A(0,2)=1;
- A(1,0)=0,A(1,1)=1,A(1,2)=1;
- A(2,0)=0,A(2,1)=0,A(2,2)=0;
- JacobiSVD<Eigen::MatrixXf> svd(A, ComputeThinU | ComputeThinV );
- Matrix3f V = svd.matrixV(), U = svd.matrixU();
- Matrix3f S = U.inverse() * A * V.transpose().inverse(); // S = U^-1 * A * VT * -1
- std::cout<<"A :\n"<<A<<std::endl;
- std::cout<<"U :\n"<<U<<std::endl;
- std::cout<<"S :\n"<<S<<std::endl;
- std::cout<<"V :\n"<<V<<std::endl;
- std::cout<<"U * S * VT :\n"<<U * S * V.transpose()<<std::endl;
- system("pause");
- //-------------------------------svd测试 eigen
-
- return 0;
- }

- #include <opencv2/core/core.hpp>
- #include <opencv2/highgui/highgui.hpp>
- #include"opencv2/imgproc/imgproc.hpp"
- #include <iostream>
-
- using namespace std;
- using namespace cv;
-
- 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 main ()
- {
- float abt[3 * 3] = {
- 1,0,1,
- 0,1,1,
- 0,0,0
- };
- float abt_d[3 * 3]={0}, abt_u[3 * 3]={0}, abt_v[3 * 3]={0};
-
- CvMat ABt = cvMat(3, 3, CV_64F, abt);//CvMat 取对数组的引用而不是拷贝
- CvMat ABt_D = cvMat(3, 3, CV_64F, abt_d);
- CvMat ABt_U = cvMat(3, 3, CV_64F, abt_u);
- CvMat ABt_VT = cvMat(3, 3, CV_64F, abt_v);
-
- cvSVD(&ABt, &ABt_D, &ABt_U, &ABt_VT, CV_SVD_V_T);//最后一个参数用于控制返回 UT或U VT或V
- std::cout<<"A : "<<std::endl;
- print(ABt);
- std::cout<<"U : "<<std::endl;
- print(ABt_U);
- std::cout<<"S : "<<std::endl;
- print(ABt_D);
- std::cout<<"V : "<<std::endl;
- print(ABt_VT);
- system("pause");
- return 0;
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。