Perform a morphological operation on a binary image.
BW2=bwmorph(BW,operation) performs a morphological operation specified by operation on binary image BW. All possible operations and their meaning are specified in a table below.
BW2=bwmorph(BW,operation,n) performs a morphological operation n times. Keep in mind that it has no sense to apply some operations more than once, since some of them return the same result regardless how many iterations we request. Those return a warning if are called with n>1 and they compute the result for n=1.
n>1 is actually used for the following operations: diag, dilate, erode, majority, shrink, skel, spur, thicken and thin.
'bothat'
- Performs a bottom hat operation, a closing operation (which is a dilation followed by an erosion) and finally substracts the original image.
'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.
'close'
- Performs closing operation, which is a dilation followed by erosion. It uses a ones(3) matrix as structuring element for both operations.
'diag'
- Performs a diagonal fill operation. Sets a pixel to 1 if that eliminates eight-connectivity of the background.
'dilate'
- Performs a dilation operation. It uses ones(3) as structuring element.
'erode'
- Performs an erosion operation. It uses ones(3) as structuring element.
'fill'
- Performs a interior fill operation. Sets a pixel to 1 if all four-connected pixels are 1.
'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 five or more pixels in a 3-by-3 window are 1. If not it is set to 0.
'open'
- Performs an opening operation, which is an erosion followed by a dilation. It uses ones(3) as structuring element.
'remove'
- Performs a iterior pixel remove operation. Sets a pixel to 0 if all of its four-connected neighbours are 1.
'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-lantuejol'
- Performs a skeletonization operation as described in Gonzalez & Woods "Digital Image Processing" pp 538-540. The text references Lantuejoul as authour 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
.'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'
- 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 substracts the original image.
Some useful concepts to understant 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:
X3 X2 X1 X4 X X0 X5 X6 X7 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:
'fill'
- Checking MATLAB behaviour is needed because its documentation doesn't make clear if it creates a black pixel if all eight-connected pixels are black or if four-connected suffice (as we do currently following Pratt's book).
'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 algoritm (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.
'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"