Reputation: 153
I am trying to calculate the eigenvector of a 4x4 matrix in opencv.
For this I first calculate the eigenvalue according to this formula:
Det( A - lambda * identity matrix ) = 0
From wiki on eigenvalues and eigenvectors.
After solving this, it gives me 4 eigenvalues that look something like this:
0.37789 + 1.91687i
0.37789 - 1.91687i
0.412312 + 1.87453i
0.412312 - 1.87453i
From these 4 eigenvalues I take the highest value and I want use that with this formula:
( A - lambda * identity matrix ) v = 0
I tried to use my original matrix A with the opencv function "eigen()", but this doesn't give me the results I am looking for.
I also tried to use RREF (reduced row echelon form), however I don't know how to do this with complex eigenvalues.
So my question is, how would you calculate this eigenvector?
I plugged my data in to wolframalpha to see what my results should be.
Upvotes: 1
Views: 1917
Reputation: 153
So I solved the problem using the 'ComplexEigenSolver' from the Eigen library.
//create a multichannel matrix
Mat a_com = Mat::zeros(4,4,CV_32FC2);
for(int i = 0; i<4; i++)
{
for(int j = 0; j<4; j++)
{
a_com.at<Vec2f>(i,j)[0] = a.at<double>(i,j);
a_com.at<Vec2f>(i,j)[1] = 0;
}
}
MatrixXcf eigenA;
cv2eigen(a_com,eigenA); //convert OpenCV to Eigen
ComplexEigenSolver<MatrixXcf> ces;
ces.compute(eigenA);
cout << "The eigenvalues of A are:\n" << ces.eigenvalues() << endl;
cout << "The matrix of eigenvectors, V, is:\n" << ces.eigenvectors() << endl;
This gives me the following output (which is more or less what I was looking for):
The eigenvalues of A are:
(0.3951,-1.89571)
(0.3951,1.89571)
(0.3951,1.89571)
(0.3951,-1.89571)
The matrix of eigenvectors, V, is:
(-0.704546,0) (-5.65862e-009,-0.704546) (-0.064798,-0.0225427) (0.0167534,0.0455606)
(-2.22328e-008,0.707107) (0.707107,-1.65536e-008) (0.0206999,-0.00474562) (-0.0145628,-0.0148895)
(-6.07644e-011,0.0019326) (0.00193259,-4.52426e-011) (-0.706729,6.83797e-005) (-0.000121153,0.706757)
(-1.88954e-009,0.0600963) (0.0600963,-1.40687e-009) (0.00200449,0.703827) (-0.70548,-0.00151068)
Upvotes: 1
Reputation: 2575
Opencv already has function for calculating eigenvalues and eigenvectors, cv::eigen()
. I advise using it instead of writing the algorithm yourself.
Here is good blog that explains how to do this in c, c++ and python.
Upvotes: 1