Mahotas – 条件膨胀图像
在本文中,我们将了解如何在 mahotas 中对图像进行条件膨胀。膨胀将像素添加到图像中对象的边界,而腐蚀则删除对象边界上的像素。从图像中的对象添加或删除的像素数量取决于用于处理图像的结构元素的大小和形状。我们使用 mahotas.morph.dilate 方法进行正常扩张。
在本教程中,我们将使用“lena”图像,下面是加载它的命令。
mahotas.demos.load('lena')
下面是莉娜的图片
In order to do this we will use mahotas.cdilate method
Syntax : mahotas.cdilate(img, c_grey, Bc={3×3 cross}, n=1)
Argument : It takes image object, conditional image as compulsory argument, element structure and iteration number are optional argument
Return : It returns image object
注意:输入图像应被过滤或应加载为灰色
为了过滤图像,我们将获取图像对象 numpy.ndarray 并在索引的帮助下对其进行过滤,下面是执行此操作的命令
image = image[:, :, 0]
下面是实现
Python3
# importing required libraries
# importing required libraries
import mahotas
import mahotas.demos
from pylab import gray, imshow, show
import numpy as np
# loading image
img = mahotas.demos.load('lena')
# grey image
g = img[:, :, 1]
# multiplying grey image values
g = g * 100
# filtering image
img = img.max(2)
# 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()
# dilating image using conditional grey image
dilate_img = mahotas.cdilate(img, g)
# showing dilated image
print("Dilated Image")
imshow(dilate_img)
show()
Python3
# importing required libraries
import mahotas
import numpy as np
from pylab import gray, imshow, show
import os
# loading image
img = mahotas.imread('dog_image.png')
# grey image
g = img[:, :, 2]
# multiplying grey image values
g = g * 100
# filtering 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()
# dilating image using conditional grey image
dilate_img = mahotas.cdilate(img, g)
# 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
from pylab import gray, imshow, show
import os
# loading image
img = mahotas.imread('dog_image.png')
# grey image
g = img[:, :, 2]
# multiplying grey image values
g = g * 100
# filtering 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()
# dilating image using conditional grey image
dilate_img = mahotas.cdilate(img, g)
# showing dilated image
print("Dilated Image")
imshow(dilate_img)
show()
输出 :
Image threshold using Otsu Method
Dilated Image