Mahotas 简明教程

Mahotas - Reversing Haar Transform

反转 Haar 变换是指从 Haar 变换的图像重构原始图像的技术。在了解反转 Haar 变换之前,让我们了解一下 Haar transformation

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

在 Haar 变换中,图像被分解成一组称为 Haar 小波的 orthonormal basis functions

反转 Haar 变换通过以特定方式组合 Haar 小波(如下所述)将小波系数转换成像素强度值。

Reversing Haar Transform in Mahotas

在 Mahotas 中,我们可以使用 mahotas.ihaar() 函数执行反转 Haar 变换。以下是如何执行反转 Haar 变换的基本方法 −

  1. 首先,从 Haar 变换获取 Haar 小波系数。

  2. 接下来,将每个系数乘以一个比例因子和小波。对于 Haar 小波,比例因子通常对于近似系数为 $\mathrm{1/\sqrt{2}}$,对于细节系数为 1。

  3. 然后,对高频(细节)系数和低频(近似)系数求和。

  4. 最后,组合重构的系数,如果像素值不在 0 到 255 的范围内,则执行归一化。

一旦完成这些步骤,原始图像就会从 Haar 变换的图像中重构。

The mahotas.ihaar() function

mahotas.ihaar() 函数将 Haar 变换的图像作为输入,并返回原始的灰度图像作为输出。

反向图像与原始图像完全重建,因为 Haar 变换是一个可逆的过程。

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

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

其中,

  1. f − 这是输入图像。

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

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

在以下示例中,我们使用 mh.ihaar() 函数来反转 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
haar_transform = mh.haar(image)
# Reversing Haar transformation
reverse_haar = mh.ihaar(haar_transform)
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 3)
# 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()
# Displaying the reversed image
axes[2].imshow(reverse_haar, cmap='gray')
axes[2].set_title('Reverse Haar Image')
axes[2].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()

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

reversing haar transform

Without Preserving Energy

我们还可以反转 Haar 变换对图像的影响而无需保留其能量。图像的能量是指其亮度,在变换图像时它可能会发生变化。

在 mahotas 中,我们可以在 mh.ihaar() 函数中将 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)
# Reversing Haar transformation
reverse_haar = mh.ihaar(haar_transform, preserve_energy=False)
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 3)
# 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()
# Displaying the reversed image
axes[2].imshow(reverse_haar, cmap='gray')
axes[2].set_title('Reverse Haar Image')
axes[2].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()

上述代码的输出如下:

preserving energy mahotas

Inline Reverse Haar Transformation

反转 Haar 变换的另一种方法是执行内联反向 Haar 变换。内联是指对原始图像本身应用变换而不创建新图像,从而在变换过程中节省空间。

在 mahotas 中,通过将 mh.ihaar() 函数中的 inline 参数设置为布尔值 'True',可以实现内联反向 Haar 变换。

Example

在这里,我们对 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)
# Reversing Haar transformation
mh.ihaar(mh.haar(image), inline=True)
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 1)
# Displaying the reversed image
axes.imshow(image, cmap='gray')
axes.set_title('Reverse Haar Image')
axes.set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()

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

preserving energy mahotas1