python 用二维对称Daubechies小波对图像变换demo程序

版权声明:本文为博主原创文章,请转载时注明出处,欢迎浏览! https://blog.csdn.net/leemboy/article/details/84669369

用Daubeches db2对称小波变化的lena图像

详细python代码如下,使用格式建代码说明部分

#this demo for 2D wavelets transform is developed by Xueshang Nai
#Referrence Link:https://pywavelets.readthedocs.io/en/latest/regression/wp2d.html
#can expanded to other type of wavelet.
#you can use in cmd line with: python wavelets_transfer.py --image image.jpg
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
import argparse
import imutils
import cv2
from datetime import datetime
import numpy as np
import pywt


def wavelete_packet2D(img_name):
    img = np.array(Image.open(img_name).convert('L'))
    rows, cols = img.shape
    plt.figure(img_name)
    plt.imshow(img, cmap='gray')
    #plt.axis('off')
    plt.show()# show the original image

    #use 2D wavelete db1 mode='symmetric'
    wp = pywt.WaveletPacket2D(data=img, wavelet='db2', mode='symmetric')

    #show a - LL, low-low coefficients
    plt.imshow((wp['a'].data), cmap='gray')
    plt.show()

    # show h - LH, low-high coefficients
    plt.imshow((wp['h'].data), cmap='gray')
    plt.show()

    # show v - HL, high-low coefficients
    plt.imshow((wp['v'].data), cmap='gray')
    plt.show()

    # show d - HH, high-high coefficients
    plt.imshow((wp['d'].data), cmap='gray')
    plt.show()

if __name__ == '__main__':
    ap = argparse.ArgumentParser()
    ap.add_argument("-i", "--image", required=True,
                    help="path to the input image")
    args = vars(ap.parse_args())
    #image = cv2.imread(args["image"])
    t1 = datetime.now()    
    #img_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    wavelete_packet2D(args["image"])
    print(datetime.now() - t1)

原图如下,由于原图有边框,所以变换后高频部分有明显变化

小波变换后四部分如下。详细描述见下面英文部分。

Because now we deal with a bit more complex structure (each node has four children), we have four basic path names based on the dwt 2D output convention to address the WP2D structure:

  • a - LL, low-low coefficients
  • h - LH, low-high coefficients
  • v - HL, high-low coefficients
  • d - HH, high-high coefficients

In other words, subnode naming corresponds to the dwt2() function output naming convention (as wavelet packet transform is based on the dwt2 transform):

                            -------------------
                            |        |        |
                            | cA(LL) | cH(LH) |
                            |        |        |
(cA, (cH, cV, cD))  <--->   -------------------
                            |        |        |
                            | cV(HL) | cD(HH) |
                            |        |        |
                            -------------------

   (fig.1: DWT 2D output and interpretation)

用其他小波的实验还在继续中

猜你喜欢

转载自blog.csdn.net/leemboy/article/details/84669369
今日推荐