How to cluster lines ?

How to cluster lines ?
Here below an example:
x = [1 1 2 3 4 5 6 7 1 1 2 3 3 4 4 5 15 15 15 16 16 16 17 18 20 20 21
2 5 9 5 4 9 6 7 3 7 3 5 7 6 7 14 14 19 23 22 15 16 17 20 22 21 23
];
y = [2 10 3 20 5 2 4 6 100 106 102 93 90 100 81 77 66 59 74 60 62 35 48 69 30 58 55
6 8 3 11 8 2 10 21 66 108 75 92 90 100 82 72 67 55 75 60 68 55 53 70 32 37 32
];
plot(x,y,'LineWidth',2,'color','k') ;

 采纳的回答

Image Analyst
Image Analyst 2022-7-5

0 个投票

I'd pass in both endpoints of each line to kmeans and let it figure it out. Demo attached.
If you have a situation where one endpoint is in one cluster and the other is in a different cluster you might decide to assign both end points to whatever cluster is nearer one of the endpoints.

4 个评论

thanks @Image Analyst, I have tried to use k-means clustering as you have suggested
x = [1 1 2 3 4 5 6 7 1 1 2 3 3 4 4 5 15 15 15 16 16 16 17 18 20 20 21
2 5 9 5 4 9 6 7 3 7 3 5 7 6 7 14 14 19 23 22 15 16 17 20 22 21 23
];
y = [2 10 3 20 5 2 4 6 100 106 102 93 90 100 81 77 66 59 74 60 62 35 48 69 30 58 55
6 8 3 11 8 2 10 21 66 108 75 92 90 100 82 72 67 55 75 60 68 55 53 70 32 37 32
];
X = [x(1,:) x(2,:)];
Y = [y(1,:) y(2,:)];
numGroups = 3;
groupLabels = kmeans([X',Y'],numGroups);
hold on
plot(x,y,'LineWidth',2,'color','k') ;
gscatter(X, Y, groupLabels,[],[],30);
......but it looks like that k-means clustering does not work perfectly.....
Since here 4 edges would have endpoints with two colors, I should check manually & visually those edges and re-assign the correct color/cluster...... I do not know.... hmmmmm....
I have also tried another algorithm for clustering, called spectral cluster, but it looks like even worse...
x = [1 1 2 3 4 5 6 7 1 1 2 3 3 4 4 5 15 15 15 16 16 16 17 18 20 20 21
2 5 9 5 4 9 6 7 3 7 3 5 7 6 7 14 14 19 23 22 15 16 17 20 22 21 23
];
y = [2 10 3 20 5 2 4 6 100 106 102 93 90 100 81 77 66 59 74 60 62 35 48 69 30 58 55
6 8 3 11 8 2 10 21 66 108 75 92 90 100 82 72 67 55 75 60 68 55 53 70 32 37 32
];
X = [x(1,:) x(2,:)];
Y = [y(1,:) y(2,:)];
numGroups = 3;
groupLabels = spectralcluster([X',Y'],numGroups);
hold on
plot(x,y,'LineWidth',2,'color','k') ;
gscatter(X, Y, groupLabels,[],[],30);
If I just plot x and y
x = [1 1 2 3 4 5 6 7 1 1 2 3 3 4 4 5 15 15 15 16 16 16 17 18 20 20 21
2 5 9 5 4 9 6 7 3 7 3 5 7 6 7 14 14 19 23 22 15 16 17 20 22 21 23
];
y = [2 10 3 20 5 2 4 6 100 106 102 93 90 100 81 77 66 59 74 60 62 35 48 69 30 58 55
6 8 3 11 8 2 10 21 66 108 75 92 90 100 82 72 67 55 75 60 68 55 53 70 32 37 32
];
% Plot endpoints
plot(x, y, '.', 'MarkerSize', 30)
How do I know which (x,y) pair corresponds to the two end points of a single line?
hope this drawing clarifies a bit...

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by