2

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);
     }
Ahsan Khan
  • 678
  • 9
  • 12
  • Why this fitellipse function is fitting lines also as a ellipse?? How can i get rid of this problem?? – Ahsan Khan Mar 03 '15 at 15:32
  • 2
    ellipse fitting isnt ellipse detection! – Micka Mar 04 '15 at 08:54
  • @Micka so how can we detect ellipse?? – Ahsan Khan Mar 04 '15 at 12:56
  • 1
    there is no openCV functionality to detect ellipses afaik. If I had to implement it, I would implement some RANSAC method. Or maybe try to find/create a HoughTransform for ellipses. – Micka Mar 04 '15 at 13:29
  • 1
    if you can isolate your ellipses and other objects, you could use fitEllipse on each contour and afterwards test/compare your found ellipses with your canny edges to decide whether your fitEllipse really was an ellipse or maybe something else ;) – Micka Mar 04 '15 at 13:31

1 Answers1

4

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:

Community
  • 1
  • 1
Rollen
  • 1,212
  • 11
  • 15
  • Sorry can you more elaborate your answer?? Thanks – Ahsan Khan Mar 04 '15 at 12:55
  • 1
    @ahsan-khan What would you like elaborated? I've expanded on the point regarding your comment however the solutions presented in the answers linked are sufficient to answer the rest of your question. – Rollen Mar 05 '15 at 01:31