Pas encore de compte ?
Transformée discrète en ondelettes à plusieurs niveaux
Testé sous Anaconda et Python 3.7
import numpy as np import pywt from matplotlib import pyplot as plt from pywt._doc_utils import wavedec2_keys, draw_2d_wp_basis x = pywt.data.camera().astype(np.float32) shape = x.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(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() plt.show()
Testé sous Anaconda et Python 3.7
import numpy as np import matplotlib.pyplot as plt import pywt import tensorflow as tf import pylab import cv2 def showimage(myimage, figsize=[10,10]): if (myimage.ndim>2): #This only applies to RGB or RGBA images (e.g. not to Black and White images) myimage = myimage[:,:,::-1] #OpenCV follows BGR order, while matplotlib likely follows RGB order fig, ax = plt.subplots(figsize=figsize) ax.imshow(myimage, cmap = 'gray', interpolation = 'bicubic') plt.xticks([]), plt.yticks([]) # to hide tick values on X and Y axis plt.show() pylab.rcParams['figure.figsize'] = (10.0, 10.0) def dwt2d(x, wave='haar'): # shape x: (b, h, w, c) nc = int(x.shape.dims[3]) w = pywt.Wavelet(wave) ll = np.outer(w.dec_lo, w.dec_lo) lh = np.outer(w.dec_hi, w.dec_lo) hl = np.outer(w.dec_lo, w.dec_hi) hh = np.outer(w.dec_hi, w.dec_hi) core = np.zeros((np.shape(ll)[0], np.shape(ll)[1], 1, 4)) core[:, :, 0, 0] = ll[::-1, ::-1] core[:, :, 0, 1] = lh[::-1, ::-1] core[:, :, 0, 2] = hl[::-1, ::-1] core[:, :, 0, 3] = hh[::-1, ::-1] core = core.astype(np.float32) kernel = np.array([core], dtype=np.float32) kernel = tf.convert_to_tensor(kernel) p = 2 * (len(w.dec_lo) // 2 - 1) with tf.variable_scope('dwt2d'): # padding odd length x = tf.pad(x, tf.constant([[0, 0], [p, p+1], [p, p+1], [0, 0]])) xh = tf.shape(x)[1] - tf.shape(x)[1]%2 xw = tf.shape(x)[2] - tf.shape(x)[2]%2 x = x[:, 0:xh, 0:xw, :] # convert to 3d data x3d = tf.expand_dims(x, 1) x3d = tf.split(x3d, int(x3d.shape.dims[4]), 4) x3d = tf.concat([a for a in x3d], 1) y3d = tf.nn.conv3d(x3d, kernel, padding='VALID', strides=[1, 1, 2, 2, 1]) y = tf.split(y3d, int(y3d.shape.dims[1]), 1) y = tf.concat([a for a in y], 4) y = tf.reshape(y, (tf.shape(y)[0], tf.shape(y)[2], tf.shape(y)[3], 4*nc)) channels = tf.split(y, nc, 3) outputs = [] for channel in channels: (cA, cH, cV, cD) = tf.split(channel, 4, 3) AH = tf.concat([cA, cH], axis=2) VD = tf.concat([cV, cD], axis=2) outputs.append(tf.concat([AH, VD], axis=1)) pass outputs = tf.concat(outputs, axis=-1) pass return outputs def wavedec2d(x, level=1, wave='haar'): if level == 0: return x y = dwt2d(x, wave=wave) hcA = tf.floordiv(tf.shape(y)[1], 2) wcA = tf.floordiv(tf.shape(y)[2], 2) cA = y[:, 0:hcA, 0:wcA, :] cA = wavedec2d(cA, level=level-1, wave=wave) cA = cA[:, 0:hcA, 0:wcA, :] hcA = tf.shape(cA)[1] wcA = tf.shape(cA)[2] cH = y[:, 0:hcA, wcA:, :] cV = y[:, hcA:, 0:wcA, :] cD = y[:, hcA:, wcA:, :] AH = tf.concat([cA, cH], axis=2) VD = tf.concat([cV, cD], axis=2) outputs = tf.concat([AH, VD], axis=1) return outputs def idwt2d(x, wave='haar'): # shape x: (b, h, w, c) nc = int(x.shape.dims[3]) w = pywt.Wavelet(wave) ll = np.outer(w.dec_lo, w.dec_lo) lh = np.outer(w.dec_hi, w.dec_lo) hl = np.outer(w.dec_lo, w.dec_hi) hh = np.outer(w.dec_hi, w.dec_hi) core = np.zeros((np.shape(ll)[0], np.shape(ll)[1], 1, 4)) core[:, :, 0, 0] = ll[::-1, ::-1] core[:, :, 0, 1] = lh[::-1, ::-1] core[:, :, 0, 2] = hl[::-1, ::-1] core[:, :, 0, 3] = hh[::-1, ::-1] core = core.astype(np.float32) kernel = np.array([core], dtype=np.float32) kernel = tf.convert_to_tensor(kernel) s = 2 * (len(w.dec_lo) // 2 - 1) with tf.variable_scope('idwt2d'): hcA = tf.floordiv(tf.shape(x)[1], 2) wcA = tf.floordiv(tf.shape(x)[2], 2) y = [] for c in range(nc): channel = x[:, :, :, c] channel = tf.expand_dims(channel, -1) cA = channel[:, 0:hcA, 0:wcA, :] cH = channel[:, 0:hcA, wcA:, :] cV = channel[:, hcA:, 0:wcA, :] cD = channel[:, hcA:, wcA:, :] temp = tf.concat([cA, cH, cV, cD], axis=-1) y.append(temp) pass # nc * 4 y = tf.concat(y, axis=-1) y3d = tf.expand_dims(y, 1) y3d = tf.split(y3d, nc, 4) y3d = tf.concat([a for a in y3d], 1) output_shape = [tf.shape(y)[0], tf.shape(y3d)[1], \ 2*(tf.shape(y)[1]-1)+np.shape(ll)[0], \ 2*(tf.shape(y)[2]-1)+np.shape(ll)[1], 1] x3d = tf.nn.conv3d_transpose(y3d, kernel, output_shape=output_shape, padding='VALID', strides=[1, 1, 2, 2, 1]) outputs = tf.split(x3d, nc, 1) outputs = tf.concat([x for x in outputs], 4) outputs = tf.reshape(outputs, (tf.shape(outputs)[0], tf.shape(outputs)[2], tf.shape(outputs)[3], nc)) outputs = outputs[:, s:2*(tf.shape(y)[1]-1)+np.shape(ll)[0]-s, \ s:2*(tf.shape(y)[2]-1)+np.shape(ll)[1]-s, :] pass return outputs tf.reset_default_graph() inputs = tf.placeholder(tf.float32, [None, None, None, 3], name='inputs') image = cv2.imread('pexels-ali-pazani-2878373.jpg') showimage(image) x = np.array([image, image[:, ::-1, :]]) dec = wavedec2d(inputs, level=5, wave='sym4') dwt = dwt2d(inputs, wave='sym4') idwt = idwt2d(dwt, wave='sym4') with tf.Session() as sess: sess.run(tf.global_variables_initializer()) result = sess.run(dec, feed_dict={inputs:x}) showimage(np.array(result[0], dtype=np.uint8)) trans = sess.run(dwt, feed_dict={inputs:x}) showimage(np.array(trans[0], dtype=np.uint8)) recons = sess.run(idwt, feed_dict={inputs:x}) showimage(np.array(recons[0], dtype=np.uint8)) pass
Discrete-Wavelet-Transform-2D - GitHub
Image originale
dec
dwt
idwt
Bienvenu, je m’appelle Eric Soupet et je suis l'administrateur du site elodees.com. elodees.com est un état de l'art de l'Intelligence Artificielle et se veut collaboratif, vous pouvez dès à présent proposer du contenu tels que des articles, des événements, des tutoriels, ... alors n'hésitez pas !
Crédit des images de la plate-forme : Pixabay - Pixabay License | Pexels - Pexels License