Python Dilation and Erosion using Mahotas














































Python Dilation and Erosion using Mahotas



Dilation and erosion are very basic operators. These operators are available in mahotas.morph module of mahotas. 
Dilation are two opposite operations as dilation adds pixels on the boundaries of the object in image where as erosion will remove pixels from the boundaries of the object in image.

You can specify the structuring element by yourself using :
se = np.array([
[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]], bool)


CODE :
# importing required libraries
import mahotas
import numpy as np
from pylab import imshow, show

# loading image
img = mahotas.imread('HarryPotter.jpg')

# setting filter to the image
img = img[:, :, 0]

# image threshold using otsu method
T_otsu = mahotas.otsu(img)

# image values should be greater than otsu value
img = img > T_otsu

print("Image threshold using Otsu Method")

# showing image
imshow(img)
show()

# erode structure
erode_s = np.array([
[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]], bool)

# dilating image
dilate_image = mahotas.morph.dilate(img, erode_s)

# showing dilated image
print("Dilated Image")
imshow(dilate_image)
show()

erode_image =mahotas.morph.erode(img)

# showing eroded image
print("Eroded Image")
imshow(erode_image)
show()

Output :




Comments