当前位置: 首页 > news >正文

opencv矩阵转eigen_numpy opencv matlab eigen SVD结果对比

参考

https://zhuanlan.zhihu.com/p/26306568

https://byjiang.com/2017/11/18/SVD/

http://www.bluebit.gr/matrix-calculator/

https://stackoverflow.com/questions/3856072/single-value-decomposition-implementation-c

https://stackoverflow.com/questions/35665090/svd-matlab-implementation

矩阵奇异值分解简介及C++/OpenCV/Eigen的三种实现

https://blog.csdn.net/fengbingchun/article/details/72853757

numpy.linalg.svd 源码

https://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.svd.html

计算矩阵的特征值与特征向量的方法

https://blog.csdn.net/Junerror/article/details/80222540

https://jingyan.baidu.com/article/27fa7326afb4c146f8271ff3.html

不同的库计算结果不一致

原因在于特征向量不唯一,特征值是唯一的

来源

https://stackoverflow.com/questions/35665090/svd-matlab-implementation

Both are correct... The rows of the v you got from numpy are the eigenvectors of M.dot(M.T) (the transpose would be a conjugate transpose in the complex case). Eigenvectors are in the general case defined only up to a multiplicative constant, so you could multiply any row of v by a different number, and it will still be an eigenvector matrix.

There is the additional constraint on v that it be a unitary matrix, which loosely translates to its rows being orthonormal. This reduces your available choices for every eigenvector to only 2: the normalized eigenvector pointing in either direction. But you still get to multiply any row by -1 and still have a valid v.

A = U * S * V

1 手动计算

给定一个大小为

的矩阵

,虽然这个矩阵是方阵,但却不是对称矩阵,我们来看看它的奇异值分解是怎样的。

进行对称对角化分解,得到特征值为

,相应地,特征向量为

;由

进行对称对角化分解,得到特征值为

当 lamda1 = 32

AA.T  -  lamda1*E = [-7 7

7 -7]

线性变换 【-1 1

0 0】

-x1 + x2 = 0

x1 = 1 x2 = 1 特征向量为【1 1】.T  归一化为【1/sqrt(2), 1/sqrt(2)】

x1 = -1 x2 = -1 特征向量为【-1 -1】.T  归一化为【-1/sqrt(2), -1/sqrt(2)】

当 lamda2 = 18

AA.T  -  lamda2*E = [7 7

7 7]

线性变换 【1 1

0 0】

x1 + x2 = 0

如果x1 = -1 x2 = 1 特征向量为【-1 1】.T  归一化为【-1/sqrt(2), 1/sqrt(2)】

如果 x1 = 1 x2 = -1 特征向量为【-1 1】.T  归一化为【1/sqrt(2), -1/sqrt(2)】可见特征向量不唯一

特征向量为

。取

,则矩阵

的奇异值分解为

.

2 MATLAB 结果与手动计算不同

AB = [[ 4 4],

[-3 3]]

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

U

S

V'#需要转置

AB =

4 4

-3 3

U =

-1 0

0 1

S =

5.6569 0

0 4.2426

V =

-0.7071 -0.7071

-0.7071 0.7071

3 NUMPY结果与手动计算不同, 与matlab相同 它们都是调用lapack的svd分解算法。

importnumpy as npimportmathimportcv2

a= np.array([[4,4],[-3,3]])#a = np.random.rand(2,2) * 10

print(a)

u, d, v=np.linalg.svd(a)print(u)print(d)print(v)#不需要转置

A = [[ 4 4]

[-3 3]]

U =

[[-1. 0.]

[ 0. 1.]]

S=

[5.65685425 4.24264069]

V=

[[-0.70710678 -0.70710678]

[-0.70710678 0.70710678]]

4 opencv结果 与手动计算结果相同

importnumpy as npimportmathimportcv2

a= np.array([[4,4],[-3,3]],dtype=np.float32)#a = np.random.rand(2,2) * 10

print(a)

S1, U1, V1=cv2.SVDecomp(a)print(U1)print(S1)print(V1)#不需要转置

a = [[ 4. 4.]

[-3. 3.]]

U =

[[0.99999994 0. ]

[0. 1. ]]

S = [[5.656854 ]

[4.2426405]]

V =

[[ 0.70710677 0.70710677]

[-0.70710677 0.70710677]]

5  eigen结果与手动计算相同

#include #include #include #include #include #include "opencv2/imgproc/imgproc.hpp"#include

using namespace std;

using namespace cv;//using Eigen::MatrixXf;

using namespace Eigen;

using namespace Eigen::internal;

using namespace Eigen::Architecture;

int GetEigenSVD(Mat&Amat, Mat &Umat, Mat &Smat, Mat &Vmat)

{//-------------------------------svd测试 eigen

Matrix2f A;

A(0,0)=Amat.at(0,0);

A(0,1)=Amat.at(0,1);

A(1,0)=Amat.at(1,0);

A(1,1)=Amat.at(1,1);

JacobiSVD<: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

//Matrix2f Arestore = U * S *V.transpose();// printeEigenMat(A ,"/sdcard/220/Ae.txt");// printeEigenMat(U ,"/sdcard/220/Ue.txt");// printeEigenMat(S ,"sdcard/220/Se.txt");// printeEigenMat(V ,"sdcard/220/Ve.txt");// printeEigenMat(U * S * V.transpose() ,"sdcard/220/U*S*VTe.txt");

Umat.at(0,0) =U(0,0);

Umat.at(0,1) = U(0,1);

Umat.at(1,0) = U(1,0);

Umat.at(1,1) = U(1,1);

Vmat.at(0,0) =V(0,0);

Vmat.at(0,1) = V(0,1);

Vmat.at(1,0) = V(1,0);

Vmat.at(1,1) = V(1,1);

Smat.at(0,0) =S(0,0);

Smat.at(0,1) = S(0,1);

Smat.at(1,0) = S(1,0);

Smat.at(1,1) = S(1,1);// Smat.at(0,0) =S(0,0);// Smat.at(0,1) = S(1,1);//-------------------------------svd测试 eigenreturn0;

}

