Ask Your Question
3

Bug in findContours or convexHull or convexityDefects

asked 2013-03-26 12:24:01 -0600

kfrance gravatar image

I think I found a bug but I don't know what assumptions are suppose to be true for the functions findContours, convexHull, and convexityDefects. I have a contour around an image that may have a little noise because of the segmentation previous to finding contours. Some points in the contour exists multiple times (not right next to each other but with one Point in between them). I assume that is valid and when I draw the contour it looks great.

Where I think the real problem is is in convexHull. I'm calling it using

    vector<int> hull;  
    convexHull(contour, hull);

So I expect to get back a list of indices into the contour. In cvConvexityDefects it checks the orientation of the hull points by looking at the first 3 indices of the hull.

    else
    {
        index1 = *CV_SEQ_ELEM( hull, int, 0 );
        index2 = *CV_SEQ_ELEM( hull, int, 1 );
        index3 = *CV_SEQ_ELEM( hull, int, 2 );
    }

    sign += (index2 > index1) ? 1 : 0;
    sign += (index3 > index2) ? 1 : 0;
    sign += (index1 > index3) ? 1 : 0;

    rev_orientation = (sign == 2) ? 0 : 1;

So it assumes that all the indices in the hull points should be decreasing or increasing. Is that correct?

I get the following list of indices as hull points with the problem bolded. Indices are decreasing until it gets to that point. This causes convexityDefects to check all the points on the contour rather than the points between the two hull points and give a bad result.

4288 4280 4278 4219 4214 3938 3936 3932 3899 3898 3896 1568 1333 1332 1029 1010 1011 1002 971 925 891 750 749 716 700 600 421 177 175 0 5839 5793 5787 5587 5585 5491 5468 5453 5395 5374 5182 4509 4492 4461 4434 4419 4417 4292

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2014-02-14 10:27:18 -0600

rjewell gravatar image

I ran into this problem also. Did you find a fix? I will try to write a replacement algorithm.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2013-03-26 12:24:01 -0600

Seen: 840 times

Last updated: Feb 14 '14