Ask Your Question
0

Calculation of eigenvalues and eigenvectors for contour

asked 2019-02-05 10:07:49 -0600

vps gravatar image

updated 2019-02-05 14:06:34 -0600

LBerger gravatar image

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 flag offensive close merge delete

Comments

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.

berak gravatar imageberak ( 2019-02-06 02:12:23 -0600 )edit

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

vps gravatar imagevps ( 2019-02-06 09:44:23 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2019-02-05 14:25:21 -0600

LBerger gravatar image

updated 2019-02-06 10:06:32 -0600

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...

edit flag offensive delete link more

Comments

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

vps gravatar imagevps ( 2019-02-06 09:43:26 -0600 )edit

Question Tools

1 follower

Stats

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

Seen: 577 times

Last updated: Feb 06 '19