Mahotas 简明教程

Mahotas - Haar Transform

Haar 变换是一种将图像从像素强度值转换为小波系数的技术。小波系数是表示不同频率对图像贡献的数值。

Haar transform is a technique used to convert an image from pixel intensity values to wavelet coefficients. Wavelet coefficients are numerical values representing the contribution of different frequencies to an image.

在 Haar 变换中,图像被分解为一组正交标准正交基函数,称为 Haar 小波。

In Haar transform, an image is broken into a set of orthonormal basis functions called Haar wavelets.

正交标准正交基函数指的是满足两个重要性质的数学函数:它与其他基函数垂直(或正交),并且其系数长度为 1。

An orthonormal basis function refers to a mathematical function that satisfies two important properties : it is perpendicular (or orthogonal) to other basis functions, and its coefficients have a length of 1.

基函数是由单个小波通过缩放和平移生成的。缩放是指改变小波函数的持续时间,而平移涉及沿 x 轴移动小波函数。

The basis functions are generated from a single wavelet by scaling and shifting. Scaling refers to changing the duration of the wavelet function, while shifting involves moving the wavelet function along the x−axis.

Haar Transform in Mahotas

在 Mahotas 中,我们可以通过对图像使用 mahotas.haar() 函数来执行 Haar 变换。以下是在图像上执行 Haar 变换的基本方法 −

In Mahotas, we can perform Haar transformation by using the mahotas.haar() function on an image. Following is the basic approach to perform Haar transformation on an image −

  1. Image Partitioning − The first step involves dividing the input image into nonoverlapping blocks of equal size.

  2. Averaging and Differencing − Next, the low and high frequency coefficients are computed within each block. The low frequency coefficient represents the smooth, global features of the image and is calculated as the average of pixel intensities. The high frequency coefficient represents the sharp, local features of the image and is calculated by finding differences between neighboring pixels.

  3. Subsampling − The resultant low and high frequency coefficients are then down sampled (degraded) by discarding alternate values in each row and column.

重复步骤 2 和 3,直到变换整个图片。

Steps 2 and 3 are repeated until are repeated until the entire image has been transformed.

The mahotas.haar() function

mahotas.haar() 函数将灰度图像作为输入,并返回图像的 wavelet 系数。wavelet 系数为一个数组元组。

The mahotas.haar() function takes a grayscale image as input and returns the wavelet coefficients as an image. The wavelet coefficients are a tuple of arrays.

第一个数组包含低频系数,而第二个数组包含高频系数。

The first array contains the low−frequency coefficients, and the second array contains the high−frequency coefficients.

以下是 mahotas 中 haar() 函数的基本语法 −

Following is the basic syntax of the haar() function in mahotas −

mahotas.haar(f, preserve_energy=True, inline=False)

其中,

Where,

  1. f − It is the input image.

  2. preserve_energy (optional) − It specifies whether to preserve the energy of the output image (default is True).

  3. inline (optional) − It specifies whether to return a new image or modify input image (default is False).

在以下示例中,我们使用 mh.haar() 函数对图像进行 Haar 变换。

In the following example, we are applying Haar transformation on an image using the mh.haar() function.

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 Haar transformation
haar_transform = mh.haar(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 Haar transformed image
axes[1].imshow(haar_transform, cmap='gray')
axes[1].set_title('Haar 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 −

haar transform

Without Preserving Energy

我们也可以对图像执行 Haar 变换而不保留能量。图像的能量是指亮度,当图像从一个域变换到另一个域时,其亮度可能会改变。

We can also perform Haar transformation on an image without preserving energy. The energy of the image refers to its brightness, and it can change when an image is being transformed from one domain to another.

在 mahotas 中,mh.haar() 函数的 preserve_energy 参数决定是否保留输出图像的能量。如果我们不想保留能量,我们可以将此参数设置为 False。

In mahotas, the preserve_energy parameter of the mh.haar() function determines whether to preserve the energy of the output image. If we don’t want to preserve the energy, we can set this parameter to False.

因此,输出图像的亮度将不同于输入图像的亮度。

Hence, the brightness of the output image will be different from the brightness of the input image.

如果此参数设置为 True,则输出图像和输入图像将具有相同的亮度。

If this parameter is set to True, then the output image and the input image will have the same brightness.

Example

在下面提到的示例中,我们正在对图像执行 Haar 变换而不保留其能量。

In the example mentioned below, we are performing Haar transformation on an image without preserving its energy.

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 Haar transformation
haar_transform = mh.haar(image, preserve_energy=False)
# 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 Haar transformed image
axes[1].imshow(haar_transform, cmap='gray')
axes[1].set_title('Haar Transformed 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 −

preserving energy

Inline Haar Transformation

我们也可以对输入图像执行内联 Haar 变换。内联是指在不创建新图像的情况下对原始图像本身应用变换。

We can also perform inline Haar transformation on an input image. Inline refers to applying transformation on the original image itself without creating a new image.

当对图像应用变换时,这让我们能够节省空间。

This allows us to save space when applying transformations on an image.

在 mahotas 中,可以在 mh.haar() 函数中将 inline 参数设置为布尔值 True 来实现内联 Haar 变换。这样就无需创建新图像来存储输出。

In mahotas, inline Haar transformation can be achieved by setting the inline parameter to boolean value True in the mh.haar() function. This way, a new image need not be created to store the output.

Example

这里,我们正在对输入图像执行内联 Haar 变换。

In here, we are performing inline Haar transformation on an input 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)
# Applying Haar transformation
mh.haar(image, preserve_energy=False, inline=True)
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 1)
# Displaying the transformed image
axes.imshow(image, cmap='gray')
axes.set_title('Haar Transformed Image')
axes.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 −

inline haar transformation

Note − 由于输入图像在变换过程中被覆盖,因此输出屏幕将只包含一幅图像,如上所示。

Note − Since the input image is getting overridden during transformation, the output screen will only contain a single image as seen above.