Current situation: I am trying to extract segments from an image. Thanks to the openCV method findContours findContours() , I now have a list of points with 8 links for each path. However, these lists cannot be used directly, because they contain many duplicates.
Problem: Given a list of 8-connected points that may contain duplicates, extract segments from it.
Possible solutions:
- At first I used the openCV method
approxPolyDP() . However, the results are pretty bad ... Here are the large-scale contours:

Here is the result of approxPolyDP() : (9 segments! Some overlap)

but I want more:

This is bad because approxPolyDP() can convert something that "looks like multiple segments" to "multiple segments". However, I have a list of points that tend to repeat several times above themselves.
For example, if my points are:
0 1 2 3 4 5 6 7 8 9
Then the list of points will be 0 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1 9 ... And if the number of points becomes large (> 100), then the segments extracted by approxPolyDP() , unfortunately, are not duplicated (then is: they overlap, but are not strictly equal, so I can’t just say “remove duplicates”, unlike pixels, for example)
- Maybe I have a solution, but it is quite long (albeit interesting). First of all, for all 8-linked lists, I create a sparse matrix (for efficiency) and set the matrix values ​​to 1 if the pixel belongs to the list. Then I create a graph , with nodes corresponding to pixels, and edges between adjacent pixels. It also means that I add all the missing edges between the pixels (the complexity is small, possible due to the sparse matrix). Then I delete all possible “squares” (4 neighboring nodes), and this is possible because I am already working on rather thin contours. Then I can run the minimal spanning tree algorithm. And finally, I can approximate each tree branch with openCV
approxPolyDP()
segmentation http://img197.imageshack.us/img197/4488/segmentation.png
Here are some fantastic snapshots (thanks to Paint!) Of the source list and a related graph. Then when I add edges between neighbors. And finally, when I remove the edges and create a minimal spanning tree (not useful here)
To summarize: I have a tedious method that I haven't implemented yet, as it seems to be error prone. However, I ask you folks from StackOverflow: are there other existing methods, perhaps with good implementations?
Edit: To clarify, as soon as I have a tree, I can extract the “branches” (the branches begin at the leaves or nodes associated with 3 or more other nodes). Then the algorithm in openCV approxPolyDP() is the Ramer-Douglas-Puker algorithm , and here is a Wikipedia photo of what it does:

With this picture, it's easy to see why it fails when the dots can be duplicates.
Other editing: There is something interesting to note in my method. When you consider points located in a grid (for example, pixels), then, as a rule, the minimal spanning tree algorithm is not useful, since there are many possible minimal trees
XXXX | XXXX
fundamentally different from
XXXX | | | | XXXX
but both are minimal spanning trees
However, in my case, my nodes rarely form clusters, because they must be contours, and the thinning algorithm already exists, which is executed in advance in findContours() .
Reply to Tomalak's comment:

If the DP algorithm returns 4 segments (the segment from point 2 to the center, which will be there twice), I would be happy! Of course, with good parameters, I can get to a state where, by chance, I have the same segments, and I can remove duplicates. However, obviously, the algorithm is not intended for him.
Here is a real example with too many segments:
