Mahotas 简明教程
Mahotas - Gaussian Filtering
高斯滤波是一种用于模糊或平滑图像的技术。它降低了图像中的噪音并软化了锐利边缘。
Gaussian filtering is a technique used to blur or smoothen an image. It reduces the noise in the image and softens the sharp edges.
将你的图像想象成一个由微小圆点组成的网格,每个圆点代表一个像素。高斯滤波通过获取每个像素并根据周围像素调整其值来发挥作用。
Imagine your image as a grid of tiny dots, and each dot represents a pixel. Gaussian filtering works by taking each pixel and adjusting its value based on the surrounding pixels.
它计算其邻域中像素值的加权平均值,更强调较近的像素,而较不强调较远的像素。
It calculates a weighted average of the pixel values in its neighborhood, placing more emphasis on the closer pixels and less emphasis on the farther ones.
通过对图像中每个像素重复此过程,高斯滤波器通过平滑不同区域之间的急剧过渡并减少噪声来模糊图像。
By repeating this process for every pixel in the image, the Gaussian filter blurs the image by smoothing out the sharp transitions between different areas and reducing the noise.
滤波器的尺寸决定了模糊的程度。较大的滤波器尺寸意味着考虑较宽的区域,从而产生更显着的模糊。
The size of the filter determines the extent of blurring. A larger filter size means a broader region is considered, resulting in more significant blurring.
简单来说,高斯滤波通过对附近像素值求平均并更看重较近像素而不太看重较远像素来使图像看起来更平滑。这有助于减少噪声并使图像不那么清晰。
In simpler terms, Gaussian filtering makes an image look smoother by averaging the nearby pixel values, giving more importance to the closer pixels and less to the ones farther away. This helps to reduce noise and make the image less sharp.
Gaussian Filtering in Mahotas
在 Mahotas 中,我们可以使用 mahotas.gaussian_filter() 函数对图像执行高斯滤波。此函数通过使用称为高斯核的特殊矩阵对图像应用模糊效果。
In Mahotas, we can perform Gaussian filtering on an image using the mahotas.gaussian_filter() function. This function applies a blurring effect to an image by using a special matrix called a Gaussian kernel.
高斯核是一个具有以特定方式排列数字的特殊矩阵。核中的每个数字都表示一个权重。
A Gaussian kernel is a special matrix with numbers arranged in a specific way. Each number in the kernel represents a weight.
核被放置在图像中的每个像素上,并且相邻像素的值乘以它们在核中的相应权重。
The kernel is placed over each pixel in the image, and the values of the neighboring pixels are multiplied by their corresponding weights in the kernel.
然后将乘以的值相加,并将其指定为中心像素的新值。此过程对图像中的每个像素重复进行,从而获得模糊图像,其中清晰的细节和噪声被减少。
The multiplied values are then summed, and assigned as the new value to the central pixel. This process is repeated for every pixel in the image, resulting in a blurred image where the sharp details and noise are reduced.
The mahotas.gaussian_filter() function
mahotas.gaussian_filter() 函数将灰度图像作为输入,并返回图像的模糊版本作为输出。
The mahotas.gaussian_filter() function takes a grayscale image as an input and returns a blurred version of the image as output.
模糊的量由 sigma 值决定。sigma 值越大,应用于输出图像的模糊就越多。
The amount of blurring is determined by the sigma value. The higher the sigma value, more will be the blurring applied to the output image.
以下是 mahotas 中 gaussian_filter() 函数的基本语法 −
Following is the basic syntax of the gaussian_filter() function in mahotas −
mahotas.gaussian_filter(array, sigma, order=0, mode='reflect', cval=0.,
out={np.empty_like(array)})
其中,
Where,
-
array − It is the input image.
-
sigma − It determines the standard deviation of the Gaussian kernel.
-
order (optional) − It specifies the order of the Gaussian filter. Its value can be 0, 1, 2, or 3 (default is 0).
-
mode (optional) − It specifies how the border should be handled (default is 'reflect').
-
cval (optional) − It represents the padding value applied when mode is 'constant'(default is 0).
-
out (optional) − It specifies where to store the output image (default is an array of same size as array).
在以下示例中,我们使用 mh.gaussian_filter() 函数对图像应用高斯滤波。
In the following example, we are applying Gaussian filtering on an image using the mh.gaussian_filter() function.
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()
以下是上面代码的输出: -
Following is the output of the above code −
Filtering with Different Order
我们可以不同阶数对图像执行高斯滤波。高斯滤波中的阶数决定作用于图像上的平滑度(模糊度)。
We can perform Gaussian filtering on an image with different order. Order in Gaussian filtering determines the degree of smoothness (blurring) applied on the image.
阶数值越大,应用于图像上的平滑效果就越大。
Higher the order value, more will be the smoothing effect applied on the image.
处理非常嘈杂的图像时,较高的阶数非常有用。然而,较高的阶数也增加了处理时间,因为滤波器被多次应用。
Higher orders are useful when dealing with very noisy images. However, higher orders also increase the processing time as the filter is applied multiple times.
0 阶应用一次高斯滤波器,1、2 或 3 阶分别应用两次、三次和四次高斯滤波器。
An order of 0 applies Gaussian filter once, an order of 1, 2, or 3 applies Gaussian filter twice, thrice and four times respectively.
在 mahotas 中,要执行具有不同阶数的高斯滤波,我们会将除 0 之外的任何值作为 order 参数传递给 gaussian_filter() 函数。
In mahotas, to perform gaussian filtering with different order, we pass any value other than 0 as the order parameter to the gaussian_filter() function.
Example
在下面提到的示例中,我们对具有不同阶数的图像应用高斯滤波。
In the example mentioned below, we are applying Gaussian filtering on an image with different orders.
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()
上述代码的输出如下:
Output of the above code is as follows −
Filtering with 'Mirror' Mode
在图像中应用滤波时,确定如何处理图像边框非常重要。镜面模式是一种常见的处理方法,它通过在图像边框处镜像图像内容来处理边框像素。
When applying filters to images, it is important to determine how to handle the borders of the image. The mirror mode is a common approach that handles border pixels by mirroring the image content at the borders.
这意味着图像边界外的值是通过镜像图像中最近的像素获得的。这是通过镜像边缘处的现有像素完成的。
This means that the values beyond the image boundaries are obtained by mirroring the nearest pixels within the image. This is done by mirroring the existing pixels along the edges.
这种镜像技术确保了实际图像和镜像图像之间的平滑过渡,从而产生更好的连续性。
This mirroring technique ensures a smooth transition between the actual image and the mirrored image, resulting in better continuity.
Example
在这里,我们对带有“镜像”模式的图像应用高斯滤波。
In here, we are applying Gaussian filtering on an image with 'mirror' mode.
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()
执行上面的代码后,我们得到以下输出: -
After executing the above code, we get the following output −