Problems getting point set for convex hull

asked 2014-09-07 16:44:18 -0600

cmc gravatar image

I have a function that takes a double (proximity) that determines how far a contour must be from another to be considered close enough to add together. When the contours are added a convex hull is then drawn. When the function takes 2 as its parameter it produces this image: https://i.imgur.com/G5sX6Jc.png.

As you can see from the image something very funky is happening that I cannot figure out. If someone could tell me where I have gone wrong in my code I would be very grateful.

for (size_t i = 0; i < contours.size(); i++)
{
    minDistance = 9999999999999999999;
    for (size_t j = 0; j < contours[i].size(); j++)
    {
        for (size_t a = i + 1; a < contours.size();)
        {
            for (size_t b = 0; b < contours[a].size(); b++)
            {
                euclidean = norm(contours[i][j] - contours[a][b]);
                if (euclidean < minDistance)
                {
                    minDistance = euclidean;
                }
            }
            if (minDistance <= proximity)
            {
                for (size_t q = 0; q < contours[a].size(); q++)
                {
                    contours[i].push_back(contours[a][q]);
                }
                contours.erase(remove(begin(contours), end(contours), contours[a]), contours.end());
            }
            else
            {
                a++;
            }
        }
    }
}
edit retag flag offensive close merge delete

Comments

1

don't erase from the contours vector, while you're iterating over it.

berak gravatar imageberak ( 2014-09-08 00:57:53 -0600 )edit

If I don't remove the contour then there is a risk that a contour may be included in multiple convex hulls.

cmc gravatar imagecmc ( 2014-09-08 04:10:09 -0600 )edit