Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Plot watershed boundaries

Subject: Plot watershed boundaries

From: Bard R.

Date: 13 Sep, 2011 12:40:32

Message: 1 of 2

Hi,
I have a labeled image L which is produced using the watershed function.
Now I want to plot a boundary around each region, but I want the boundaries to be placed on the watershed line (center of the background pixel) rather than along the edge of each region.
As you can see from the example below bwboundaries does not give me the result I want. I thought I could do this by dilating each region individually such as, but that doesnt work well either. I guess I may have to trace the background pixels directly in some way rather than the boundary of the region itself. Any suggestions would be much appreciated.

%Example code:
Z = peaks(50);
L = watershed(Z,4);
figure, imagesc(L)
title('Inner boundaries')
%Get inner boundaries...
B1 = bwboundaries(L,4,'noholes');
%...and plot them
hold on
for k = 1:length(B1)
    boundary = B1{k};
    plot(boundary(:,2), boundary(:,1), 'w', 'LineWidth', 2)
end
%
%I thought this would do the trick...
%
figure, imagesc(L)
title('Better, but not quite')
hold on
for i = 1:max(L(:))
    boundary = bwboundaries(imdilate(L==i,[0 1 1;1 1 1;0 1 0]),4,'noholes');
    boundary = boundary{1};
    plot(boundary(:,2), boundary(:,1), 'g', 'LineWidth', 2)
end
%
%But as you see that doesn't work well in the corners.
%

Subject: Plot watershed boundaries

From: Wolfgang Schwanghart

Date: 13 Sep, 2011 13:55:29

Message: 2 of 2

Hi Bard,

I have written a code which I haven't submitted to the FEX so far, which does what you need. Just send me a message via the contact formula with your email address and I'll send it to you.

Cheers,
W.

"Bard R." wrote in message <j4nj00$s0j$1@newscl01ah.mathworks.com>...
> Hi,
> I have a labeled image L which is produced using the watershed function.
> Now I want to plot a boundary around each region, but I want the boundaries to be placed on the watershed line (center of the background pixel) rather than along the edge of each region.
> As you can see from the example below bwboundaries does not give me the result I want. I thought I could do this by dilating each region individually such as, but that doesnt work well either. I guess I may have to trace the background pixels directly in some way rather than the boundary of the region itself. Any suggestions would be much appreciated.
>
> %Example code:
> Z = peaks(50);
> L = watershed(Z,4);
> figure, imagesc(L)
> title('Inner boundaries')
> %Get inner boundaries...
> B1 = bwboundaries(L,4,'noholes');
> %...and plot them
> hold on
> for k = 1:length(B1)
> boundary = B1{k};
> plot(boundary(:,2), boundary(:,1), 'w', 'LineWidth', 2)
> end
> %
> %I thought this would do the trick...
> %
> figure, imagesc(L)
> title('Better, but not quite')
> hold on
> for i = 1:max(L(:))
> boundary = bwboundaries(imdilate(L==i,[0 1 1;1 1 1;0 1 0]),4,'noholes');
> boundary = boundary{1};
> plot(boundary(:,2), boundary(:,1), 'g', 'LineWidth', 2)
> end
> %
> %But as you see that doesn't work well in the corners.
> %

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us