MATLAB Answers

Isolate clusters from a series of points

3 views (last 30 days)
Fed
Fed on 6 Mar 2021
Edited: Fed on 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

Accepted Answer

Image Analyst
Image Analyst on 6 Mar 2021
Edited: Image Analyst on 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 Comment
Fed
Fed on 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!

Sign in to comment.

More Answers (1)

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

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!