Mahotas 简明教程
Mahotas - Reversing Haar Transform
反转 Haar 变换是指从 Haar 变换的图像重构原始图像的技术。在了解反转 Haar 变换之前,让我们了解一下 Haar transformation 。
Reversing Haar transform refers to the technique of reconstructing the original image from a Haar transformed image. Before understanding reverse Haar transformation, let us learn about Haar transformation.
Haar 变换是一种将图像从像素强度值转换成小波系数(表示图像不同频率的值)的技术。
Haar transformation is a technique that converts an image from pixel intensity values to wavelet coefficients (values that represent different frequencies of an image).
在 Haar 变换中,图像被分解成一组称为 Haar 小波的 orthonormal basis functions 。
In Haar transformation, the image is broken into a set of orthonormal basis functions called Haar wavelets.
反转 Haar 变换通过以特定方式组合 Haar 小波(如下所述)将小波系数转换成像素强度值。
Reversing Haar transformation converts the wavelet coefficients into pixel intensity values by combining the Haar wavelets in a specific manner (as discussed below).
Reversing Haar Transform in Mahotas
在 Mahotas 中,我们可以使用 mahotas.ihaar() 函数执行反转 Haar 变换。以下是如何执行反转 Haar 变换的基本方法 −
In Mahotas, we can perform reverse Haar transformation by using the mahotas.ihaar() function. Following is the basic approach to perform the inverse Haar transformation −
-
First, obtain the Haar wavelet coefficients from Haar transformation.
-
Next, multiply each coefficient by a scaling factor and the Haar wavelet. For the Haar wavelet, the scaling factor is usually $\mathrm{1/\sqrt{2}}$ for the approximation coefficients and 1 for the detail coefficients.
-
Then, sum up these scaled coefficients for both the high frequency (detail) and the low frequency (approximation) coefficients.
-
Finally, combine the reconstructed coefficients and perform normalization if the pixel values are not within the range of 0 to 255.
一旦完成这些步骤,原始图像就会从 Haar 变换的图像中重构。
Once these steps are completed, the original image is reconstructed from the Haar transformed image.
The mahotas.ihaar() function
mahotas.ihaar() 函数将 Haar 变换的图像作为输入,并返回原始的灰度图像作为输出。
The mahotas.ihaar() function takes a Haar transformed image as an input and returns the original grayscale image as output.
反向图像与原始图像完全重建,因为 Haar 变换是一个可逆的过程。
The reverse image is a perfect reconstruction of the original image since Haar transformation is a reversible process.
以下是 mahotas 中 ihaar() 函数的基本语法 −
Following is the basic syntax of the ihaar() function in mahotas −
mahotas.ihaar(f, preserve_energy=True, inline=False)
其中,
Where,
-
f − It is the input image.
-
preserve_energy (optional) − It specifies whether to preserve the energy of the output image (default is True).
-
inline (optional) − It specifies whether to return a new image or modify the input image (default is False).
在以下示例中,我们使用 mh.ihaar() 函数来反转 Haar 变换对图像的影响。
In the following example, we are using the mh.ihaar() function to reverse the effect of Haar transformation 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)
# 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()
以下是上面代码的输出: -
Following is the output of the above code −
Without Preserving Energy
我们还可以反转 Haar 变换对图像的影响而无需保留其能量。图像的能量是指其亮度,在变换图像时它可能会发生变化。
We can also reverse the effects of Haar transformation on an image without preserving its energy. The energy of the image refers to its brightness and it can change when an image is being transformed.
在 mahotas 中,我们可以在 mh.ihaar() 函数中将 preserve_energy 参数设置为 'False' 以防止能量保留。因此,输出图像的亮度将与原始输入图像不同。
In mahotas, we can set the preserve_energy parameter to 'False' in the mh.ihaar() function to prevent the energy preservation. Hence, the brightness of the output image will be different from the original 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 reverse 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)
# 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()
上述代码的输出如下:
Output of the above code is as follows −
Inline Reverse Haar Transformation
反转 Haar 变换的另一种方法是执行内联反向 Haar 变换。内联是指对原始图像本身应用变换而不创建新图像,从而在变换过程中节省空间。
Another way we can reverse Haar transformation is by performing inline reverse Haar transformation. Inline refers to applying the transformation on the original image itself without creating a new image, thus saving space during transformation.
在 mahotas 中,通过将 mh.ihaar() 函数中的 inline 参数设置为布尔值 'True',可以实现内联反向 Haar 变换。
In mahotas, inline reverse Haar transformation can be achieved by setting the inline parameter to the boolean value 'True' in the mh.ihaar() function.
Example
在这里,我们对 Haar 变换图像执行内联反向 Haar 变换。
Here, we are performing inline reverse Haar transformation on a Haar transformed image.
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()
执行上面的代码后,我们得到以下输出: -
After executing the above code, we get the following output −