| Image Processing Toolbox™ | ![]() |
B = bwtraceboundary(BW,P,fstep) B = bwtraceboundary(BW,P,fstep,conn) B = bwtraceboundary(...,N,dir)
B = bwtraceboundary(BW, P,fstep) traces the outline of an object in binary image bw. Nonzero pixels belong to an object and 0 pixels constitute the background. P is a two-element vector specifying the row and column coordinates of the point on the object boundary where you want the tracing to begin.
fstep is a string specifying the initial search direction for the next object pixel connected to P. You use strings such as 'N' for north, 'NE' for northeast, to specify the direction. The following figure illustrates all the possible values for fstep.

bwtraceboundary returns B, a Q-by-2 matrix, where Q is the number of boundary pixels for the region. B holds the row and column coordinates of the boundary pixels.
B = bwtraceboundary(bw, P, fstep, conn) specifies the connectivity to use when tracing the boundary. conn can have either of the following scalar values.
Value | Meaning |
|---|---|
4 | 4-connected neighborhood |
8 | 8-connected neighborhood. This is the default. |
B = bwtraceboundary(...,N,dir) specifies n, the maximum number of boundary pixels to extract, and dir, the direction in which to trace the boundary. When N is set to Inf, the default value, the algorithm identifies all the pixels on the boundary. dir can have either of the following values:
Value | Meaning |
|---|---|
'clockwise' | Search in a clockwise direction. This is the default. |
'counterclockwise' | Search in counterclockwise direction. |
BW can be logical or numeric and it must be real, 2-D, and nonsparse. B, P, conn, and N are of class double. dir and fstep are strings.
Read in and display a binary image. Starting from the top left, project a beam across the image searching for the first nonzero pixel. Use the location of that pixel as the starting point for the boundary tracing. Including the starting point, extract 50 pixels of the boundary and overlay them on the image. Mark the starting points with a green x. Mark beams that missed their targets with a red x.
BW = imread('blobs.png');
imshow(BW,[]);
s=size(BW);
for row = 2:55:s(1)
for col=1:s(2)
if BW(row,col),
break;
end
end
contour = bwtraceboundary(BW, [row, col], 'W', 8, 50,...
'counterclockwise');
if(~isempty(contour))
hold on;
plot(contour(:,2),contour(:,1),'g','LineWidth',2);
hold on;
plot(col, row,'gx','LineWidth',2);
else
hold on; plot(col, row,'rx','LineWidth',2);
end
end![]() | bwselect | bwulterode | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |