bwboundaries : missing coordinates of one of the two borders of my image

1 view (last 30 days)
HI,
I have a problem with the function bwboundaries. When I use it with a black and white image (containing 2 pores), I can extract only the coordinates of a single border (whereas I have two). The plot gives me two disctinct border.
How can I have the coordinates for both borders?
I put the short code and the picture below.
Thank you
Image = imread('GDgeoRE2.bmp');
I = im2bw(Image);
Ifill = imfill(I,'holes');
B = bwboundaries(Ifill);
for k = 1:length(B)
b = B{k};
plot(b(:,1),b(:,2),'r','linewidth',2);
end
c1 = downsample(b(:,1),1);
c2 = downsample(b(:,2),1);
disp(b)
disp(c1)
disp(c2)

Answers (1)

Guillaume
Guillaume on 18 Sep 2019
Edited: Guillaume on 18 Sep 2019
The problem has nothing to do with bwboundary. The problem is with you calling plot in a loop without hold on, so each time you call plot it erases the previous plot. Hence you're left with just the last boundary plot.
figure;
hold on;
for k = 1:length(B)
b = B{k};
plot(b(:,1),b(:,2),'r','linewidth',2);
end
would fix that.
  3 Comments
Guillaume
Guillaume on 18 Sep 2019
I don't really understand what you are saying.
You could indeed concatenate all the boundaries into one array and plot it all at once but you'll get a line linking all the boundaries.
The concatenation can be done more efficiently without a loop:
v = [B{:}];

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!