Isolate clusters from a series of points

4 vues (au cours des 30 derniers jours)
Fed
Fed le 6 Mar 2021
Modifié(e) : Fed le 8 Mar 2021
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

Réponse acceptée

Image Analyst
Image Analyst le 6 Mar 2021
Modifié(e) : Image Analyst le 6 Mar 2021
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 commentaire
Fed
Fed le 6 Mar 2021
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!

Connectez-vous pour commenter.

Plus de réponses (1)

Image Analyst
Image Analyst le 6 Mar 2021
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 commentaires
Image Analyst
Image Analyst le 7 Mar 2021
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 le 7 Mar 2021
Thanks again for your answer, now it is clear.
You always provide very accurate contributions!

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by