Function File: bwmorph (bw, operation)
Function File: bwmorph (bw, operation, n)

Perform morphological operation on binary image.

For a binary image bw, performs the morphological operation, n times. All possible values of operation are listed on the table below. By default, n is 1. If n is Inf, the operation is continually performed until it no longer changes the image.

In some operations, bw can be a binary matrix with any number of dimensions (see details on the table of operations).

Note that the output will always be of class logical, independently of the class of bw.

bothat

Performs a bottom hat operation, a closing operation (which is a dilation followed by an erosion) and finally subtracts the original image (see imbothat). bw can have any number of dimensions, and strel ("hypercube", ndims (bw), 3) is used as structuring element.

bridge

Performs a bridge operation. Sets a pixel to 1 if it has two nonzero neighbours which are not connected, so it "bridges" them. There are 119 3-by-3 patterns which trigger setting a pixel to 1.

clean

Performs an isolated pixel remove operation. Sets a pixel to 0 if all of its eight-connected neighbours are 0. bw can have any number of dimensions in which case connectivity is (3^ndims(bw)) -1, i.e., all of the elements around it.

close

Performs closing operation, which is a dilation followed by erosion (see imclose). bw can have any number of dimensions, and strel ("hypercube", ndims (bw), 3) is used as structuring element.

diag

Performs a diagonal fill operation. Sets a pixel to 1 if that eliminates eight-connectivity of the background.

dilate

Performs a dilation operation (see imdilate). bw can have any number of dimensions, and strel ("hypercube", ndims (bw), 3) is used as structuring element.

endpoints

Finds the endpoints of a skeleton. The skeleton can be computed using bwmorph (bw, "skel").

erode

Performs an erosion operation (see imerode). bw can have any number of dimensions, and strel ("hypercube", ndims (bw), 3) is used as structuring element.

fill

Performs a interior fill operation. Sets a pixel to 1 if all four-connected pixels are 1. bw can have any number of dimensions in which case connectivity is (2*ndims(bw)).

hbreak

Performs a H-break operation. Breaks (sets to 0) pixels that are H-connected.

majority

Performs a majority black operation. Sets a pixel to 1 if the majority of the pixels (5 or more for a two dimensional image) in a 3-by-3 window is 1. If not set to 0. bw can have any number of dimensions in which case the window has dimensions repmat (3, 1, ndims (bw)).

open

Performs an opening operation, which is an erosion followed by a dilation (see imopen). bw can have any number of dimensions, and strel ("hypercube", ndims (bw), 3) is used as structuring element.

remove

Performs a iterior pixel remove operation. Sets a pixel to 0 if all of its four-connected neighbours are 1. bw can have any number of dimensions in which case connectivity is (2*ndims(bw)).

shrink

Performs a shrink operation. Sets pixels to 0 such that an object without holes erodes to a single pixel (set to 1) at or near its center of mass. An object with holes erodes to a connected ring lying midway between each hole and its nearest outer boundary. It preserves Euler number.

skel

Performs a skeletonization operation. It calculates a "median axis skeleton" so that points of this skeleton are at the same distance of its nearby borders. It preserver Euler number. Please read compatibility notes for more info.

It uses the same algorithm as skel-pratt but this could change for compatibility in the future.

skel-lantuejoul

Performs a skeletonization operation as described in Gonzalez & Woods "Digital Image Processing" pp 538-540. The text references Lantuejoul as author of this algorithm.

It has the beauty of being a clean and simple approach, but skeletons are thicker than they need to and, in addition, not guaranteed to be connected.

This algorithm is iterative. It will be applied the minimum value of n times or number of iterations specified in algorithm description. It’s most useful to run this algorithm with n=Inf.

bw can have any number of dimensions.

skel-pratt

Performs a skeletonization operation as described by William K. Pratt in "Digital Image Processing".

spur

Performs a remove spur operation. It sets pixel to 0 if it has only one eight-connected pixel in its neighbourhood.

thicken

Performs a thickening operation. This operation "thickens" objects avoiding their fusion. Its implemented as a thinning of the background. That is, thinning on negated image. Finally a diagonal fill operation is performed to avoid "eight-connecting" objects.

thin-pratt

Performs a thinning operation, according to W. K. Pratt, "Digital Image Processing", 3rd Edition, pp 413-414.

thin

Performs a thinning operation. When n=Inf, thinning sets pixels to 0 such that an object without holes is converted to a stroke equidistant from its nearest outer boundaries. If the object has holes it creates a ring midway between each hole and its near outer boundary. This differ from shrink in that shrink converts objects without holes to a single pixels and thin to a stroke. It preserves Euler number.

tophat

Performs a top hat operation, a opening operation (which is an erosion followed by a dilation) and finally subtracts the original image (see imtophat). bw can have any number of dimensions, and strel ("hypercube", ndims (bw), 3) is used as structuring element.

Some useful concepts to understand operators:

Operations are defined on 3-by-3 blocks of data, where the pixel in the center of the block. Those pixels are numerated as follows:

X3X2X1
X4XX0
X5X6X7

Neighbourhood definitions used in operation descriptions:

'four-connected'

It refers to pixels which are connected horizontally or vertically to X: X1, X3, X5 and X7.

'eight-connected'

It refers to all pixels which are connected to X: X0, X1, X2, X3, X4, X5, X6 and X7.

Compatibility notes:

'skel'

Algorithm used here is described in Pratt’s book. When applying it to the "circles" image in MATLAB documentation, results are not the same. Perhaps MATLAB uses Blum’s algorithm (for further info please read comments in code).

'skel-pratt'

This option is not available in MATLAB.

'skel-lantuejoul'

This option is not available in MATLAB.

'thin-pratt'

This option is not available in MATLAB.

'thicken'

This implementation also thickens image borders. This can easily be avoided i necessary. MATLAB documentation doesn’t state how it behaves.

References: W. K. Pratt, "Digital Image Processing" Gonzalez and Woods, "Digital Image Processing"

See also: imdilate, imerode, imtophat, imbothat, makelut, applylut.

Demonstration 1

The following code

 bwmorph (true (11), "shrink", Inf)
 # Should return 0 matrix with 1 pixel set to 1 at (6,6)

Produces the following figure

Figure 1

Package: image