I am stuck in weird situation. My fitEllipse
method returns a big Ellipse than my original Contour. Here is the output of Image:
and my binary image from which it is generating contour is:
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,