Debian Codegraph @

demo/dwt2_dwtn_image.py

raw symbols SCIP
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 import numpy as np
5 import matplotlib.pyplot as plt
6
7 import pywt
8 import pywt.data
9
10
11 # Load image
12 original = pywt.data.camera()
13
14 # Wavelet transform of image, and plot approximation and details
15 titles = ['Approximation', ' Horizontal detail',
16 'Vertical detail', 'Diagonal detail']
17 coeffs2 = pywt.dwt2(original, 'bior1.3')
18 LL, (LH, HL, HH) = coeffs2
19 fig = plt.figure()
20 for i, a in enumerate([LL, LH, HL, HH]):
21 ax = fig.add_subplot(2, 2, i + 1)
22 ax.imshow(a, interpolation="nearest", cmap=plt.cm.gray)
23 ax.set_title(titles[i], fontsize=12)
24 ax.set_xticks([])
25 ax.set_yticks([])
26
27 fig.suptitle("dwt2 coefficients", fontsize=14)
28
29 # Now reconstruct and plot the original image
30 reconstructed = pywt.idwt2(coeffs2, 'bior1.3')
31 fig = plt.figure()
32 plt.imshow(reconstructed, interpolation="nearest", cmap=plt.cm.gray)
33
34 # Check that reconstructed image is close to the original
35 np.testing.assert_allclose(original, reconstructed, atol=1e-13, rtol=1e-13)
36
37
38 # Now do the same with dwtn/idwtn, to show the difference in their signatures
39
40 coeffsn = pywt.dwtn(original, 'bior1.3')
41 fig = plt.figure()
42 for i, key in enumerate(['aa', 'ad', 'da', 'dd']):
43 ax = fig.add_subplot(2, 2, i + 1)
44 ax.imshow(coeffsn[key], interpolation="nearest", cmap=plt.cm.gray)
45 ax.set_title(titles[i], fontsize=12)
46 ax.set_xticks([])
47 ax.set_yticks([])
48
49 fig.suptitle("dwtn coefficients", fontsize=14)
50
51 # Now reconstruct and plot the original image
52 reconstructed = pywt.idwtn(coeffsn, 'bior1.3')
53 fig = plt.figure()
54 plt.imshow(reconstructed, interpolation="nearest", cmap=plt.cm.gray)
55
56 # Check that reconstructed image is close to the original
57 np.testing.assert_allclose(original, reconstructed, atol=1e-13, rtol=1e-13)
58
59
60 plt.show()