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分解算法。
import numpy as np
import math
import cv2
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结果 与手动计算结果相同
import numpy as np
import math
import cv2
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 <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 GetEigenSVD(Mat &Amat, Mat &Umat, Mat &Smat, Mat &Vmat)
{
//-------------------------------svd测试 eigen
Matrix2f A;
A(0,0)=Amat.at<double>(0,0);
A(0,1)=Amat.at<double>(0,1);
A(1,0)=Amat.at<double>(1,0);
A(1,1)=Amat.at<double>(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
//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<double>(0,0) = U(0,0);
Umat.at<double>(0,1) = U(0,1);
Umat.at<double>(1,0) = U(1,0);
Umat.at<double>(1,1) = U(1,1);
Vmat.at<double>(0,0) = V(0,0);
Vmat.at<double>(0,1) = V(0,1);
Vmat.at<double>(1,0) = V(1,0);
Vmat.at<double>(1,1) = V(1,1);
Smat.at<double>(0,0) = S(0,0);
Smat.at<double>(0,1) = S(0,1);
Smat.at<double>(1,0) = S(1,0);
Smat.at<double>(1,1) = S(1,1);
// Smat.at<double>(0,0) = S(0,0);
// Smat.at<double>(0,1) = S(1,1);
//-------------------------------svd测试 eigen return 0;
} 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 <<A<<endl;
cout <<Arestore<< endl;
cout <<"U " << U<<endl;
cout <<"S " <<S<<endl;
cout <<"V " << V.t()<<endl;
cout <<V<<endl; return 0;
}
[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
numpy opencv matlab eigen SVD结果对比的更多相关文章
- MATLAB与C语言对比实例:随机数生成
MATLAB与C语言对比实例:随机数生成 作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ 一.整型随机数生成函数 1.C语言程序 int intrand ...
- OpenCV特征点检测算法对比
识别算法概述: SIFT/SURF基于灰度图, 一.首先建立图像金字塔,形成三维的图像空间,通过Hessian矩阵获取每一层的局部极大值,然后进行在极值点周围26个点进行NMS,从而得到粗略的特征点, ...
- 相机标定与矫正opencv+MATLAB
博客转载自:http://blog.csdn.net/Loser__Wang/article/details/51811347 本文目的在于记录如何使用MATLAB做摄像机标定,并通过opencv进行 ...
- OpenCV进行图像相似度对比的几种办法
转载请注明出处:http://blog.csdn.net/wangyaninglm/article/details/43853435, 来自:shiter编写程序的艺术 对计算图像相似度的方法,本文做 ...
- NumPy for MATLAB users
http://mathesaurus.sourceforge.net/matlab-numpy.html Help MATLAB/Octave Python Description dochelp - ...
- 人脸检测学习笔记(数据集-DLIB人脸检测原理-DLIB&OpenCV人脸检测方法及对比)
1.Easily Create High Quality Object Detectors with Deep Learning 2016/10/11 http://blog.dlib.net/201 ...
- numpy和matlab计算协方差矩阵的不同(matlab是标准的,numpy相当于转置后计算)
matlab是标准的,numpy相当于转置后计算 >> x = [2,0,-1.4;2.2,0.2,-1.5;2.4,0.1,-1;1.9,0,-1.2] x = 2.0000 0 ...
- 相机标定过程(opencv) + matlab参数导入opencv + matlab标定和矫正
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 辛苦原创所得,转载请注明出处 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ...
- Matlab/Eigen矩阵填充问题
Matlab进行矩阵填充时可以填充空矩阵,相当于空矩阵不存在,例如一下代码: P_RES = [ P_xv P_xvy P_xv*dy_dxv'; P_yxv P_y P_yxv*dy_dxv'; d ...
随机推荐
- webBrowser获取取Cookie不全的解决方法
//取当前webBrowser登录后的Cookie值 [DllImport("wininet.dll", CharSet = CharSet.Auto, Set ...
- MacOS Sierra10.12.4编译Android7.1.1源代码必须跳的坑
简单介绍 下载Android7.1.1源代码花费了两天,编译整个源代码相同花费了2天,期间遇到无数个坑. 如今编译源代码,一旦中间遇到错误,则要又一次開始. 本文记录编译过程遇到的问题及解决方式,如有 ...
- FFmpeg(8)-打开音视频解码器,配置解码器上下文(avcodec_find_decoder()、avcodec_alloc_context3())
一.avcodec_find_decoder 获取解码器.在使用之前必须保证所用到的解码器已经注册,最简单的就是调用avcodec_register_all() 函数,就像之前注册解封装器的时候,也要 ...
- FFmpeg(1)-创建支持FFmpeg的AS项目
一.新建Android Studio项目 注意点: 1.在“Create Android Project”栏目时,须勾选“Include C++ support” 复选框: 2.在“Customize ...
- Chunk
Chunk是一系列语句,Lua执行的每一块语句,比如一个文件或者交互模式下的每一行都是一个Chunk. 每个语句结尾的分号(;)是可选的,但如果同一行有多个语句最好用:分开 a = 1 b = a ...
- how many shards and replicas should be set for Elastic Search
https://cpratt.co/how-many-shards-should-elasticsearch-indexes-have/ https://blog.trifork.com/2014/0 ...
- C#对Sql Server TEXT字段存储的实现(使用存储过程)
1.这里先准备数据库:在数据库中建立一个News数据表,字段为id,title,content2.建立用于插入news的存储过程:NewsInsert和NewsUpdate,代码如下: ),@cont ...
- C# IOCP服务器项目(学习)
无论什么平台,编写支持高并发性的网络服务器,瓶颈往往出在I/O上,目前最高效的是采用Asynchronous I/O模型,Linux平台提供了epoll,Windows平台提供了I/O Complet ...
- 【Java】Java复习笔记-第四部分
反射 反射: 在运行时动态分析或使用一个类进行工作. java.lang.Class类:描述类信息的类. 类对象:描述一个类信息的对象,当虚拟机加载类的时候,就会创建这个类的类对象并加载该对象,Cla ...
- Asp.Net支付宝手机网站支付接口API之C#版
一.准备工作 1.使用企业支付宝签约手机网站支付 2.下载支付宝官方demo 文档地址:https://doc.open.alipay.com/doc2/detail?treeId=60&ar ...