Mahotas 简明教程
Mahotas - Daubechies Wavelet
小波变换是垂直小波,它指的是可以使用波表示图像的数学函数。
Daubechies wavelets are perpendicular wavelets, which refer to mathematical functions that can represent an image using waves.
小波变换在一个有限区间内仅为非零值,并以最大消失矩数量为特征。
Daubechies wavelets have a non−zero value only within a finite interval and are characterized by a maximum number of vanishing moments.
小波的消失矩是指等于零的矩数量。矩是小波函数乘以 x 的幂后的积分(曲线下的面积)。
Vanishing moments of a wavelet refers to the number of moments that is equal to zero. Moments are integrals (area under a curve) of the wavelet function multiplied by powers of x.
具有更多消失矩的小波更好地表示平滑信号,而具有更少消失矩的小波更好地表示具有不连续性的信号。
A wavelet with more vanishing moments better represents smooth signals, while a wavelet with fewer vanishing moments better represents signals with discontinuities.
Daubechies Wavelet in Mahotas
在 Mahotas 中,我们可以使用 mahotas.daubechies() 函数对图像应用小波变换。
In Mahotas, we can apply Daubechies wavelet transformation on an image using the mahotas.daubechies() function.
它支持 D2 到 D20 的多个小波变换,其中整数表示小波中的消失矩数量。
It supports multiple Daubechies wavelets ranging from D2 to D20, where the integer represents the number of vanishing moments in a wavelet.
变换包括将图像分解为低频(平滑特征)和高频系数(详细信息特征)。这使得可以独立分析图像的不同频率。
The transformations involve breaking the image into low−frequency (smooth features) and high−frequency coefficients (detailed features). This allows different frequencies of the image to be analyzed independently.
The mahotas.daubechies() function
mahotas.daubechies() 函数以灰度图像作为输入,并返回小波系数作为一张新图像。
The mahotas.daubechies() function takes a grayscale image as input, and returns the wavelet coefficients as a new image.
小波系数是对应于图像平滑和详细信息特征的数组元组。
The wavelet coefficients are a tuple of arrays that correspond to the smooth and the detailed features of the image.
以下是 mahotas 中 daubechies() 函数的基本语法:
Following is the basic syntax of the daubechies() function in mahotas −
mahotas.daubechies(f, code, inline=False)
其中,
Where,
-
f − It is the input image.
-
code − It specifies the type of wavelet to use, can be 'D2', 'D4',….., 'D20'.
-
inline (optional) − It specifies whether to return a new image or modify input image (default is False).
在以下示例中,我们使用 mh.daubechies() 函数对图像应用小波变换。
In the following example, we are applying Daubechies wavelet transformation on an image using the mh.daubechies() function.
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)
# Applying Daubechies transformation
daubechies_transform = mh.daubechies(image, 'D20')
# 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 Daubechies transformed image
axes[1].imshow(daubechies_transform, cmap='gray')
axes[1].set_title('Daubechies Transformed 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 −
Multiple Daubechies Wavelets
另一种应用小波变换的方法是使用多个小波变换。多个小波变换指的是具有一组不同消失矩的波变换。
Another way of applying Daubechies wavelet transformation is by using multiple Daubechies wavelets. Multiple Daubechies wavelets refers to a set of wavelets having different vanishing moments.
在 mahotas 中,若要应用多个小波变换,我们首先创建一个不同小波的列表。然后,我们遍历列表中的每个小波。
In mahotas, to apply multiple Daubechies wavelets we first create a list of different wavelets. Then, we traverse over each wavelet in the list.
最后,我们使用 mh.daubechies() 函数将不同的小波应用于输入图像。
Finally, we apply the different wavelets to the input image using the mh.daubechies() function.
例如,假设我们有一个包含三个小波的列表——D6、D12 和 D18。三个小波将分别具有 6、12 和 18 个消失矩。
For example, let’s say we have a list of three wavelets − D6, D12, and D18. The three wavelets will have 6, 12, and 18 vanishing moments respectively.
因此,将生成三个输出图像,每个图像都应用了不同的 Daubechies 小波。
Hence, three output images will be generated, each having a different Daubechies wavelet applied to it.
Example
在下面提到的示例中,我们正在对图像应用多个小波变换。
In the example mentioned below, we are applying multiple Daubechies wavelets transformations on an image.
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)
# Creating list of multiple Daubechies wavelets
daubechies_wavelets = ['D6', 'D12', 'D18']
# Creating subplots to display images for each Daubechies wavelet
fig, axes = mtplt.subplots(1, len(daubechies_wavelets) + 1)
axes[0].imshow(image, cmap='gray')
axes[0].set_title('Original Image')
axes[0].set_axis_off()
# Applying Daubechies transformation for each Daubechies wavelet
for i, daubechies in enumerate(daubechies_wavelets):
daubechies_transform = mh.daubechies(image, daubechies)
axes[i + 1].imshow(daubechies_transform, cmap='gray')
axes[i + 1].set_title(f'Wavelet={daubechies}')
axes[i + 1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()
上述代码的输出如下:
Output of the above code is as follows −
Daubechies Wavelet on a Random Image
我们还可以通过在二位的随机图像中使用小波变换,对小波进行 Daubechies 变换。
We can also perform Daubechies transformation by using Daubechies wavelets on a random image of two dimension.
随机二位图像指的是每像素有随机强度值的图像。强度值范围从 0(黑色)至 255(白色)。
A random two−dimensional image refers to an image having a random intensity value for each pixel. The intensity value can range from 0 (black) to 255 (white).
在 mahotas 中,要在随机图像上执行小波 Daubechies 变换,我们首先指定二位图像的维度(长度和宽度)。
In mahotas, to perform Daubechies wavelet transformation on a random image, we first specify the dimensions (length and width) of the 2−D image.
然后,我们将这些维度与强度范围(0 到 255)传递到 np.random.randint() 函数,以创建随机图像。由于未指定通道值,所以创建的图像为灰度图像。
Then, we pass these dimension along with the intensity range (0 to 255) to np.random.randint() function to create the random image. Since the channel value is not specified, the created image is a grayscale image.
之后,我们应用 Daubechies 小波变换,通过指定我们想要使用的小波。
After that, we apply Daubechies wavelet transformation by specifying the wavelet we want to use.
Example
在此,我们对随机生成的二位图像应用 Daubechies 小波变换。
In here, we are applying Daubechies wavelet transformation on a randomly generated two−dimensional image.
import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Specifying the dimensions
length, width = 1000, 1000
# Creating a random two-dimensional image
image = np.random.randint(0, 256, (length, width))
# Applying Daubechies transformation
daubechies_transform = mh.daubechies(image, 'D2')
# 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 Daubechies transformed image
axes[1].imshow(daubechies_transform, cmap='gray')
axes[1].set_title('Daubechies Transformed 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 −