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.
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.
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?
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?
Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.