python 离散小波变换函数的相关应用

import cv2
from matplotlib import pyplot as plt
import matplotlib.image as mpimg
import matplotlib as mpl
#mpl.rcParams['figure.dpi'] = 300
import pywt
import pywt.data
import numpy as np
from math import sqrt
from PIL import Image, ImageChops, ImageOps
import pickle


#加载图像
img = cv2.imread('img/peter-3794597_1280.jpeg')
image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# plt.imshow(image,cmap='gray');
# plt.show()

#小波变换
c = pywt.dwt2(image,wavelet)
cA, (cH, cV, cD)= c

#显示cA cH cV cD
titles = ['cA', 'cH', 'cV', 'cD']
fig = plt.figure(figsize=(12, 3))
for i, a in enumerate([cA, cH, cV, cD]):
  ax = fig.add_subplot(1, 4, i + 1)
  ax.imshow(abs(a), interpolation="nearest", cmap = 'gray')
  ax.set_title(titles[i], fontsize=20)
fig.tight_layout()
plt.show()
#逆变换
w, h = image.shape  
c_inverse = pywt.idwt2((c),wavelet)[:h,:w]
titles = ['原图像', '恢复后图像']
fig = plt.figure(figsize=(10, 6))
for i, a in enumerate([image,c_inverse]):
  ax = fig.add_subplot(1, 2, i + 1)
  ax.imshow(a, interpolation="nearest", cmap = 'gray')
  ax.set_title(titles[i], fontsize=15)
fig.tight_layout()
plt.show()

#确定像素值值位零的点的个数
print((sum(sum(sum(c[1][m][n] == 0) for n in range(len(c[1][0]))) for m in range(len(c[1])))) + sum(sum(c[0][m] == 0) for m in range(len(c[0]))))
#设定阀值
threshold = 10
#阀值以下灰度值去零
h1 = cA.shape[0] 
for i in range(h1):
      c[0][i][np.abs(c[0][i])<threshold] = 0.0 
      for j in range(len(c[1])):
         c[1][j][i][np.abs(c[1][j][i])<threshold] = 0.0 
print('零点 =',(sum(sum(sum(c[1][m][n] == 0) for n in range(h1)) for m in range(len(c[1])))) + sum(sum(c[0][m] == 0) for m in range(h1)))



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

фора 快跑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值