int main()

{//egin();//opencv3();//Eigentest();//opencv();//similarityTest();// double data[2][2] = { { 629.70374, 245.4427},// { -334.8119 , 862.1787} };

double data[2][2] = { { 4, 4},

{-3, 3} };

int dim= 2;

Mat A(dim,dim, CV_64FC1, data);

Mat U(dim, dim, CV_64FC1);

Mat V(dim, dim, CV_64FC1);

Mat S(dim, dim, CV_64FC1);

GetEigenSVD(A, U, S, V);

Mat Arestore= U * S *V.t();

cout<

cout<

cout<

cout<

cout<

cout<

}

[4, 4;

-3, 3]

U =

[0.9999999403953552, 0;

0, 0.9999999403953552]

S =

[5.656854629516602, 0;

0, 4.242640972137451]

V =

[0.7071067690849304, 0.7071067690849304;

-0.7071067690849304, 0.7071067690849304]

6 在线计算网站 与手动计算不同

http://www.bluebit.gr/matrix-calculator/calculate.aspx

Input matrix:

4.000 4.000

-3.000 3.000

Singular Value Decomposition:

U:

-1.000 0.000

0.000 1.000

S:

5.657 0.000

0.000 4.243

VT

-0.707 -0.707

-0.707 0.707

相关文章:

  • 及cp含义_电气设计图纸中AL、AW、HAL等各种符号分别代表什么含义?
  • 对当前虚拟货币问题的思考(下)
  • 光照系统可以工作了。
  • 组播vlan_单播地址、组播地址、广播地址的优缺点
  • 打印表单_重磅更新 | 表单提交校验逻辑、轮播图纷纷上线
  • 期待能好好好好好好的睡一觉。
  • (转)编辑寄语:因为爱心,所以美丽
  • vue 前端设置允许跨域_Vue3.X脚手架项目设置proxy前端跨域
  • SSIS循环导入多个同表结构不同表明的excel
  • docker logs写入文件_Docker 日志的 10 大陷阱
  • long转string mybatis_mybatis split sring(mybatis 返回string)
  • X3DAudio
  • shell 删除了hdfs 文件_Hadoop 系列(六)—— HDFS 常用 Shell 命令
  • IE 里面的activeX控件的打印预览问题
  • mysql特定权限_Mysql中设置指定IP的特定用户及特定权限
  • 【407天】跃迁之路——程序员高效学习方法论探索系列(实验阶段164-2018.03.19)...
  • 【刷算法】求1+2+3+...+n
  • Android交互
  • canvas 五子棋游戏
  • Java到底能干嘛?
  • js 实现textarea输入字数提示
  • Linux编程学习笔记 | Linux多线程学习[2] - 线程的同步
  • Logstash 参考指南(目录)
  • MySQL-事务管理(基础)
  • nginx(二):进阶配置介绍--rewrite用法,压缩,https虚拟主机等
  • Octave 入门
  • Perseus-BERT——业内性能极致优化的BERT训练方案
  • Redis 懒删除(lazy free)简史
  • 阿里云应用高可用服务公测发布
  • 订阅Forge Viewer所有的事件
  • 问:在指定的JSON数据中(最外层是数组)根据指定条件拿到匹配到的结果
  • 源码安装memcached和php memcache扩展
  • 06-01 点餐小程序前台界面搭建
  • 湖北分布式智能数据采集方法有哪些?
  • 继 XDL 之后,阿里妈妈开源大规模分布式图表征学习框架 Euler ...
  • # Swust 12th acm 邀请赛# [ K ] 三角形判定 [题解]
  • # 深度解析 Socket 与 WebSocket:原理、区别与应用
  • #Linux(帮助手册)
  • (接口封装)
  • (五)IO流之ByteArrayInput/OutputStream
  • (译) 理解 Elixir 中的宏 Macro, 第四部分:深入化
  • (转)C#开发微信门户及应用(1)--开始使用微信接口
  • (转)Google的Objective-C编码规范
  • .bat批处理(五):遍历指定目录下资源文件并更新
  • .htaccess配置重写url引擎
  • .NET 设计模式初探
  • .net 微服务 服务保护 自动重试 Polly
  • @configuration注解_2w字长文给你讲透了配置类为什么要添加 @Configuration注解
  • @private @protected @public
  • [android]-如何在向服务器发送request时附加已保存的cookie数据
  • [BUG] Hadoop-3.3.4集群yarn管理页面子队列不显示任务
  • [c++] 什么是平凡类型,标准布局类型,POD类型,聚合体
  • [C++]高精度 bign (重载运算符版本)
  • [CakePHP] 在Controller中使用Helper
  • [CDOJ 838]母仪天下 【线段树手速练习 15分钟内敲完算合格】