Sorting Data to follow down a line

2 views (last 30 days)
Andrew Luce
Andrew Luce on 3 Mar 2020
Commented: Andrew Luce on 10 Mar 2020
Hello,
I'm trying to compare an image of a path to the points in the generated via matlab. I have an image of the path, turinging it into a binary image, skeltonizing the image and taking the row and column data for the skelton. I want to have points of the line in order to follow down the line. I found this code to do so but for some reason its adding some some extra line to the end. I do not know why.
The code I used for sorting is as follows
Coordinates=[col row]; % put the coordinates into a single matrix
dist = pdist2(Coordinates,Coordinates); %calculate the distance between each point in the matrix
N = size(Coordinates,1); % Number of points in the trace line
result = NaN(1,N); %set up the matrix for the loop to order the points to follow the path
result(1) = 1; % first point is first row in data matrix
for ii=2:N % found this on the internet, creates an order for the points to follow the path of the line
dist(:,result(ii-1)) = Inf;
[~, closest_idx] = min(dist(result(ii-1),:));
result(ii) = closest_idx;
end
Coordinates=Coordinates(result,:); %apply that order to the coordinate list
Thank you

Answers (1)

Prabhan Purwar
Prabhan Purwar on 6 Mar 2020
Hi,
I am getting the following output using the above mentioned code:
load('data (1).mat');
scatter(col,row);
figure
plot(col,row);
Coordinates=[col row]; % put the coordinates into a single matrix
dist = pdist2(Coordinates,Coordinates); %calculate the distance between each point in the matrix
N = size(Coordinates,1); % Number of points in the trace line
result = NaN(1,N); %set up the matrix for the loop to order the points to follow the path
result(1) = 1; % first point is first row in data matrix
for ii=2:N % found this on the internet, creates an order for the points to follow the path of the line
dist(:,result(ii-1)) = Inf;
[~, closest_idx] = min(dist(result(ii-1),:));
result(ii) = closest_idx;
end
Coordinates=Coordinates(result,:); %apply that order to the coordinate list
figure
Hi scatter(Coordinates(:,1),Coordinates(:,2));
figure
plot(Coordinates(:,1),Coordinates(:,2));
  • Could you please elaborate what you exactly mean by "adding some some extra line to the end".
Extra line at the initial point is because algorithm expects the initial point to be accurate.
Workaround: Add a point (21,258) in the data set.
Output:
  1 Comment
Andrew Luce
Andrew Luce on 10 Mar 2020
On the top left of the graph, if you zoom in you'll see that the line doesn't have a clean end it takes a sharp turn downards. It added some extra points it seems.

Sign in to comment.

Categories

Find more on Read, Write, and Modify Image in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!