数字图像中,离散小波变换在python中的使用
目录
一、各种DWT小波变换含义
二、查看DWT参数
三、具体代码
python 的 pywt库中wavedec函数的wavelet参数取值:
'db1' ~ 'db45': Daubechies小波
'sym1' ~ 'sym20': Symlets小波
'coif1' ~ 'coif5': Coiflets小波
'bior1.1' ~ 'bior6.8': Biorthogonal小波
'rbio1.1' ~ 'rbio6.8': Reverse Biorthogonal小波
'meyr' : Meyer小波
'dmey': Discrete Meyer小波
python中:
print(pywt.wavelist(kind='discrete')),打印出DWT类型:
['bior1.1', 'bior1.3', 'bior1.5', 'bior2.2', 'bior2.4', 'bior2.6', 'bior2.8', 'bior3.1', 'bior3.3', 'bior3.5', 'bior3.7', 'bior3.9', 'bior4.4', 'bior5.5', 'bior6.8', 'coif1', 'coif2', 'coif3', 'coif4', 'coif5', 'coif6', 'coif7', 'coif8', 'coif9', 'coif10', 'coif11', 'coif12', 'coif13', 'coif14', 'coif15', 'coif16', 'coif17', 'db1', 'db2', 'db3', 'db4', 'db5', 'db6', 'db7', 'db8', 'db9', 'db10', 'db11', 'db12', 'db13', 'db14', 'db15', 'db16', 'db17', 'db18', 'db19', 'db20', 'db21', 'db22', 'db23', 'db24', 'db25', 'db26', 'db27', 'db28', 'db29', 'db30', 'db31', 'db32', 'db33', 'db34', 'db35', 'db36', 'db37', 'db38', 'dmey', 'haar', 'rbio1.1', 'rbio1.3', 'rbio1.5', 'rbio2.2', 'rbio2.4', 'rbio2.6', 'rbio2.8', 'rbio3.1', 'rbio3.3', 'rbio3.5', 'rbio3.7', 'rbio3.9', 'rbio4.4', 'rbio5.5', 'rbio6.8', 'sym2', 'sym3', 'sym4', 'sym5', 'sym6', 'sym7', 'sym8', 'sym9', 'sym10', 'sym11', 'sym12', 'sym13', 'sym14', 'sym15', 'sym16', 'sym17', 'sym18', 'sym19', 'sym20']
def full_Sparse(input, level, type='haar', height=0, width=0, threshold=0.8, DWT = True, slices=0):
print(pywt.wavelist(kind='discrete'))
shift_num = 0
if DWT == True:
input = (input-shift_num)
N = height*width
if type != "dct":
coeffs2 = pywt.wavedec2(input, type, mode='periodization', level=level) # 小波变换
img_fre_full, slices = pywt.coeffs_to_array(coeffs2) # 拼接成原图大小
else:
img_fre_full = cv2.dct(input.astype(dtype=np.float32))
if threshold < 1:
img_fre = np.reshape(img_fre_full, (height*width), "F")
sort_cof = np.sort(np.abs(img_fre), axis=0)
threshold = int(np.ceil(threshold * N))
thresh_val = sort_cof[N-threshold]
img_fre_full[np.abs(img_fre_full) <= thresh_val] = 0.0
return img_fre_full, slices
else: ## IDWT
if type != "dct":
coeffs_n = pywt.array_to_coeffs(input, slices, output_format='wavedec2') # 将图片重新变成coeffs
temp_im = pywt.waverec2(coeffs_n, type, mode='periodization') # 反变换到像素域
else:
temp_im = cv2.idct(input)
temp_im = temp_im + shift_num
return temp_im