Mahotas 简明教程
Mahotas - Making Image Wavelet Center
图像小波中心化是指将图像的小波系数移至小波中心,小波中心是小波达到最大幅值的一个点。小波系数是表示图像中不同频率贡献的数值。
Image wavelet centering refers to shifting the wavelet coefficients of an image to the wavelet center, a point at which the wavelet reaches its maximum amplitude. Wavelet coefficients are numerical values representing the contribution of different frequencies to an image.
通过使用小波变换将图像分解为单个波,可以获得小波系数。通过对系数进行中心化,低频和高频可以与中心频率对齐,从而去除图像噪声。
Wavelet coefficients are obtained by breaking an image into individual waves using a wavelet transformation. By centering the coefficients, the low and high frequencies can be aligned with the central frequencies to remove noise from an image.
Making Image Wavelet Center in Mahotas
在 Mahotas 中,我们可以使用 mahotas.wavelet_center() 函数创建图像小波中心来减少噪声。此函数执行两个主要步骤来使图像小波中心化,如下所示:
In Mahotas, we can use the mahotas.wavelet_center() function to make an image wavelet centered to reduce noise. The function performs two major steps to make the image wavelet centered, they are as follows −
-
First it decomposes the signals of the original image into wavelet coefficients.
-
Next, it takes the approximation coefficients, which are coefficients with low frequencies, and aligns them with central frequencies.
通过对齐频率,去除图像的平均强度,从而去除噪声。
By doing the alignment of frequencies, the average intensity of the image is removed, hence removing noise.
The mahotas.wavelet_center() function
mahotas.wavelet_center() 函数以图像为输入,并返回一个新的图像,其中小波中心位于原点。
The mahotas.wavelet_center() function takes an image as input, and returns a new image with the wavelet center at the origin.
它使用小波变换对原始输入图像进行分解(分解),然后将小波系数移至频谱中心的中心。
It decomposes (breaks−down) the original input image using a wavelet transformation and then shifts the wavelet coefficients to the center of the frequency spectrum.
在查找图像小波中心时,此函数会忽略指定像素大小的边界区域。
The function ignores a border region of the specified pixel size when finding the image wavelet center.
以下是 Mahotas 中 wavelet_center() 函数的基本语法:
Following is the basic syntax of the wavelet_center() function in mahotas −
mahotas.wavelet_center(f, border=0, dtype=float, cval=0.0)
其中,
where,
-
f − It is the input image.
-
border (optional) − It is the size of the border area (default is 0 or no border).
-
dtype (optional) − It is the data type of the returned image (default is float).
-
cval (optional) − It is the value used to fill the border area (default is 0).
在以下示例中,我们使用 mh.wavelet_center() 函数制作图像小波中心。
In the following example, we are making an image wavelet centered using the mh.wavelet_center() function.
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)
# Centering the image
centered_image = mh.wavelet_center(image)
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 2)
# Displaying the original image
axes[0].imshow(image, cmap='gray')
axes[0].set_title('Original Image')
axes[0].set_axis_off()
# Displaying the centered image
axes[1].imshow(centered_image, cmap='gray')
axes[1].set_title('Centered 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 −
Centering using a Border
我们可以使用边界执行图像小波中心,以操作输出图像。边界区域是指图像中围绕对象的一个区域。它将对象与背景区域或相邻对象分隔开。
We can perform image wavelet centering using a border to manipulate the output image. A border area refers to a region surrounding an object in the image. It separates the object from background region or neighboring objects.
在mahotas中,我们可以通过将像素值设置为零来定义一个在小波中心时不应该考虑的区域。这是通过将一个值传递给 mahotas.wavelet_center() 函数的 border 参数来完成的。
In mahotas, we can define an area that should not be considered when doing wavelet centering by setting the pixel values to zero. This is done by passing a value to the border parameter of the mahotas.wavelet_center() function.
该函数在执行图像小波中心时忽略参数中指定的像素数。例如,如果 border 参数设置为 500,则在对图像小波进行中心化时将忽略所有侧面的 500 个像素。
The function ignores as many pixels as specified in the parameter when doing image wavelet centering. For example, if border parameter is set to 500, then 500 pixels on all sides will be ignored when centering the image wavelet.
Example
在下面提到的示例中,我们在对图像小波进行中心化时忽略了一定大小的边界。
In the example mentioned below, we are ignoring a border of certain size when centering an image wavelet.
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)
# Centering the image with border
centered_image = mh.wavelet_center(image, border=500)
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 2)
# Displaying the original image
axes[0].imshow(image, cmap='gray')
axes[0].set_title('Original Image')
axes[0].set_axis_off()
# Displaying the centered image
axes[1].imshow(centered_image, cmap='gray')
axes[1].set_title('Centered 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 −
Centering by applying Padding
我们还可以通过对边界区域填充一定色度的灰色来进行中心化。
We can also do centering by applying padding to fill the border area with a shade of gray.
填充是指在图像边缘周围添加额外的像素值以创建边界的技术。
Padding refers to the technique of adding extra pixel values around the edges of an image to create a border.
在mahotas中,可以通过将一个值指定给 mahotas.wavelet_center() 函数的 cval 参数来应用填充。它允许我们用一种颜色的边界区域进行填充,取值范围从 0(黑色)到 255(白色)。
In mahotas, padding can be applied by specifying a value to the cval parameter of the mahotas.wavelet_center() function. It allows us to fill the border region with a color, with the value ranging from 0 (black) to 255 (white).
Note − 只有在存在边界区域时才能应用填充。因此,值或 border 参数不应为 0。
Note − Padding can only be applied if a border area is present. Hence, the value or border parameter should not be 0.
Example
在这里,我们在中心化图像小波时忽略了一个特定像素大小的边界并应用填充。
In here, we are ignoring a border of specific pixel size and applying padding to center an image wavelet.
import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image = mh.imread('nature.jpeg')
# Converting it to grayscale
image = mh.colors.rgb2gray(image)
# Centering the image with border
centered_image = mh.wavelet_center(image, border=100, cval=109)
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 2)
# Displaying the original image
axes[0].imshow(image, cmap='gray')
axes[0].set_title('Original Image')
axes[0].set_axis_off()
# Displaying the centered image
axes[1].imshow(centered_image, cmap='gray')
axes[1].set_title('Centered 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 −