Mahotas 简明教程

Mahotas - Haar Transform

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

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

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

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

Haar Transform in Mahotas

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

  1. Image Partitioning −第一步涉及将输入图像分成相等尺寸的不重叠块。

  2. Averaging and Differencing − 接下来,在每个块中计算低频系数和高频系数。低频系数代表图像光滑的全局特征,计算方法是计算像素强度的平均值。高频系数代表图像锐利的局部特征,计算方法是在邻近像素之间寻找差异。

  3. Subsampling − 然后,通过丢弃所有行和列中的值,对生成的低频系数和高频系数进行下采样(降级)。

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

The mahotas.haar() function

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

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

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

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

其中,

  1. f − 这是输入图像。

  2. preserve_energy (optional) − 指定是否保留输出图像的能量(默认为 True)。

  3. inline (optional) − 指定返回新图像还是修改输入图像(默认为 False)。

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

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()

以下是上面代码的输出: -

haar transform

Without Preserving Energy

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

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

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

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

Example

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

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()

上述代码的输出如下:

preserving energy

Inline Haar Transformation

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

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

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

Example

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

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()

执行上面的代码后,我们得到以下输出: -

inline haar transformation

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