SVD分解 Eigen库 opencv库
opencv
OpenCV: 开源计算机视觉库
项目地址:https://gitcode.com/gh_mirrors/opencv31/opencv

·
如题,使用库函数进行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;
}
opencv
OpenCV: 开源计算机视觉库
项目地址:https://gitcode.com/gh_mirrors/opencv31/opencv
#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;
}
推荐内容
阅读全文
AI总结




OpenCV: 开源计算机视觉库
最近提交(Master分支:7 个月前 )
6ef57463
Migrated IPP impl for flip and transpose to HAL 4 天前
c1d71d53
imgproc: disable SIMD for compareHist(INTERSECT) if f64 is unsupported #27220
Close https://github.com/opencv/opencv/issues/24757
### Pull Request Readiness Checklist
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
Patch to opencv_extra has the same branch name.
- [ ] The feature is well documented and sample code can be built with the project CMake
4 天前
更多推荐
相关推荐
查看更多
opencv

OpenCV: 开源计算机视觉库
opencv

opencv

R bindings for OpenCV
热门开源项目
活动日历
查看更多
直播时间 2025-04-09 14:34:18

樱花限定季|G-Star校园行&华中师范大学专场
直播时间 2025-04-07 14:51:20

樱花限定季|G-Star校园行&华中农业大学专场
直播时间 2025-03-26 14:30:09

开源工业物联实战!
直播时间 2025-03-25 14:30:17

Heygem.ai数字人超4000颗星火燎原!
直播时间 2025-03-13 18:32:35

全栈自研企业级AI平台:Java核心技术×私有化部署实战
所有评论(0)