Documentation

This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.

bwtraceboundary

Trace object in binary image

Syntax

B = bwtraceboundary(BW,P,fstep)
B = bwtraceboundary(bw,P,fstep,conn)
B = bwtraceboundary(bw,P,fstep,conn,n,dir)

Description

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 character vector specifying the initial search direction for the next object pixel connected to P. 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

    Note:   With this connectivity, fstep is limited to the following values: 'N', 'E', 'S', and 'W'.

8

8-connected neighborhood. This is the default.

B = bwtraceboundary(bw,P,fstep,conn,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.

Code Generation support: Yes.

MATLAB® Function Block support: No.

Class Support

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 character vectors.

Examples

collapse all

Read image and display it.

BW = imread('blobs.png');
imshow(BW,[]);

Pick an object in the image and trace the boundary. To select an object, specify a pixel on its boundary. This example uses the coordinates of a pixel on the boundary of the thick white circle, obtained through visual inspection using impixelinfo. The example specifies the initial search direction, the connectivity, how many boundary pixels should be returned, and the direction in which to perform the search.

r = 163;
c = 37;
contour = bwtraceboundary(BW,[r c],'W',8,Inf,'counterclockwise');

Plot the contour on the image.

hold on;
plot(contour(:,2),contour(:,1),'g','LineWidth',2);

More About

collapse all

Code Generation

This function supports the generation of C code using MATLAB Coder™. Note that if you choose the generic MATLAB Host Computer target platform, the function generates code that uses a precompiled, platform-specific shared library. Use of a shared library preserves performance optimizations but limits the target platforms for which code can be generated. For more information, see Understanding Code Generation with Image Processing Toolbox.

When generating code, the dir, fstep, and conn arguments must be compile-time constants.

See Also

|

Introduced before R2006a

Was this topic helpful?