Mahotas 简明教程
Mahotas - Gaussian Filtering
高斯滤波是一种用于模糊或平滑图像的技术。它降低了图像中的噪音并软化了锐利边缘。
将你的图像想象成一个由微小圆点组成的网格,每个圆点代表一个像素。高斯滤波通过获取每个像素并根据周围像素调整其值来发挥作用。
它计算其邻域中像素值的加权平均值,更强调较近的像素,而较不强调较远的像素。
通过对图像中每个像素重复此过程,高斯滤波器通过平滑不同区域之间的急剧过渡并减少噪声来模糊图像。
滤波器的尺寸决定了模糊的程度。较大的滤波器尺寸意味着考虑较宽的区域,从而产生更显着的模糊。
简单来说,高斯滤波通过对附近像素值求平均并更看重较近像素而不太看重较远像素来使图像看起来更平滑。这有助于减少噪声并使图像不那么清晰。
Gaussian Filtering in Mahotas
在 Mahotas 中,我们可以使用 mahotas.gaussian_filter() 函数对图像执行高斯滤波。此函数通过使用称为高斯核的特殊矩阵对图像应用模糊效果。
高斯核是一个具有以特定方式排列数字的特殊矩阵。核中的每个数字都表示一个权重。
核被放置在图像中的每个像素上,并且相邻像素的值乘以它们在核中的相应权重。
然后将乘以的值相加,并将其指定为中心像素的新值。此过程对图像中的每个像素重复进行,从而获得模糊图像,其中清晰的细节和噪声被减少。
The mahotas.gaussian_filter() function
mahotas.gaussian_filter() 函数将灰度图像作为输入,并返回图像的模糊版本作为输出。
模糊的量由 sigma 值决定。sigma 值越大,应用于输出图像的模糊就越多。
以下是 mahotas 中 gaussian_filter() 函数的基本语法 −
mahotas.gaussian_filter(array, sigma, order=0, mode='reflect', cval=0.,
out={np.empty_like(array)})
其中,
-
array − 这是输入图像。
-
sigma − 它确定高斯核的标准差。
-
order (optional) − 它指定高斯滤波器的阶数。其值可以是 0、1、2 或 3(默认值为 0)。
-
mode (optional) − 它指定如何处理边框(默认值为“reflect”)。
-
cval (optional) − 它表示当模式为“constant”时应用的填充值(默认值为 0)。
-
out (optional) − 它指定存储输出图像的位置(默认值为与数组相同大小的数组)。
在以下示例中,我们使用 mh.gaussian_filter() 函数对图像应用高斯滤波。
import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image = mh.imread('tree.tiff')
# Converting it to grayscale
image = mh.colors.rgb2gray(image)
# Applying gaussian filtering
gauss_filter = mh.gaussian_filter(image, 4)
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 2)
# Displaying the original image
axes[0].imshow(image)
axes[0].set_title('Original Image')
axes[0].set_axis_off()
# Displaying the gaussian filtered image
axes[1].imshow(gauss_filter)
axes[1].set_title('Gaussian Filtered Image')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()
以下是上面代码的输出: -
Filtering with Different Order
我们可以不同阶数对图像执行高斯滤波。高斯滤波中的阶数决定作用于图像上的平滑度(模糊度)。
阶数值越大,应用于图像上的平滑效果就越大。
处理非常嘈杂的图像时,较高的阶数非常有用。然而,较高的阶数也增加了处理时间,因为滤波器被多次应用。
0 阶应用一次高斯滤波器,1、2 或 3 阶分别应用两次、三次和四次高斯滤波器。
在 mahotas 中,要执行具有不同阶数的高斯滤波,我们会将除 0 之外的任何值作为 order 参数传递给 gaussian_filter() 函数。
Example
在下面提到的示例中,我们对具有不同阶数的图像应用高斯滤波。
import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image = mh.imread('sea.bmp')
# Converting it to grayscale
image = mh.colors.rgb2gray(image)
# Applying gaussian filtering
gauss_filter = mh.gaussian_filter(image, 3, 1)
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 2)
# Displaying the original image
axes[0].imshow(image)
axes[0].set_title('Original Image')
axes[0].set_axis_off()
# Displaying the gaussian filtered image
axes[1].imshow(gauss_filter)
axes[1].set_title('Gaussian Filtered Image')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()
上述代码的输出如下:
Filtering with 'Mirror' Mode
在图像中应用滤波时,确定如何处理图像边框非常重要。镜面模式是一种常见的处理方法,它通过在图像边框处镜像图像内容来处理边框像素。
这意味着图像边界外的值是通过镜像图像中最近的像素获得的。这是通过镜像边缘处的现有像素完成的。
这种镜像技术确保了实际图像和镜像图像之间的平滑过渡,从而产生更好的连续性。
Example
在这里,我们对带有“镜像”模式的图像应用高斯滤波。
import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image = mh.imread('sun.png')
# Converting it to grayscale
image = mh.colors.rgb2gray(image)
# Applying gaussian filtering
gauss_filter = mh.gaussian_filter(image, 3, 0, mode='mirror')
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 2)
# Displaying the original image
axes[0].imshow(image)
axes[0].set_title('Original Image')
axes[0].set_axis_off()
# Displaying the gaussian filtered image
axes[1].imshow(gauss_filter)
axes[1].set_title('Gaussian Filtered Image')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()
执行上面的代码后,我们得到以下输出: -