Ahsan Khan
Ahsan Khan

Reputation: 830

Ambiguity in Ellipse Detection in opencv c++

Hey i am doing the road sign detection project from last two weeks and i am getting the major problem for detecting the ellipse from the image.Can anyone tell me how can i handle this problem!!!You can see the result and code.Thanks

Road sign Detection

    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;

    findContours(GreyImage, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));

    for (size_t i = 0; i < contours.size(); i++)
    {
        Mat pointsf;
        Mat(contours[i]).convertTo(pointsf, CV_32F);
        RotatedRect box = fitEllipse(pointsf);
        ellipse(OrignalImg, box, Scalar(0, 0, 255), 1, 8);
     }

Upvotes: 2

Views: 7944

Answers (1)

Rollen
Rollen

Reputation: 1210

From OpenCV Documentation: http://docs.opencv.org/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html?highlight=fitellipse#fitellipse

The function calculates the ellipse that fits (in a least-squares sense) a set of 2D points best of all.

As long as you pass a contour it will fit an ellipse to it. What does this mean? Well consider a list of points. If I passed the points that represented a rectangle (the corners), then the likely output would be the ellipse drawn.

Fitted ellipse to rectangle Least squares fit

The above rectangle would actually find an ellipse to have 'no error' since it fits using the corners of the rectangle. In other circumstances, it will attempt to find an ellipse that fits reducing error (such as in the image with the polygon). That's okay, as long as you pass it contours that were close to ellipses in the image.

Contours (returned by findContours) are defined as lines that separate regions. These regions can be squares, triangles or pretty much any shape. You pass these contours to your fitEllipse function; that is, you made an ellipses that pass through these contours, whether or not these contours actually represented ellipses.

As @Micka said, you need to check whether a given contour is in fact an ellipse before attempting to make (fit) an ellipse. Examples can be found in StackOverflow using a variety of methods such as HoughCircles, MatchShape, HuTransforms and RANSAC:

Upvotes: 4

Related Questions