Question

I have this code in my program:

findContours(canny_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
for(int i = 0; i < contours.size(); i++){
    if(hierarchy[i][3] >=0 && arcLength(contours[i], true) > 240){
        Moments mu =  moments(contours[i], false);
        Point2d mc(mu.m10/mu.m00, mu.m01/mu.m00);
        double m11 = mu.m11 / mu.m00;
        double m20 = mu.m20 / mu.m00;
        double m02 = mu.m02 / mu.m00;
        double orientation = 0.5*atan(2*m11 / (m20 - m02));
        double lambda1 = (m20 + m02)/2 + sqrt(4*m11*m11 - (m20 - m02)*(m20 - m02));
        double lambda2 = (m20 + m02)/2 - sqrt(4*m11*m11 - (m20 - m02)*(m20 - m02));
        cout << "labmda1: " << lambda1 << endl << "labmda2: " << lambda2 << endl;
        cout << "orientation: " << orientation << endl << "mc: " << mc << endl << endl;
        //ellipse(drawing_ellipses, mc, Size(lambda2, lambda1), orientation, 0, 360, Scalar(0, 0, 255), 1);
    }
}

If I understand correctly, lambda1 and lambda2 should be semi-major and semi-minor axes of ellipse, mc - his center and orientation - angle of rotation. Instead only mc calculates correctly, other parametrs are wrong (lambda much larger then should be), even when contour is ellipse itself. How I can fix it?

Was it helpful?

Solution

Can I ask what this is for? I am doing similar research in the attempt to make a fiducial marker.

The problem from my research is that:

lambda1 = (m20 + m02)/2 + sqrt(4*m11*m11 - (m20 - m02)*(m20 - m02));

should actually be:

lambda1 = srqt((m20 + m02)/2 + sqrt(4*m11*m11 - (m20 - m02)*(m20 - m02)));

Hope this helps :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top