New in version 0.8: open() & close() were added in version 0.8
Morphological operators were the first operations in mahotas (back then, it was even, briefly, just a single C++ module called morph). Since then, mahotas has grown a lot. This module, too, has grown and acquired more morphological operators as well as being optimised for speed.
Let us first select an interesting image
(Source code, png, hires.png, pdf)
Dilation and erosion are two very basic operators (mathematically, you only need one of them as you can define the erosion as dilation of the negative or vice-versa).
mahotas.morph.dilate(eye)
Dilation is, intuitively, making positive areas “fatter”:
(Source code, png, hires.png, pdf)
mahotas.morph.erode(eye)
Erosion, by contrast, thins them out:
(Source code, png, hires.png, pdf)
Mahotas supports greyscale erosion and dilation (depending on the dtype of the arguments) and you can specify any structuring element you wish (including non-flat ones). By default, a 1-cross is used:
# if no structure-element is passed, use a cross:
se = np.array([
[0, 1, 0],
[1, 1, 1],
[0, 1, 0]])
However, you can use whatever structuring element you want:
se = np.array([
[1, 1, 0],
[1, 1, 1],
[0, 1, 1]])
dilated = mahotas.morph.dilate(eye, se)
eroded = mahotas.morph.erode(eye, se)
Closing and opening are based on erosion and dilation. Again, they work in greyscale and can use an arbitrary structure element.
Here is closing:
mahotas.morph.close(eye)
(Source code, png, hires.png, pdf)
And here is opening:
mahotas.morph.open(eye)
(Source code, png, hires.png, pdf)
Both close and open take an optional structuring element as a second argument:
mahotas.morph.open(eye, se)