bwmorph
Morphological operations on binary images
Description
Examples
Perform Morphological Operations on Binary Image
Read binary image and display it.
BW = imread('circles.png');
imshow(BW);
Remove interior pixels to leave an outline of the shapes.
BW2 = bwmorph(BW,'remove');
figure
imshow(BW2)
Get the image skeleton.
BW3 = bwmorph(BW,'skel',Inf);
figure
imshow(BW3)
Input Arguments
BW
— Binary image
2D numeric matrix  2D logical matrix
Binary image, specified as a 2D numeric matrix or 2D logical matrix. For numeric input,
any nonzero pixels are considered to be 1
(true
).
operation
— Morphological operation to perform
character vector  string scalar
Morphological operation to perform, specified as one of the following.
Operation  Description 

 Perform the morphological bottom hat operation, returning the image minus the morphological closing of the image. The

 Find branch points of skeleton. For example: 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 1 1 1 1 becomes 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 Note: To find
branch points, the image must be skeletonized. To create a skeletonized image,
use 
 Bridge unconnected pixels, that is, sets 1 0 0 1 1 0 1 0 1 becomes 1 1 1 0 0 1 0 1 1 
 Remove isolated pixels (individual 0 0 0 0 1 0 0 0 0 
 Perform morphological closing (dilation followed by erosion). The

 Use diagonal fill to eliminate 8connectivity of the background. For example: 0 1 0 0 1 0 1 0 0 becomes 1 1 0 0 0 0 0 0 0 
 Find end points of skeleton. For example: 1 0 0 0 1 0 0 0 0 1 0 0 becomes 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 Note: To find end
points, the image must be skeletonized. To create a skeletonized image, use

 Fill isolated interior pixels (individual 1 1 1 1 0 1 1 1 1 
 Remove Hconnected pixels. For example: 1 1 1 1 1 1 0 1 0 becomes 0 0 0 1 1 1 1 1 1 
 Set a pixel to 
 Perform morphological opening (erosion followed by dilation). The

 Remove interior pixels. This option sets a pixel to 
 With 
 With When working
with 3D volumes, or when you want to prune a skeleton, use the 
 Remove spur pixels. For example: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 becomes 0 0 0 0 0 1 0 0 0 1 0 0 1 1 0 0 1 1 0 0 
 With 
 With 
 Perform the morphological top hat operation, returning the image minus the morphological opening of the image. The

Tip
To perform morphological erosion or dilation, use the imerode
or imdilate
function, respectively. If you
want to replicate the dilation or erosion performed by the
bwmorph
function, then specify the neighborhood as
ones(3)
.
Data Types: char
 string
n
— Number of times to perform operation
positive integer  Inf
Number of times to perform the operation, specified as a positive integer or
Inf
. When you specify n
as
Inf
, the bwmorph
function repeats the
operation until the image no longer changes.
Example: 100
Data Types: single
 double
 int8
 int16
 int32
 int64
 uint8
 uint16
 uint32
 uint64
Output Arguments
BW2
— Image after morphological operations
2D logical matrix
Image after morphological operations, returned as a 2D logical matrix.
Data Types: logical
Algorithms
When used with the "thin"
option, bwmorph
uses the
following algorithm [3]:
In the first subiteration, delete pixel p if and only if the conditions G_{1}, G_{2}, and G_{3} are all satisfied.
In the second subiteration, delete pixel p if and only if the conditions G_{1}, G_{2}, and $${G}_{3}\prime $$ are all satisfied.
Condition G1:
$${X}_{H}(p)=1$$
where
$${X}_{H}(p)={\displaystyle \sum _{i=1}^{4}{b}_{i}}$$
$${b}_{i}=\{\begin{array}{c}1,\text{if}{x}_{2i1}=0\text{and}({x}_{2i}=1\text{or}{x}_{2i+1}=1)\\ 0\text{,otherwise}\end{array}$$
x_{1}, x_{2}, ..., x_{8} are the values of the eight neighbors of p, starting with the east neighbor and numbered in counterclockwise order.
Condition G2:
$$2\le \mathrm{min}\left\{{n}_{1}(p),{n}_{2}(p)\right\}\le 3$$
where
$${n}_{1}(p)={\displaystyle \sum _{k=1}^{4}{x}_{2k1}\vee {x}_{2k}}$$
$${n}_{2}(p)={\displaystyle \sum _{k=1}^{4}{x}_{2k}\vee {x}_{2k+1}}$$
Condition G3:
$$({x}_{2}\vee {x}_{3}\vee {\overline{x}}_{8})\wedge {x}_{1}=0$$
Condition G3':
$$({x}_{6}\vee {x}_{7}\vee {\overline{x}}_{4})\wedge {x}_{5}=0$$
The two subiterations together make up one iteration of the
thinning algorithm. When the user specifies an infinite number of
iterations (n=Inf
), the iterations are repeated
until the image stops changing. The conditions are all tested using applylut
with
precomputed lookup tables.
References
[1] Haralick, Robert M., and Linda G. Shapiro, Computer and Robot Vision, Vol. 1, AddisonWesley, 1992.
[2] Kong, T. Yung and Azriel Rosenfeld, Topological Algorithms for Digital Image Processing, Elsevier Science, Inc., 1996.
[3] Lam, L., SeongWhan Lee, and Ching Y. Suen, "Thinning MethodologiesA Comprehensive Survey," IEEE Transactions on Pattern Analysis and Machine Intelligence, Vol 14, No. 9, September 1992, page 879, bottom of first column through top of second column.
[4] Pratt, William K., Digital Image Processing, John Wiley & Sons, Inc., 1991.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
bwmorph
supports the generation of C code (requires MATLAB^{®} Coder™). Note that if you choose the genericMATLAB Host Computer
target platform,bwmorph
generates code that uses a precompiled, platformspecific 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 Types of Code Generation Support in Image Processing Toolbox.When generating code, the character vectors or string scalars specifying the operation must be a compiletime constant and, for best results, the input image must be of class
logical
.
GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.
Usage notes and limitations:
When generating code, the character vectors or string scalars specifying the operation must be a compiletime constant and, for best results, the input image must be of class
logical
.
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
This function fully supports GPU arrays. For more information, see Image Processing on a GPU.
Version History
Introduced before R2006a
See Also
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
 América Latina (Español)
 Canada (English)
 United States (English)
Europe
 Belgium (English)
 Denmark (English)
 Deutschland (Deutsch)
 España (Español)
 Finland (English)
 France (Français)
 Ireland (English)
 Italia (Italiano)
 Luxembourg (English)
 Netherlands (English)
 Norway (English)
 Österreich (Deutsch)
 Portugal (English)
 Sweden (English)
 Switzerland
 United Kingdom (English)