]> AND Private Git Repository - HindawiJournalOfChaos.git/blob - IH/iihmsp13/exp/dwt_marquage.py
Logo AND Algorithmique Numérique Distribuée

Private GIT Repository
abstract, conclusion
[HindawiJournalOfChaos.git] / IH / iihmsp13 / exp / dwt_marquage.py
1 #-*-coding:utf-8-*-
2 import Image as im
3 import pywt
4 from numpy import *
5
6
7 class Marquage_DWT:
8
9     _matrices_DWT = {'A1' : None,
10                      'H1' : None, 'D1' : None, 'V1' : None,
11                      'A2' : None,
12                      'H2' : None, 'D2' : None, 'V2' : None,
13                      'A3' : None,
14                      'H3' : None, 'D3' : None, 'V3' : None
15                      }
16
17     def __init__(self, hote, famille = 'db1'):
18         '''
19         Constructeur
20         '''
21         self._hote = im.open(hote)
22
23         print "--------",self._hote.mode
24         self._hote_marque = im.new(self._hote.mode,
25                                    self._hote.size)
26
27         self._hote_marque.putdata(self._hote.getdata())
28
29         self._matrice_hote = array(self._hote.getdata()).\
30                                             reshape(self._hote.size)
31
32         self._famille = famille
33
34         self._get_matrices()
35
36
37
38     def _get_matrices(self):
39
40         A1,(H1,V1,D1) = pywt.wavedec2\
41                         (self._matrice_hote,
42                         self._famille,
43                         level = 1)
44
45         self._matrices_DWT['A1'] = A1
46         self._matrices_DWT['H1'] = H1
47         self._matrices_DWT['V1'] = V1
48         self._matrices_DWT['D1'] = D1
49
50         A2,(H2,V2,D2),(_,_,_) = pywt.wavedec2\
51                         (self._matrice_hote,
52                         self._famille,
53                          level = 2)
54
55         self._matrices_DWT['A2'] = A2
56         self._matrices_DWT['H2'] = H2
57         self._matrices_DWT['V2'] = V2
58         self._matrices_DWT['D2'] = D2
59
60         A3,(H3,V3,D3), (_,_,_), (_,_,_) = pywt.wavedec2\
61                         (self._matrice_hote,
62                         self._famille,
63                         level = 3)
64
65         self._matrices_DWT['A3'] = A3
66         self._matrices_DWT['H3'] = H3
67         self._matrices_DWT['V3'] = V3
68         self._matrices_DWT['D3'] = D3
69
70
71
72     def get_matrice_DWT(self, matrice):
73         assert matrice in self._matrices_DWT.keys()
74         return self._matrices_DWT[matrice]
75
76
77     def set_matrice_DWT(self, matrice, valeur):
78         assert matrice in self._matrices_DWT.keys()
79         self._matrices_DWT[matrice] = valeur
80         self._reconstruit_image(matrice)
81
82
83     def _reconstruit_image(self, matrice_modifiee):
84         if '1' in matrice_modifiee:
85             self._hote_marque.putdata(pywt.waverec2((\
86                         self._matrices_DWT['A1'],
87                         (self._matrices_DWT['H1'],
88                          self._matrices_DWT['V1'],
89                          self._matrices_DWT['D1'])),
90                         self._famille).flatten())
91         elif '2' in matrice_modifiee:
92             print pywt.waverec2((\
93                         self._matrices_DWT['A2'],
94                         (self._matrices_DWT['H2'],
95                          self._matrices_DWT['V2'],
96                          self._matrices_DWT['D2']),
97                         (self._matrices_DWT['H1'],
98                          self._matrices_DWT['V1'],
99                          self._matrices_DWT['D1'])),
100                         self._famille).flatten()
101             self._hote_marque.putdata(pywt.waverec2((\
102                         self._matrices_DWT['A2'],
103                         (self._matrices_DWT['H2'],
104                          self._matrices_DWT['V2'],
105                          self._matrices_DWT['D2']),
106                         (self._matrices_DWT['H1'],
107                          self._matrices_DWT['V1'],
108                          self._matrices_DWT['D1'])),
109                         self._famille).flatten())
110         elif '3' in matrice_modifiee:
111             self._hote_marque.putdata(pywt.waverec2((\
112                         self._matrices_DWT['A3'],
113                         (self._matrices_DWT['H3'],
114                          self._matrices_DWT['V3'],
115                          self._matrices_DWT['D3']),
116                         (self._matrices_DWT['H2'],
117                          self._matrices_DWT['V2'],
118                          self._matrices_DWT['D2']),
119                         (self._matrices_DWT['H1'],
120                          self._matrices_DWT['V1'],
121                          self._matrices_DWT['D1'])),
122                         self._famille).flatten())
123
124
125     def get_hote_marque(self):
126         return self._hote_marque
127
128
129 if __name__ == '__main__':
130     print "Mise à zéro des matrices d'approximation"
131     for k in ['A1','A2','A3']:
132         print "  Matrice "+k
133         unMarquage = Marquage_DWT(hote = 'lena.png',
134                        famille = 'db1')
135         matrice_nulle = zeros(unMarquage.get_matrice_DWT(k).shape)
136         unMarquage.set_matrice_DWT(k,matrice_nulle)
137         unMarquage.get_hote_marque().show()
138         raw_input('   (Appuyez sur une touche)')
139
140     print
141     print "Mise à zéro des matrices de détail D"
142     for k in ['D1','D2','D3']:
143         print "  Matrice "+k
144         unMarquage = Marquage_DWT(hote = 'lena.png',
145                        famille = 'db1')
146         matrice_nulle = zeros(unMarquage.get_matrice_DWT(k).shape)
147         unMarquage.set_matrice_DWT(k,matrice_nulle)
148         unMarquage.get_hote_marque().show()
149         raw_input('   (Appuyez sur une touche)')