Trace object in binary image
B = bwtraceboundary(BW,P,fstep)
B = bwtraceboundary(BW,P,fstep,conn)
B = bwtraceboundary(BW,P,fstep,conn,m,dir)
traces the outline of an object in binary image
B = bwtraceboundary(
pixels belong to an object and zero-valued pixels constitute the background.
P specifies the row and column coordinates of the point on
the object boundary where you want the tracing to begin.
specifies the initial search direction for the next object pixel connected to
B holds the row and column
coordinates of the boundary pixels for the region.
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);
BW— Binary image
Binary image, specified as a 2-D numeric or logical matrix.
P— Coordinates of starting point
Coordinates of starting point on the object boundary where you want the
tracing to begin, specified as a 2-element vector of the format
fstep— Initial search direction
conn— Pixel connectivity
Pixel connectivity, specified as
Pixels are connected if their edges touch. The neighborhood of a pixel are the adjacent pixels in the horizontal or vertical direction.
Pixels are connected if their edges or corners touch. The neighborhood of a pixel are the adjacent pixels in the horizontal, vertical, or diagonal direction.
m— Maximum number of boundary pixels to extract
Inf(default) | positive integer
Maximum number of boundary pixels to extract, specified as a positive
integer. By default,
bwtraceboundary identifies all the pixels on the
dir— Direction in which to trace boundary
Direction in which to trace boundary, specified as
B— Row and column coordinates of boundary pixels
Row and column coordinates of the boundary pixels for the region, returned
as a q-by-2 matrix. Each row in
has the form
bwtraceboundary function implements the Moore-Neighbor tracing
algorithm modified by Jacob's stopping criteria. This function is based on the
boundaries function presented in the first edition of
Digital Image Processing Using MATLAB, by Gonzalez, R. C., R.
E. Woods, and S. L. Eddins, New Jersey, Pearson Prentice Hall, 2004.
 Gonzalez, R. C., R. E. Woods, and S. L. Eddins, Digital Image Processing Using MATLAB, New Jersey, Pearson Prentice Hall, 2004.
Usage notes and limitations:
This function supports the generation of C code using MATLAB® Coder™. For more information, see Code Generation for Image Processing.
When generating code, the
must be compile-time constants.