def mul_wavelet_dec(imArray): shape = imArray.shape fig, axes = plt.subplots(2, 4, figsize=[14, 8]) max_lev = 3 # how many levels of decomposition to draw label_levels = 3 # how many levels to explicitly label on the plots for level in range(0, max_lev + 1): if level == 0: # show the original image before decomposition axes[0, 0].set_axis_off() axes[1, 0].imshow(imArray, cmap=plt.cm.gray) axes[1, 0].set_title('Image') axes[1, 0].set_axis_off() continue # plot subband boundaries of a standard DWT basis draw_2d_wp_basis(shape, wavedec2_keys(level), ax=axes[0, level], label_levels=label_levels) axes[0, level].set_title('{} level\ndecomposition'.format(level)) # compute the 2D DWT c = pywt.wavedec2(imArray, 'haar', mode='periodization', level=level) # normalize each coefficient array independently for better visibility c[0] /= np.abs(c[0]).max() for detail_level in range(level): c[detail_level + 1] = [d / np.abs(d).max() for d in c[detail_level + 1]] # show the normalized coefficients arr, slices = pywt.coeffs_to_array(c) axes[1, level].imshow(arr, cmap=plt.cm.gray) axes[1, level].set_title('Coefficients\n({} level)'.format(level)) axes[1, level].set_axis_off()
def dw_show(img): """ Code taken from https://pywavelets.readthedocs.io/ to show what wavelet decomposition does. :param img: :return: """ shape = img.shape max_lev = 3 # how many levels of decomposition to draw label_levels = 3 # how many levels to explicitly label on the plots fig, axes = plt.subplots(2, 4, figsize=[14, 8]) for level in range(0, max_lev + 1): if level == 0: # show the original image before decomposition axes[0, 0].set_axis_off() axes[1, 0].imshow(img, cmap=plt.cm.gray) axes[1, 0].set_title('Image') axes[1, 0].set_axis_off() continue # plot subband boundaries of a standard DWT basis draw_2d_wp_basis(shape, wavedec2_keys(level), ax=axes[0, level], label_levels=label_levels) axes[0, level].set_title('{} level\ndecomposition'.format(level)) # compute the 2D DWT c = pywt.wavedec2(img, 'db2', mode='periodization', level=level) print(level, np.array(c[0]).shape) # normalize each coefficient array independently for better visibility c[0] /= np.abs(c[0]).max() for detail_level in range(level): c[detail_level + 1] = [d / np.abs(d).max() for d in c[detail_level + 1]] # show the normalized coefficients arr, slices = pywt.coeffs_to_array(c) axes[1, level].imshow(arr, cmap=plt.cm.gray) axes[1, level].set_title('Coefficients\n({} level)'.format(level)) axes[1, level].set_axis_off() plt.tight_layout() plt.show()
x = pywt.data.camera() shape = x.shape max_lev = 2 label_levels = 2 fig, axes = plt.subplots(2, 3, figsize=[14, 8]) for level in range(0, max_lev + 1): if level == 0: axes[0, 0].set_axis_off() axes[1, 0].imshow(x, cmap=plt.cm.gray) axes[1, 0].set_title('Image') axes[1, 0].set_axis_off() continue draw_2d_wp_basis(shape, wavedec2_keys(level), ax=axes[0, level], label_levels=label_levels) axes[0, level].set_title('{} level\ndecomposition'.format(level)) c = pywt.wavedec2(x, 'db2', mode='periodization', level=level) c[0] /= np.abs(c[0]).max() for detail_level in range(level): c[detail_level + 1] = [d / np.abs(d).max() for d in c[detail_level + 1]] arr, slices = pywt.coeffs_to_array(c) axes[1, level].imshow(arr, cmap=plt.cm.gray) axes[1, level].set_title('Coefficients\n({} level)'.format(level)) axes[1, level].set_axis_off() plt.tight_layout() plt.show()
max_lev = 3 # how many levels of decomposition to draw label_levels = 3 # how many levels to explicitly label on the plots fig, axes = plt.subplots(2, 4, figsize=[14, 8]) for level in range(0, max_lev + 1): if level == 0: # show the original image before decomposition axes[0, 0].set_axis_off() axes[1, 0].imshow(x, cmap=plt.cm.gray) axes[1, 0].set_title('Image') axes[1, 0].set_axis_off() continue # plot subband boundaries of a standard DWT basis draw_2d_wp_basis(shape, wavedec2_keys(level), ax=axes[0, level], label_levels=label_levels) axes[0, level].set_title('{} level\ndecomposition'.format(level)) # compute the 2D DWT c = pywt.wavedec2(x, 'db2', mode='periodization', level=level) # normalize each coefficient array independently for better visibility c[0] /= np.abs(c[0]).max() for detail_level in range(level): c[detail_level + 1] = [d/np.abs(d).max() for d in c[detail_level + 1]] # show the normalized coefficients arr, slices = pywt.coeffs_to_array(c) axes[1, level].imshow(arr, cmap=plt.cm.gray) axes[1, level].set_title('Coefficients\n({} level)'.format(level)) axes[1, level].set_axis_off() plt.tight_layout()
draw_2d_fswavedecn_basis) shape = (512, 512) max_lev = 4 # how many levels of decomposition to draw label_levels = 2 # how many levels to explicitly label on the plots if False: fig, axes = plt.subplots(1, 4, figsize=[16, 4]) axes = axes.ravel() else: fig, axes = plt.subplots(2, 2, figsize=[8, 8]) axes = axes.ravel() # plot a 5-level standard DWT basis draw_2d_wp_basis(shape, wavedec2_keys(max_lev), ax=axes[0], label_levels=label_levels) axes[0].set_title('wavedec2 ({} level)'.format(max_lev)) # plot for the fully separable case draw_2d_fswavedecn_basis(shape, max_lev, ax=axes[1], label_levels=label_levels) axes[1].set_title('fswavedecn ({} level)'.format(max_lev)) # get all keys corresponding to a full wavelet packet decomposition wp_keys = list(product(['a', 'd', 'h', 'v'], repeat=max_lev)) draw_2d_wp_basis(shape, wp_keys, ax=axes[2]) axes[2].set_title('wavelet packet\n(full: {} level)'.format(max_lev)) # plot an example of a custom wavelet packet basis keys = ['aaaa', 'aaad', 'aaah', 'aaav', 'aad', 'aah', 'aava', 'aavd', 'aavh', 'aavv', 'ad', 'ah', 'ava', 'avd', 'avh', 'avv', 'd', 'h', 'vaa', 'vad', 'vah', 'vav', 'vd', 'vh', 'vv']
shape = (512, 512) max_lev = 4 # how many levels of decomposition to draw label_levels = 2 # how many levels to explicitly label on the plots if False: fig, axes = plt.subplots(1, 4, figsize=[16, 4]) axes = axes.ravel() else: fig, axes = plt.subplots(2, 2, figsize=[8, 8]) axes = axes.ravel() # plot a 5-level standard DWT basis draw_2d_wp_basis(shape, wavedec2_keys(max_lev), ax=axes[0], label_levels=label_levels) axes[0].set_title('wavedec2 ({} level)'.format(max_lev)) # plot for the fully separable case draw_2d_fswavedecn_basis(shape, max_lev, ax=axes[1], label_levels=label_levels) axes[1].set_title('fswavedecn ({} level)'.format(max_lev)) # get all keys corresponding to a full wavelet packet decomposition wp_keys = list(product(['a', 'd', 'h', 'v'], repeat=max_lev)) draw_2d_wp_basis(shape, wp_keys, ax=axes[2]) axes[2].set_title('wavelet packet\n(full: {} level)'.format(max_lev)) # plot an example of a custom wavelet packet basis keys = [ 'aaaa', 'aaad', 'aaah', 'aaav', 'aad', 'aah', 'aava', 'aavd', 'aavh',