Mahotas – 扩张图像的元素结构
在本文中,我们将了解如何在 mahotas 中设置图像扩张的元素结构。膨胀将像素添加到图像中对象的边界,而腐蚀则删除对象边界上的像素。从图像中的对象添加或删除的像素数量取决于用于处理图像的结构元素的大小和形状。为了对图像进行扩张,我们使用 mahotas.morph.dilate 方法。通过设置元素结构,我们可以增加或减少对图像的扩张效果。
在本教程中,我们将使用“luispedro”图像,下面是加载它的命令。
mahotas.demos.load('luispedro')
下面是 luispedro 图像
Implementation steps :
1. Load the image
2. Filter the image
3. Use otsu method for threshold of the image
4. Create a structure of the element with the help of numpy ndarray for binary values
5. Use the element for dilating the image
下面是实现
Python3
# importing required libraries
import mahotas
import mahotas.demos
from pylab import gray, imshow, show
import numpy as np
# loading image
luispedro = mahotas.demos.load('luispedro')
# filtering image
luispedro = luispedro.max(2)
# otsu method
T_otsu = mahotas.otsu(luispedro)
# image values should be greater than otsu value
img = luispedro > T_otsu
print("Image threshold using Otsu Method")
# showing image
imshow(img)
show()
# erode structure
es = np.array([
[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]], bool)
# dilating image
dilate_img = mahotas.morph.dilate(img, es)
# showing dilated image
print("Dilated Image")
imshow(dilate_img)
show()
Python3
# importing required libraries
import mahotas
import numpy as np
import matplotlib.pyplot as plt
import os
# loading image
img = mahotas.imread('dog_image.png')
# setting filter to the image
img = img[:, :, 0]
# 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
es = np.array([
[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]], bool)
# dilating image
dilate_img = mahotas.morph.dilate(img, es)
# showing dilated image
print("Dilated Image")
imshow(dilate_img)
show()
输出 :
Image threshold using Otsu Method
Dilated Image
另一个例子
Python3
# importing required libraries
import mahotas
import numpy as np
import matplotlib.pyplot as plt
import os
# loading image
img = mahotas.imread('dog_image.png')
# setting filter to the image
img = img[:, :, 0]
# 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
es = np.array([
[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]], bool)
# dilating image
dilate_img = mahotas.morph.dilate(img, es)
# showing dilated image
print("Dilated Image")
imshow(dilate_img)
show()
输出 :
Image threshold using Otsu Method
Dilated Image