1.1.用途:
# 无损压缩图片-小波变换,在保留大部分信号的前提下使大多数值均为0(否则较小)
2.实例:
实例1:无损压缩图片
import numpy as np ,mahotas as mh
from matplotlib import pyplot as plt
image = mh.demos.load('luispedro', as_grey=True) #加载图片-原始图像中没有零
image = image[:256,:256]
print("1.图像中像素为0所占比例: {0}".format(np.mean(image==0)))#0.0
# plt.imshow(image,cmap='gray')
# plt.show()
# 1.基线压缩方法:每隔一个像素保存一次,仅保存高位
img1 = image[::2,::2].copy()
img1 /= 8;img1 = img1.astype(np.uint8) #丢弃低位
print("2.图像中像素为0所占比例: {0}".format(np.mean(img1==0)))#0.000732421875
plt.imshow(img1,cmap='gray') #图像上有小方块
plt.show()
#2.1.使用D8小波变换以获得变换后的图像img2:丢弃高阶位
img2 = mh.daubechies(image,'D8')
img2 /= 8 ;img2 = img2.astype(np.int8) #丢弃低位
print("3.图像中像素为0所占比例: {0}".format(np.mean(img2==0)))#0.5917205810546875
plt.imshow(img2,cmap='gray')
plt.show()
#2.2.小波反变换 显示图片质量:在图片质量没有太多损失的情况下是一个相当不错的降低
img3 = mh.idaubechies(img2, 'D8')
plt.imshow(img3,cmap='gray')
plt.show()
# 3.继续压缩图片到77%(其余值都较小)非常好地压缩为无损图像,且可在传输后重建完整图像
threshold = mh.thresholding.soft_threshold(img2, 12)
print("4.图像中像素为0所占比例: {0}".format(np.mean(threshold==0)))#0.7700042724609375
img4 = mh.idaubechies(threshold, 'D8')#小波反变换
plt.imshow(img4,cmap='gray')
plt.show()
实例2:边界Borders
import numpy as np ,mahotas as mh
from matplotlib import pyplot as plt
image = mh.demos.load('luispedro', as_grey=True) #加载图片-原始图像中没有零
image = image[:256,:256]
plt.title('image');plt.imshow(image,cmap='gray');plt.show()
# 可在边框处看到一些工件。可用wavelet_center,wavelet_decenter处理边框:
img1 = mh.wavelet_center(image)
plt.title('image1');plt.imshow(img1,cmap='gray');plt.show()
img2 = mh.daubechies(img1, 'D8')
img3 = mh.idaubechies(img1, 'D8')
plt.title('image3');plt.imshow(img3,cmap='gray');plt.show()
img4 = mh.wavelet_decenter(img3, img1.shape)# 现在,img4等于(不舍入)img1没有任何边框效果
plt.title('image4');plt.imshow(img4,cmap='gray');plt.show()
3.函数:
mh.haar(image, preserve_energy=True, inline=False)# 哈尔变换Haar transform
参数:
image:二维数组-输入图像
reserve_energy:bool,可选-是否标准化结果以便保留能量energy is preserved(默认值)
inline : bool, optional是否将结果写入输入图像。默认返回新图像。整数图像始终会转换为浮点并进行复制
mh.ihaar(image, preserve_energy=True, inline=False)# 逆哈尔变换
参数:
image:二维数组-输入图像。如果它是整数图像,则将其转换为浮点数(双精度)
reserve_energy:bool,可选-是否标准化结果以便保留能量(默认值)
inline : bool, optional是否将结果写入输入图像。默认返回新图像。整数图像始终会转换为浮点并进行复制
返回值:image:ndarray
mh.daubechies(image,code,inline = False )# Daubechies小波变换-如图像大小是2的幂最有效
参数:
image:ndarray二维图像
code : str “ D2”,“ D4”,...“ D20”之一
inline : bool, optional是否将结果写入输入图像。默认返回新图像。整数图像始终会转换为浮点并进行复制
mh.idaubechies(image, code, inline=False) # Daubechies小波逆变换
参数:
image:ndarray二维图像
code : str“ D2”,“ D4”,...“ D20”之一
inline : bool, optional是否将结果写入输入图像。默认返回新图像。整数图像始终会转换为浮点并进行复制