Ask Your Question
1

FitEllipse return bigger ellipse than original ContourSize

asked 2016-11-30 05:48:53 -0600

Saghir Khatri gravatar image

updated 2020-10-04 12:34:55 -0600

I am stuck in weird situation. My fitEllipse method returns a big Ellipse than my original Contour. Here is the output of Image:

enter image description here

and my binary image from which it is generating contour is:

enter image description here

There is no noise or anything but I am still getting a large ellipse.

My code is as follows:

findContours( meter_bin, contours, hierarchy, RETR_LIST, CHAIN_APPROX_SIMPLE);
for (int z=0;z<contours.size();z++)
{
    /* Ignore areas with less area */
    if (contourArea(contours[z])>200)
    {
        Mat pointsf;
        Mat(contours[z]).convertTo(pointsf, CV_32F);
        RotatedRect box = fitEllipse(pointsf);
        if( MAX(box.size.width, box.size.height) > MIN(box.size.width, box.size.height)*30 )
            continue;
        drawContours(img4, contours, (int)z, Scalar::all(255), 1, 8);
        ellipse(img4, box, Scalar(0,0,255), 1, LINE_AA);
        ellipse(img4, box.center, box.size*0.5f, box.angle, 0, 360, Scalar(0,255,255), 1, LINE_AA);
        Point2f vtx[4];
        double length[4];
        box.points(vtx);
        for( int j = 0; j < 4; j++ )
        {
            length[j]=sqrt((vtx[j].x - vtx[(j+1)%4].x) *  (vtx[j].x - vtx[(j+1)%4].x) + (vtx[j].y - vtx[(j+1)%4].y) *  (vtx[j].y - vtx[(j+1)%4].y));
            line(img4, vtx[j], vtx[(j+1)%4], Scalar(0,255,0), 1, LINE_AA);
        }
    }
}

I am not sure why this is happening as everything seems to be fine here. Please let me know what is going wrong here Regards,

edit retag flag offensive close merge delete

Comments

consider

RotatedRect box = minAreaRect(pointsf);
sturkmen gravatar imagesturkmen ( 2020-10-04 12:59:51 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2016-11-30 10:29:11 -0600

matman gravatar image

I think the problem is CHAIN_APPROX_SIMPLE. You have a relative straight contour at the upper side, which can be approximated with very few points at the corners of the upper side. So your ellipse fit won't consider this part of contour. Try CHAIN_APPROX_NONE instead.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-11-30 05:48:53 -0600

Seen: 440 times

Last updated: Nov 30 '16