0

I have a list of coordinates in tuples. Every two tuples correspond to the line, for example ((x1,y1),(x2,y2)) - is a line. The list is unordered and contains connected (chained) and non-connected segments. Under connected (chained) I mean the following list: [((x1,y1), (x2,y2)), ((x3,y3),(x4,y4)), ((x5,y5),(x2,y2)), ((x3,y3),(x5,y5))] so that if to connect them together the line appears. The problem is that in the list I have different lines that I want to segment. For now if to draw all the segments the figure looks like the following: enter image description here

I want to smooth it so that the result is a single line for each branch. How to do it better?

for now, I tried to find connected components by the following function:

def get_connected_components(data):
result = [[]]
for connect in data:
    # empty, add all
    if not result[-1]:
        result[-1].extend(connect)
        continue
    # does not continue current streak
    if result[-1][-1] != connect[0]:
        result.append([c for c in connect])
    # does continue current streak
    else:
        # to collect all intermediate steps as well
        result[-1].append(connect[1])
        # if you do not need to build the whole thing, use this instead:
        # result[-1][-1] = connect[1]
# simplify result
r = [(r[0], r[-1]) for r in result]
return r

but the problem is that tuples can be flipped. Moreover, the connectivity can be not only by 2 lines in 1 point but in 3 or even more. But the filtering is available only for a single line, not for a connection of them.

  • Please, check [ask]. What have you tried so far? Show [mre], incl. sample input and expected output. Ask a question about specific problem with your code. – buran Jan 14 '22 at 09:56
  • Also you ask _How to do it better?_ but do not show your current code/attempt. If you have a code that works it may be better to ask on [Code Review](http://codereview.stackexchange.com). – buran Jan 14 '22 at 09:57
  • In an image you could use erosion after dilatation ( https://en.wikipedia.org/wiki/Erosion_(morphology) ) – Patrick Artner Jan 14 '22 at 10:01
  • work with image is not an option as far as I extracted lines from the image – Марина Лисниченко Jan 14 '22 at 10:02
  • I suggest you give us a minimal input and an expected output. You may want to include all possible challenges you have described above. Otherwise, it is very difficult to help you. – Neb Jan 14 '22 at 10:05
  • 1
    Maybe research in direction "merging / clustering" of "line segments / paths" - f.e. https://stackoverflow.com/questions/57867914/algorithm-for-merging-spatially-close-paths-line-segments – Patrick Artner Jan 14 '22 at 10:12

0 Answers0