FindContours Duplicate Points
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:
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.
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.
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.
Why Canny?
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/...
Yes, but with Canny you get virtual duplicates because it makes a contour for both sides of the edge.
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.
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.
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.
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.
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.
K cool. :)