I’m having trouble in calculating Eigen Vectors, Values in OpenCV. I have done the same in Python (SciPy) but I’m having trouble in porting my code.
I have a 2 Matrices Sw, Sb whose values are:
Sw:[0.0112962962962963, 0.00675925925925926;
0.00675925925925926, 0.007962962962962963]
Sb:[0.0530787037037037, 0.01657407407407407;
0.01657407407407407, 0.004606481481481482]
For above values of Sw, Sb, computing Eigen Values, Eigen Vectors in SciPy (Python) using the following:
from numpy import *
from scipy import linalg as la
evals,evecs = la.eig(Sw,Sb)
yields the following:
evals:
[ 0.17299805+0.j -8.47412141+0.j]
evecs:
[[ 1. -0.31926401]
[-0.54311321 1. ]]
I’m trying to port the above code to OpenCV (C++ API)
For the same values of Sw, Sb, computing Eigen Values and Eigen Vectors in OpenCV using
cv::eigen(Sb,Sb_Eig_Val,Sb_Eig_Vec);
yields different values which is:
Sb_Eig_Val
[0.05820394496612978; -0.0005187597809445917]
Sb_Eig_Vec
[0.9553644860284983, 0.2954296850952915;
-0.2954296850952915, 0.9553644860284983]
Am I missing something here?
You’re solving two different linear algebra problems! Consider:
Which gives:
exactly as openCV. In the
scipycase with two input arguments you are solving the generalized eigenvalue problem, but with only one argument it assumes the other matrix is the identity (this is typically what we mean when we say diagonalization)Since the matrices are symmetric, you should be using
eighin scipy. Real symmetric matrices give real eigenpairs, and it will stop returning a complex number.