Isolate clusters from a series of points

2 次查看(过去 30 天)
I have a set of points whose x and y coordinates are stored into two vectors.
imshow(Imm)
hold on
plot(Lpix_x,Lpix_y,'go')
I want to plot only the clusters of points, deleting the other points.
I thought about computing the euclidean distance between each point and its subsequent and then saying that 'if distance is > than a certain threshold, then the corresponding elements in Lpix_x and L_pix_y must be deleted'.
for i = 1:n-1
Xdist(i) = Lpix_x(i+1) - Lpix_x(i);
Ydist(i) = Lpix_y(i+1) - Lpix_y(i);
dist(i) = sqrt((Xdist(i))^2 + (Ydist(i))^2);
if dist(i)>2 % 2 is an example of threshold
%???????
end
end
I don't know what to put instead of ???? . What should I say there?
If there is another way to solve the problem, any suggestion will be well accepted.
Thank you

采纳的回答

Image Analyst
Image Analyst 2021-3-6
编辑:Image Analyst 2021-3-6
You can use kmeans() to determine the location of 5 cluster centroids. Then compute the distances of each point in the class from that class's centroid. Threshold it to determine which are closer than the threshold distance, something like (untested)
xy = [x(:), y(:)];
[classIndexes, centroids] = kmeans(xy, 5);
goodPoints = false(size(xy, 1), 1);
% Find points that are within 75 of any centroid.
threshold = 75;
for k = 1 : size(centroids, 1)
xc = centroids(k, 1);
yc = centroids(k, 2);
plot(xc, yc, 'r+', 'LineWidth', 2, 'MarkerSize', 30);
distances = sqrt((x - xc).^2 + (y - yc).^2);
goodPoints = goodPoints | (distances < threshold);
end
xy = xy(goodPoints, :);
x = xy(:, 1);
y = xy(:, 2);
subplot(2, 1, 2);
plot(xy(:, 1), xy(:, 2), 'b.', 'MarkerSize', 20);
grid on;
Attach your data if you need more help.
  1 个评论
Fed
Fed 2021-3-6
Dear Image Analyst, thank you very much for your answer.
I tried to implement your code but I wasn't able to delete the points.
I attach all the data of my project.
Thanks again!

请先登录,再进行评论。

更多回答(1 个)

Image Analyst
Image Analyst 2021-3-6
Frederica, Here is a full demo:
clc; % Clear command window.
clear; % Delete all variables.
close all; % Close all figure windows except those created by imtool.
workspace; % Make sure the workspace panel is showing.
fontSize = 18;
markerSize = 20;
LineWidth = 2;
load('workspace.mat')
subplot(2, 1, 1);
plot(X_Lpix_x, X_Lpix_y, 'b.', 'MarkerSize', 20);
grid on;
hold on;
% plot(X_Rpix_x, X_Rpix_y, 'r.', 'MarkerSize', 20);
% Cluster analysis
xy = [X_Lpix_x(:), X_Lpix_y(:)];
x = xy(:, 1);
y = xy(:, 2);
[classIndexes, centroids] = kmeans(xy, 5);
goodPoints = false(size(xy, 1), 1);
% Find points that are within 75 of any centroid.
threshold = 75;
for k = 1 : size(centroids, 1)
xc = centroids(k, 1);
yc = centroids(k, 2);
plot(xc, yc, 'r+', 'LineWidth', 2, 'MarkerSize', 30);
distances = sqrt((x - xc).^2 + (y - yc).^2);
goodPoints = goodPoints | (distances < threshold);
end
xy = xy(goodPoints, :);
x = xy(:, 1);
y = xy(:, 2);
subplot(2, 1, 2);
plot(xy(:, 1), xy(:, 2), 'b.', 'MarkerSize', 20);
grid on;
  3 个评论
Image Analyst
Image Analyst 2021-3-7
75 is the distance from the center that divides points you want to keep from points you don't want to keep.
goodPoints is a list of whether the original point is to be kept or not. So it's true if the distance is less than 75 and false if it's farther away than that. You could use a double array if you want with 0 and 1s, but using 8 bytes to store a true or false value is overkill, though it will work. Do you think it should not be logical? If so, what do you think it should be?
To get 5 sets of vectors you can do
classIndexes = classIndexes(goodPoints); % Extract only those we want to keep.
class = 1;
x1 = x(classIndexes == class);
y1 = y(classIndexes == class);
class = 2;
x2 = x(classIndexes == class);
y2 = y(classIndexes == class);
class = 3;
x3 = x(classIndexes == class);
y3 = y(classIndexes == class);
class = 4;
x4 = x(classIndexes == class);
y4 = y(classIndexes == class);
class = 5;
x5 = x(classIndexes == class);
y5 = y(classIndexes == class);
Fed
Fed 2021-3-7
Thanks again for your answer, now it is clear.
You always provide very accurate contributions!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Point Cloud Processing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by