File Exchange

image thumbnail

bmorph - binary morphological transforms

version (152 KB) by tudor dima
Fast binary erosion and dilation, with even faster iterative calls.


Updated 30 Apr 2012

View License

This function performs binary erosion or dilation.

It is faster than imerode.m and imdilate.m -- by more than an order of magnitude for any structuring element other than 'square' or 'disk' .
The only time it is slightly slower (about 10%) is for very small 'square' or 'disk' strels only.
Moreover, ’disk’ structuring elements in Matlab are actually octagonal, and often times a
true ’circular’ element is necessity.

It also comes in very handy when you don't have the Matlab Image Processing Toolbox :-)...

The function also allows very fast iterative calls which re-use the work done at previous iterations. (for instance when sweeping the size/size+shape of the structuring element while looking for a certain effect, a threshold in image statistics, etc.)
This re-use will make the speed increase compound exponentially, and one can also break the execution when desired.

For 2-3M pix images and strels in the range of 10-20 pixels the speed increase can be around 20x - 100x (depending on the machine cache, slightly on strel shape, etc.), with higher values as the image and/or strel size increase.

Simple call:
IMG_OUT = BMORPH(IMG_IN, SE, DilateNotErode, [], ObjStopAtEdge)

IMG_DIL = BMORPH(IMG_IN, SE, true); % dilate
IMG_ERO = BMORPH(IMG_IN, SE, false); % erode
% erode assuming the objects stop at edge :
IMG_ERO = BMORPH(IMG_IN, SE, false, [], true);

For iterative calls and more info on execution speed see the help in bmorph.m and the attached .pdf file.

Cite As

tudor dima (2020). bmorph - binary morphological transforms (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (7)

tudor dima

@T P
The array of boolean masks is for _iterative_calls_ , this is detailed in the help file.
For straight imdilate, imerode replacement just use the simple calls as quoted in the 'Overview' above :

IMG_DIL = BMORPH(IMG_IN, SE, true); % dilate
IMG_ERO = BMORPH(IMG_IN, SE, false); % erode
Both IMG_IN and the structuring element SE are to be defined as _matrix_ of boolean (NO 'strel' object type!).

It's been a long time since I wrote this, but this should get you running.
If not, let me know.
If yes maybe you can send me a link to the work you use it for.
(and make sure that the account you write from does accept email from z-ahoo dot com :-), for now it bounces)


Hi, I'm struggling to understand what to input as 'B'/'B_prev'. The guide says 'First generate B as a cell array of boolean masks of increasing size.' but it isn't clear to me what this means... What you be able to provide specific code for generating 'B' please? For reference, I'm looking to dilate with a circular structural element.

Thanks in advance!

tudor dima

Different results are obtained when different strels are used. Incidentally, this is exactly why this box was built, for the "circular" strel in matlab is actually octagonal. It is stated in the first paragraph of the description.

As for the difference between the matlab dilation/erosion operations and this implementation you could try reading the included .pdf file.

No, I have no suggestions on grayscale morphology.

Thanks for the really fast code.
However, I do not get the expected results as the original imdilate. Would you please tell the difference between the standard dilation/erosion operations and your implementation in terms of functionality?
Another question is that, do you have a speed-up suggestion for grayscale morphology?

tudor dima

The structuring element is passed to this function (or to imdilate/imerode, that is).
I am not sure how is it done in Matlab (it's a .mex, twice wrapped :-))

What I suspect it makes the interpreted bmorph to run faster than the MEX is that I do a contour detection on both the image and the strel, then only update those values, the strel is not swept across the whole image.

Until now it only allowed symmetrical strels, but I have recently posted the new version which fixed this. (Thanks to Sean Sklios for the provided test data and also for the push!)


Great code! Very useful if you don't have the Image Processing Toolbox.

One question: how do you set up the 'circular' structuring element? This seems to be where you get the speed advantage, but I'm not sure how you implement. Thanks.

much faster! This is exactly what I am looking for to use in my image processing code. However, it did not give the same results as imerode for me. Help please?


v.0.3b, 29.04.2012
- allow asymmetrical strels
- small speed improvements

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