# Calculation of eigenvalues and eigenvectors for contour

Hi, I am trying to calculate eigenvectors but I am getting huge eigenvalues. As result, I am getting vector component 0. It will be great if you suggest the way.

int main()
{
Mat img = Mat::zeros(500, 500, CV_8U);
ellipse(img, RotatedRect(Point(250, 250), Size(300, 200), 30), 255, FILLED);

Moments m = moments(img, true);
Point p(m.m10 / m.m00, m.m01 / m.m00);

double u20 = ((m.m20 / m.m00) - (p.x * p.x));
double u02 = ((m.m02 / m.m00) - (p.y * p.y));
double u11 = ((m.m11 / m.m00) - (p.x * p.y));

double angle = 0.5 * atan((2 * u11) / ( u20 - u02));
double eigenValue1 = 0.5 * (u20 + u02) + 0.5 * sqrt((4 * u11 * u11) + (u20 - u02) * (u20 - u02));
double eigenValue2 = 0.5 * (u20 + u02) - 0.5 * sqrt((4 * u11 * u11) + (u20 - u02) * (u20 - u02));

double a[2][2] = {{ u20 - eigenValue1, u11 }, { u11, u02 - eigenValue1 } };

double b[2][1] = { { 0 }, { 0 } };

Mat A = Mat(2, 2, CV_32FC1, a);
Mat B = Mat(2, 1, CV_32FC1, b);

Mat v = A.inv() * B;
cout << "v= " << endl << " " << v << endl;
waitKey(0);
return 0;
}

edit retag close merge delete

maybe you should take a look here, -- it probably gets better, if you calculate eigenvectors based on the actual contour points, not on the moments.

( 2019-02-06 02:12:23 -0500 )edit

Thank you. I am able to calculate eigenvecors by PCA.

( 2019-02-06 09:44:23 -0500 )edit

Sort by ยป oldest newest most voted

use mu (m.mu00) instead of u20 and it will improve your results

example angle

    if (m.mu20 - m.mu02 != 0)
{
theta=1/2.0*atan(2*m.mu11/(m.mu20-m.mu02));
int s = (1 - sign(m.mu20 - m.mu02))*signm.mu11);
theta  += s*acos(-1.0)/4;
}
else if (m.mu11>0)
theta =acos(-1.0)/4;
else
theta =-acos(-1.0)/4;
if (theta<0)
theta += 2*acos(-1.0);


http://farside.ph.utexas.edu/teaching...

more

Thanks for the suggestion @LBerger, As suggested by @berak, I have used PCA method to calculate the eigenvectors.

( 2019-02-06 09:43:26 -0500 )edit

Official site

GitHub

Wiki

Documentation

## Stats

Asked: 2019-02-05 10:07:49 -0500

Seen: 46 times

Last updated: Feb 06