File Exchange

image thumbnail

Trace perimeter of binary image with line segments (bwperimtrace)

version (3.04 KB) by Daniel Warren
Traces the outer boundaries of non-zero pixels in a monochrome image


Updated 24 Aug 2015

View License

Lines surrounding contiguous regions in an image can be extracted using MATLAB's built-in contour/contourc functions, but these functions perform linear interpolation between pixels leading to 'cut corners'. bwperim from the imaging toolbox will trace boundaries without interpolation, but only provides its result as a pixel map. This function, bwperimtrace, instead outputs line segments that entirely contain all non-zero pixels in the input image. The package also includes contourc4, a replacement for contourc, which doesn't perform any interpolation between pixels.

Cite As

Daniel Warren (2021). Trace perimeter of binary image with line segments (bwperimtrace) (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (4)

Jiexian Ma

Bug in bwperimtrace.m. It fail to get 4-connected neighbourhood in the following case.
>> im=[ 1 0 0; 1 0 1; 1 1 0];
>> b= bwperimtrace( im == 1 );

Jiexian Ma

Small bug in bwperimtrace.m, Lines 27 ( in = in'; ) should be moved to line 14. In the code, x is column direction, y is row direction.

Jiexian Ma

Powerful function! Work for me!
Small bug, on lines 32 and 33, change to:
xpix = xlims(1):dx:xlims(end);
ypix = ylims(1):dy:ylims(end);

Kevin Holly

MATLAB Release Compatibility
Created with R2014a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Inspired: Im2mesh (2D image to triangular meshes)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!