Mahotas – 图像打开过程
在本文中,我们将了解如何在 mahotas 中对图像执行打开操作。开运算是先进行腐蚀运算,再进行膨胀运算的过程。它消除了所获得图像的细突起,用于去除所获得图像的内部噪声。
在本教程中,我们将使用“luispedro”图像,下面是加载它的命令。
mahotas.demos.load('luispedro')
下面是 luispedro 图像
为此,我们将使用 mahotas.morph.openmethod
Syntax : mahotas.morph.open(image)
Argument :It takes image object as argument
Return : It returns image object
注意:输入图像应被过滤或应加载为灰色
为了过滤图像,我们将获取图像对象 numpy.ndarray 并在索引的帮助下对其进行过滤,下面是执行此操作的命令
image = image[:, :, 0]
下面是实现
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()
# opening image
new_img = mahotas.morph.open(img)
# showing new image
print("Opened Image")
imshow(new_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()
# opening image
new_img = mahotas.morph.open(img)
# showing new image
print("Opened Image")
imshow(new_img)
show()
输出 :
Image threshold using Otsu Method
Opened 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()
# opening image
new_img = mahotas.morph.open(img)
# showing new image
print("Opened Image")
imshow(new_img)
show()
输出 :
Image threshold using Otsu Method
Opened Image