Save plotted lines in an matrix

Here's my code to obtain these two lines:
[B,L,N,A] = bwboundaries(ImgClosed);
figure
imshow(ImgClosed)
hold on;
% Loop through object boundaries
for k = 1:N
% Boundary k is the parent of a hole if the k-th column of the adjacency matrix A contains a non-zero element
if (nnz(A(:,k)) > 0)
boundary = B{k};
plot(boundary(:,2),boundary(:,1),'r','LineWidth',2);
% Loop through the children of boundary k
for l = find(A(:,k))'
boundary = B{l};
plot(boundary(:,2),boundary(:,1),'g','LineWidth',2);
end
end
end
As ImgClosed is the binary image of the object I'm analysing.
And this is the result:
I want to save both lines (red and green) in an unique matrix so i can plot them again on top of another image. How can i save them?

 Accepted Answer

You can save the plot and extract the XData and YData from it's subfields as shown below.
[B,L,N,A] = bwboundaries(ImgClosed);
figure
imshow(ImgClosed)
hold on;
% Loop through object boundaries
for k = 1:N
% Boundary k is the parent of a hole if the k-th column of the adjacency matrix A contains a non-zero element
if (nnz(A(:,k)) > 0)
boundary = B{k};
redline = plot(boundary(:,2),boundary(:,1),'r','LineWidth',2);
% Loop through the children of boundary k
for l = find(A(:,k))'
boundary = B{l};
greenline = plot(boundary(:,2),boundary(:,1),'g','LineWidth',2);
end
end
end
figure
axes
plot(redline.XData,redline.YData,'r','LineWidth',2)
hold on
plot(greenline.XData,greenline.YData,'g','LineWidth',2);

4 Comments

The thing is: I need to constrain a delaunay triangulation using those contours. And with that I'm still not able to do it properly
DT = delaunayTriangulation(x,y,C)
The C in the code above, should be my contours (both red and green lines mentioned before)
ImgClosed = imread('test.png');
ImgClosed=im2bw(ImgClosed);
[B,L,N,A] = bwboundaries(ImgClosed);
figure
imshow(ImgClosed)
hold on;
for k = 1:N
% Boundary k is the parent of a hole if the k-th column of the adjacency matrix A contains a non-zero element
if (nnz(A(:,k)) > 0)
boundary = B{k};
redline = plot(boundary(:,2),boundary(:,1),'r','LineWidth',2);
% Loop through the children of boundary k
for l = find(A(:,k))'
boundary = B{l};
greenline(l,:) = plot(boundary(:,2),boundary(:,1),'g','LineWidth',2);
end
end
end
figure
axes
plot(redline.XData,redline.YData,'r','LineWidth',2)
hold on
x=redline.XData;
y=redline.YData;
c = [(1:length(x)-1).' (2:length(x)).'];
for jj = 1:size(greenline,1)
try
plot(greenline(jj).XData,greenline(jj).YData,'g','LineWidth',2);
x_start = length(x);
x=[x greenline(jj).XData];
y=[y greenline(jj).YData];
c = [c;(x_start+(1:length(greenline(jj).XData)-1)).' (x_start+(2:length(greenline(jj).XData))).'];
catch
end
end
DT = delaunayTriangulation(x',y',c);
Warning: Duplicate data points have been detected and removed.
The Triangulation indices and constraints are defined with respect to the unique set of points in delaunayTriangulation.
% IC = incenter(DT);
triplot(DT)
figure
DT = delaunayTriangulation(x',y',c);
Warning: Duplicate data points have been detected and removed.
The Triangulation indices and constraints are defined with respect to the unique set of points in delaunayTriangulation.
IO = isInterior(DT);
triplot(DT(IO, :),DT.Points(:,1), DT.Points(:,2),'LineWidth', 2)
Thank you so much! Is there anyway I can densify the triangles inside the object? So I can get something like the delaunay triangulation I showed above?
I was able to do so and posted the results here.

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2021b

Community Treasure Hunt

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

Start Hunting!