Mahotas – 排名过滤器
在本文中,我们将了解如何在 mahotas 中对图像应用排名过滤器。秩过滤器特别过滤掉孤立的像素,其强度与其直接邻域的强度相差很大。使用此过滤器后,将保留与这些像素相邻的具有恒定强度值的大区域以及边缘。
在本教程中,我们将使用“Lena”图像,下面是加载它的命令。
mahotas.demos.load('lena')
下面是莉娜的图片
In order to do this we will use mahotas.rank_filter method
Syntax : mahotas.rank_filter(img, neighbour, rank)
Argument : It takes image object and two integer 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
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()
# neighbour pixel
neighbour = 3
# rank
rank = 2
# applying rank filter
new_img = mahotas.rank_filter(img, neighbour, rank)
# showing image
print("Rank Filter")
imshow(new_img)
show()
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()
# neighbour pixel
neighbour = 3
# rank
rank = 2
# applying rank filter
new_img = mahotas.rank_filter(img, neighbour, rank)
# showing image
print("Rank Filter")
imshow(new_img)
show()
输出 :
Image
Rank Filter
另一个例子
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()
# neighbour pixel
neighbour = 3
# rank
rank = 2
# applying rank filter
new_img = mahotas.rank_filter(img, neighbour, rank)
# showing image
print("Rank Filter")
imshow(new_img)
show()
输出 :
Image
Rank Filter