Mahotas – 适当的图像结构元素
在本文中,我们将了解如何在 mahotas 中获取图像的适当结构元素。结构化元素是一个矩阵,用于标识正在处理的图像中的像素,并定义在处理每个像素时使用的邻域。
在本教程中,我们将使用“lena”图像,下面是加载它的命令。
mahotas.demos.load('lena')
下面是莉娜的图片
In order to do this we will use mahotas.get_structuring_elem method
Syntax : mahotas.get_structuring_elem(img, n)
Argument : It takes image object and integer as argument
Return : It returns numpy ndarray
注意:输入图像应被过滤或应加载为灰色
为了过滤图像,我们将获取图像对象 numpy.ndarray 并在索引的帮助下对其进行过滤,下面是执行此操作的命令
image = image[:, :, 0]
下面是实现
Python3
# importing required libraries
import mahotas
import mahotas.demos
from pylab import gray, imshow, show
import numpy as np
import matplotlib.pyplot as plt
# loading image
img = mahotas.demos.load('lena')
# filtering image
img = img.max(2)
print("Image")
# showing image
imshow(img)
show()
# getting structring element
value = mahotas.get_structuring_elem(img, 1)
# showing value
print(value)
Python3
# importing required libraries
import mahotas
import numpy as np
from pylab import gray, imshow, show
import os
import matplotlib.pyplot as plt
# loading image
img = mahotas.imread('dog_image.png')
# filtering image
img = img[:, :, 0]
print("Image")
# showing image
imshow(img)
show()
# getting structring element
value = mahotas.get_structuring_elem(img, 2)
# showing value
print(value)
输出 :
Image
[[0 1 0]
[1 1 1]
[0 1 0]]
另一个例子
Python3
# importing required libraries
import mahotas
import numpy as np
from pylab import gray, imshow, show
import os
import matplotlib.pyplot as plt
# loading image
img = mahotas.imread('dog_image.png')
# filtering image
img = img[:, :, 0]
print("Image")
# showing image
imshow(img)
show()
# getting structring element
value = mahotas.get_structuring_elem(img, 2)
# showing value
print(value)
输出 :
Image
[[1 1 1]
[1 1 1]
[1 1 1]]