Reputation: 48916
I have the following image:
Is there a way to connect the dots (draw a line between them) using MATLAB
? I tried plot
by passing it the image, but didn't work.
Thanks.
UPDATE
The way I'm expecting to connect the dots is roughly as shown through the red line in the image sample below:
Upvotes: 2
Views: 2881
Reputation: 7817
To plot a trail through a number of points, you could define an adjacency matrix and use gplot to display.
Here's a snippet to show the idea, although you'll note that with this rather simple code the output is a bit messy. It depends how clean a line you want and if crossovers are allowed - there are probably better ways of creating the adjacency matrix.
This assumes you've extracted the position of your points into a matrix "data" which is size n x 2 (in this case, I just took 200 points out of your image to test).
Basically, the nearest points are found with knnsearch (requires Statistics toolbox), and then the adjacency matrix is filled in by picking a starting point and determining the nearest neighbour not yet used. This results in every point being connected to at maximum two other points, providing that the number of points found with knnsearch is sufficient that you never back yourself into a corner where all the nearest points (100 in this case) have already been used.
datal = length(data);
marked = ones(datal,1);
m = 100; % starting point - can be varied
marked(m)=0; % starting point marked as 'used'
adj = zeros(datal);
IDX = knnsearch(data, data,'K',100);
for n = 1:datal;
x = find(marked(IDX(m,:))==1,1,'first'); % nearest unused neighbour
adj(m,IDX(m,x))=1; % two points marked as connected in adj
marked(IDX(m,x))=0; % point marked as used
m = IDX(m,x);
end
gplot(adj, data); hold on; plot(data(:,1),data(:,2),'rx');
Upvotes: 1
Reputation: 5073
There are a number of ways you can do this, for instance as follows:
img=im2double(imread('SWal5.png'));
m = bwmorph(~img,'shrink',Inf);
[ix iy] = find(m);
tri = delaunay(iy,ix);
image(img)
hold on
triplot(tri,iy,ix,'g')
set(gca,'Ydir','reverse')
axis square off
If instead you want something akin to a plot
, then you can try this after running the find
step:
[ix ii]=sort(ix);
iy = iy(ii);
imshow(img)
hold on
plot(iy,ix,'k')
set(gca,'Ydir','reverse')
Upvotes: 9