MATLAB Answers

Ad Mo
0

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

Asked by Ad Mo
on 18 Sep 2019 at 9:37
Latest activity Commented on by Ad Mo
on 18 Sep 2019 at 11:46
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)

  0 Comments

Sign in to comment.

1 Answer

Answer by Guillaume
on 18 Sep 2019 at 9:49
Edited by Guillaume
on 18 Sep 2019 at 9:49

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

Hi Guillaume,
Thank you for your awnser.
That's true (I forgot to put it in the copied code).
My problem can be solved in the same way I think. Indeed, B{k} erases the previous coordinates.
I need to add a loop like
v=[]
for h=1:length(B)
v=[v,B{h}]
end
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{:}];
Guillaume,
It works well.
Thank you very much

Sign in to comment.