Ask Your Question
0

FindContours Duplicate Points

asked 2018-08-25 13:44:59 -0500

jpistorino gravatar image

I am using OpenCV 3.4.1 with VS2015 C++ on a Win10 platform.

My question relates to findContours and whether that should be returning duplicate points within a contour.

For example, I have a test image like this:

image description

I do Canny on it and then I run findContours like this:

findContours(this->MaskFrame,this->Contours,this->Hierarchy,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);

When I check the resulting contours like this:

for (int x = 0; x < Images->Contours.size(); x++)
    for (int y = 0; y < Images->Contours[x].size(); y++)
        for (int z = y + 1; z < Images->Contours[x].size(); z++)
            if (Images->Contours[x][y] == Images->Contours[x][z])
                printf("Contours duplicate point: x: %d, y: %d z: %d\n", x, y, z);

I can see there there are many/hundreds of duplicate points within a given contour.

image description

The presence of the duplicates seems to cause a problem with the drawContours function.
Nevertheless, this image shows that 6 contours were detected with ~19,000 points comprising all the contours, the largest contour has ~18,000 points, but there are 478 points that are duplicated within a contour.

However, this only seems to occur if the total number of points in a given contour is fairly large, e.g., > 2000 points. If I arrange the image so that no contour has more than ~2000 points, as below, then there are no duplicates.

image description

In this image, there are 11 contours, there are ~10,000 points comprising all the contours, with the largest contour having ~1,600 points, and no duplicates.

Before I try and get deep into findContours or something else, I thought I would ask: anyone have any ideas why I am seeing duplicate points within a contour?

Thanks for any help.

edit retag flag offensive close merge delete

Comments

Why Canny?

sjhalayka gravatar imagesjhalayka ( 2018-08-25 15:10:33 -0500 )edit

My understanding is that findContours only works on an image from which the edges have been extracted. You can do that either with a threshhold function, e.g., or with Canny. I use Canny. https://docs.opencv.org/2.4/modules/i...https://docs.opencv.org/3.3.1/d4/d73/...

jpistorino gravatar imagejpistorino ( 2018-08-25 15:52:49 -0500 )edit

Yes, but with Canny you get virtual duplicates because it makes a contour for both sides of the edge.

sjhalayka gravatar imagesjhalayka ( 2018-08-26 18:32:42 -0500 )edit

I really do not see that behavior. Using Canny, you get contours that do not have duplicates. It appears to be the case (as indicated by matman) that the duplicates only occur when the contours are not closed.

jpistorino gravatar imagejpistorino ( 2018-08-27 11:25:11 -0500 )edit

I'm sorry that you don't believe me, so here's a test code and sample image: https://github.com/sjhalayka/opencv_c...

It generates two contours, and they are virtually the same.

sjhalayka gravatar imagesjhalayka ( 2018-08-27 16:26:06 -0500 )edit

Not a question of not believing you. I just haven't seen that in my code. In my image above, there are 11 contours for the 11 shapes shown, not 22. Let me run yours and see what happens. Thanks.

jpistorino gravatar imagejpistorino ( 2018-08-27 16:31:48 -0500 )edit

I ran my code on your test image and it produces 15 contours when using Canny, but only 6 contours when using threshold. So, approximately one half of the contours are duplicates.

sjhalayka gravatar imagesjhalayka ( 2018-08-27 17:17:29 -0500 )edit

I am going to continue investigating this and thank you for your efforts. Something is not adding up because in the second image shown, I counted 6 contours using Canny, not the 15 you are seeing.

jpistorino gravatar imagejpistorino ( 2018-08-27 17:24:44 -0500 )edit

K cool. :)

sjhalayka gravatar imagesjhalayka ( 2018-08-27 17:37:55 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2018-08-26 08:24:44 -0500

matman gravatar image

In case the contour is not closed (area is 0 or length of contour) you get duplicate contour points and this is the correct behavior. See here

edit flag offensive delete link more

Comments

This appears to be correct. Help me to understand why there would be duplicates. Say there was simply a line. Why would that line have the same point on it multiple times?

jpistorino gravatar imagejpistorino ( 2018-08-27 11:23:39 -0500 )edit

Imagine a straight line with one extra pixel on one side. How else should the algorithm detect this single pixel which definitely belongs to the contour. This is a problem of stopping criterion. In case of a closed contour there is an outer side as well as an inner side. in case of a "perfect" circle for example both sides are equal, but if there is only a single extra pixel on one side they differ. Take a look at this for a better understanding.

matman gravatar imagematman ( 2018-08-28 15:07:32 -0500 )edit
Login/Signup to Answer

Question Tools

2 followers

Stats

Asked: 2018-08-25 13:44:59 -0500

Seen: 499 times

Last updated: Aug 26 '18