DWT

 1 import ia636 as ia
 2 import numpy as np
 3 
 4 def wave1d (f,freq=0,inv=0,coeff=0):
 5 
 6     r,c = f.shape
 7     g = np.zeros((r,c+8))
 8 
 9     g[:, 4:c+4] = f
10     g[:,-4:] = f[:,-2:-6:-1]
11     g[:, :4] = f[:,4:0:-1]
12 
13     #coeficientes do wikipedia: http://en.wikipedia.org/wiki/Cohen-Daubechies-Feauveau_wavelet
14 
15     if coeff==0: #CDF 5/3
16         if inv==0: #Transformada
17             if freq==0: #LOW
18                 s = np.array([0, 0, -1.0/8.0, 2.0/8.0, 6.0/8.0])
19             else:       #HIGH
20                 s = np.array([0, 0, 0, -1.0/2.0, 1.0])
21         else: #Inversa
22             if freq==0: #LOW
23                 s = np.array([0, 0, 0, 1.0/2.0, 1.0])
24             else:       #HIGH
25                 s = np.array([0, 0, -1.0/8.0, -2.0/8.0, 6.0/8.0])
26 
27     else: #CDF 9/7
28         if inv==0: #Transformada
29             if freq==0: #LOW
30                 s = np.array([0.02674875741080976,-0.01686411844287495,-0.07822326652898785,0.2668641184428723,0.6029490182363579])
31             else:       #HIGH
32                 s = np.array([0.0                , 0.09127176311424948,-0.05754352622849957,-0.5912717631142470,1.115087052456994])
33         else: #Inversa
34             if freq==0: #LOW
35                 s = np.array([0.0                ,-0.09127176311424948,-0.05754352622849957,0.5912717631142470,1.115087052456994])
36             else:       #HIGH
37                 s = np.array([0.02674875741080976, 0.01686411844287495,-0.07822326652898785,-0.2668641184428723,0.6029490182363579])
38 
39     h = np.empty((r,c))
40     h =  s[0] * g[:,0:-8] + \
41          s[1] * g[:,1:-7] + \
42          s[2] * g[:,2:-6] + \
43          s[3] * g[:,3:-5] + \
44          s[4] * g[:,4:-4] + \
45          s[3] * g[:,5:-3] + \
46          s[2] * g[:,6:-2] + \
47          s[1] * g[:,7:-1] + \
48          s[0] * g[:,8:]
49     return h[:,:c]
50 
51 def wave2d (f,coeff=0, it=0):
52 
53     r,c = f.shape
54 
55     h = np.empty(f.shape)
56     h[:,:c/2] = wave1d(f,0,0,coeff)[:,1::2]
57     h[:,c/2:] = wave1d(f,1,0,coeff)[:,::2]
58 
59     g = np.empty(f.shape)
60     g[:r/2,:] = np.transpose(wave1d(np.transpose(h),0,0,coeff)[:,1::2])
61     g[r/2:,:] = np.transpose(wave1d(np.transpose(h),1,0,coeff)[:,::2])
62 
63 
64     if it==0:
65         return g
66     else:
67         g[:c/2,:r/2] = wave2d(g[:c/2,:r/2],coeff, it-1)
68         return g
69 
70 def itwave1d (f,coeff=0):
71 
72     r,c = f.shape
73     rec = np.empty((c,r))
74 
75     recl = np.zeros((c,r))
76     recl[:,1::2] = np.transpose(f)[:,:r/2]
77     recl = wave1d(recl,0,1,coeff)
78 
79     rech = np.zeros((c,r))
80     rech[:,::2] = np.transpose(f)[:,r/2:]
81     rech = wave1d(rech,1,1,coeff)
82 
83     rec = recl + rech
84 
85     return rec
86 
87 def iwave2d (f,coeff=0,it=0):
88     r,c = f.shape
89 
90     if it>0:
91         f[:c/2,:r/2] = iwave2d(f[:c/2,:r/2],coeff,it-1)
92 
93     rect = np.empty((c,r))
94     rect = itwave1d(f,coeff)
95 
96     rec = np.empty((r,c))
97     rec = itwave1d(rect,coeff)
98 
99     return rec

Wavelet CDF5/3

 1 import ia636 as ia
 2 import numpy as np
 3 from project_franz_tomaz_1 import wave2d
 4 from project_franz_tomaz_1 import iwave2d
 5 
 6 f = adreadgray('cameraman.tif')
 7 f = f[:-1,:-1]
 8 
 9 print f.shape
10 
11 adshow(ia.ianormalize(f),'Input Image')
12 
13 g = wave2d(f,0,2)
14 adshow(ia.ianormalize(g),'Transformada wavelet CDF 5/3')
15 
16 rec = iwave2d(g,0,2)
17 adshow(ia.ianormalize(rec),'Imagem reconstruida CDF 5/3')
18 
19 print 'Maximo diferenca CDF 5/3', np.abs(f-rec).max()
(255, 255)
Maximo diferenca CDF 5/3 0.0

Input Image

Transformada wavelet CDF 5/3

Imagem reconstruida CDF 5/3

Wavelet CDF9/7

1 print f.shape
2 
3 g = wave2d(f,1,2)
4 adshow(ia.ianormalize(g),'Transformada wavelet CDF 9/7')
5 
6 rec = iwave2d(g,1,2)
7 adshow(ia.ianormalize(rec),'Imagem reconstruida CDF 9/7')
8 
9 print 'Maximo diferenca CDF 9/7', np.abs(f-rec).max()
(255, 255)
Maximo diferenca CDF 9/7 1.65698565979e-11

Transformada wavelet CDF 9/7

Imagem reconstruida CDF 9/7