Mahotas – 图像的欧拉数
在本文中,我们将了解如何在 mahotas 中获取图像的欧拉数。欧拉数是图像拓扑的度量。它被定义为图像中的对象总数减去这些对象中的孔数。您可以使用 4 或 8 连接的社区。
在本教程中,我们将使用“lena”图像,下面是加载它的命令。
mahotas.demos.load('lena')
下面是莉娜的图片
In order to do this we will use mahotas.euler method
Syntax : mahotas.euler(img)
Argument : It takes image object as argument
Return : It returns integer
注意:输入图像应被过滤或加载为灰色
为了过滤图像,我们将获取图像对象 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
img = mahotas.demos.load('lena')
# 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")
# creating a labeled image
marker, n_nucleus = mahotas.label(img)
# showing image
imshow(img)
show()
# euler number of image of image
euler = mahotas.euler(img)
print("Euler Number of Image : " + str(euler))
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')
# 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()
# euler number of image of image
euler = mahotas.euler(img)
print("Euler Number of Image : " + str(euler))
输出 :
Image threshold using Otsu Method
Euler Number of Image : 54.25
另一个例子
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')
# 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()
# euler number of image of image
euler = mahotas.euler(img)
print("Euler Number of Image : " + str(euler))
输出 :
Image threshold using Otsu Method
Euler Number of Image : 76.75