comparison transformations/thick.py @ 23:442789c94b27

Changed thick maximum erosion and dilation transformations
author Xavier Glorot <glorotxa@iro.umontreal.ca>
date Fri, 29 Jan 2010 11:44:12 -0500
parents db10ee2a07fb
children 47e7202d4f19
comparison
equal deleted inserted replaced
22:cb47cbc95a21 23:442789c94b27
19 class Thick(): 19 class Thick():
20 def __init__(self,complexity = 1): 20 def __init__(self,complexity = 1):
21 #---------- private attributes 21 #---------- private attributes
22 self.__nx__ = 32 #xdim of the images 22 self.__nx__ = 32 #xdim of the images
23 self.__ny__ = 32 #ydim of the images 23 self.__ny__ = 32 #ydim of the images
24 self.__erodemax__ = 4 #nb of index max of erode structuring elements 24 self.__erodemax__ = 9 #nb of index max of erode structuring elements
25 self.__dilatemax__ = 11 #nb of index max of dilation structuring elements 25 self.__dilatemax__ = 9 #nb of index max of dilation structuring elements
26 self.__structuring_elements__ = [N.asarray([[1,1]]),N.asarray([[1],[1]]),\ 26 self.__structuring_elements__ = [N.asarray([[1,1]]),N.asarray([[1],[1]]),\
27 N.asarray([[1,1],[1,1]]),N.asarray([[0,1,0],[1,1,1],[0,1,0]]),\ 27 N.asarray([[1,1],[1,1]]),N.asarray([[0,1,0],[1,1,1],[0,1,0]]),\
28 N.asarray([[1,1,1],[1,1,1]]),N.asarray([[1,1],[1,1],[1,1]]),\ 28 N.asarray([[1,1,1],[1,1,1]]),N.asarray([[1,1],[1,1],[1,1]]),\
29 N.asarray([[1,1,1],[1,1,1],[1,1,1]]),\ 29 N.asarray([[1,1,1],[1,1,1],[1,1,1]]),\
30 N.asarray([[1,1,1,1],[1,1,1,1],[1,1,1,1]]),\ 30 N.asarray([[1,1,1,1],[1,1,1,1],[1,1,1,1]]),\
45 45
46 def regenerate_parameters(self, complexity): 46 def regenerate_parameters(self, complexity):
47 self.erodenb = N.ceil(complexity * self.__erodemax__) 47 self.erodenb = N.ceil(complexity * self.__erodemax__)
48 self.dilatenb = N.ceil(complexity * self.__dilatemax__) 48 self.dilatenb = N.ceil(complexity * self.__dilatemax__)
49 self.Perode = self.erodenb / (self.dilatenb + self.erodenb + 1.0) 49 self.Perode = self.erodenb / (self.dilatenb + self.erodenb + 1.0)
50 self.Pdilate = self.dilatenb / (self.dilatenb + self.erodenb + 1.0) 50 self.Pdilate = self.dilatenb / (self.dilatenb + self.erodenb + 1.0)
51 assert (self.Perode + self.Pdilate <= 1) & (self.Perode + self.Pdilate >= 0) 51 assert (self.Perode + self.Pdilate <= 1) & (self.Perode + self.Pdilate >= 0)
52 assert (complexity >= 0) & (complexity <= 1) 52 assert (complexity >= 0) & (complexity <= 1)
53 P = N.random.uniform() 53 P = N.random.uniform()
54 if P>1-(self.Pdilate+self.Perode): 54 if P>1-(self.Pdilate+self.Perode):
55 if P>1-(self.Pdilate+self.Perode)+self.Perode: 55 if P>1-(self.Pdilate+self.Perode)+self.Perode:
122 d = ft.read(f) 122 d = ft.read(f)
123 123
124 pygame.surfarray.use_arraytype('numpy') 124 pygame.surfarray.use_arraytype('numpy')
125 125
126 pygame.display.init() 126 pygame.display.init()
127 screen = pygame.display.set_mode((8*2*32,8*32),0,8) 127 screen = pygame.display.set_mode((8*4*32,8*32),0,8)
128 anglcolorpalette=[(x,x,x) for x in xrange(0,256)] 128 anglcolorpalette=[(x,x,x) for x in xrange(0,256)]
129 screen.set_palette(anglcolorpalette) 129 screen.set_palette(anglcolorpalette)
130 130
131 MyThick = Thick() 131 MyThick = Thick()
132 132
133 debut=time.time() 133 #debut=time.time()
134 MyThick.transform_image(d) 134 #MyThick.transform_image(d)
135 fin=time.time() 135 #fin=time.time()
136 print '------------------------------------------------' 136 #print '------------------------------------------------'
137 print d.shape[0],' images transformed in :', fin-debut, ' seconds' 137 #print d.shape[0],' images transformed in :', fin-debut, ' seconds'
138 print '------------------------------------------------' 138 #print '------------------------------------------------'
139 print (fin-debut)/d.shape[0]*1000000,' microseconds per image' 139 #print (fin-debut)/d.shape[0]*1000000,' microseconds per image'
140 print '------------------------------------------------' 140 #print '------------------------------------------------'
141 #print MyThick.get_settings_names() 141 #print MyThick.get_settings_names()
142 #print MyThick._get_current_parameters() 142 #print MyThick._get_current_parameters()
143 #print MyThick.regenerate_parameters(0) 143 #print MyThick.regenerate_parameters(0)
144 #print MyThick.regenerate_parameters(0.5) 144 #print MyThick.regenerate_parameters(0.5)
145 #print MyThick.regenerate_parameters(1) 145 #print MyThick.regenerate_parameters(1)
152 new=pygame.transform.scale2x(new) 152 new=pygame.transform.scale2x(new)
153 new=pygame.transform.scale2x(new) 153 new=pygame.transform.scale2x(new)
154 new.set_palette(anglcolorpalette) 154 new.set_palette(anglcolorpalette)
155 screen.blit(new,(0,0)) 155 screen.blit(new,(0,0))
156 156
157 print MyThick.get_settings_names(), MyThick.regenerate_parameters(1) 157 #max dilation
158 MyThick.meth=1
159 MyThick.nb=MyThick.__dilatemax__
158 c=MyThick.transform_image(a) 160 c=MyThick.transform_image(a)
159 b=N.asarray(N.reshape(c,(32,32))).T 161 b=N.asarray(N.reshape(c,(32,32))).T
160 162
161 new=pygame.surfarray.make_surface(b) 163 new=pygame.surfarray.make_surface(b)
162 new=pygame.transform.scale2x(new) 164 new=pygame.transform.scale2x(new)
163 new=pygame.transform.scale2x(new) 165 new=pygame.transform.scale2x(new)
164 new=pygame.transform.scale2x(new) 166 new=pygame.transform.scale2x(new)
165 new.set_palette(anglcolorpalette) 167 new.set_palette(anglcolorpalette)
166 screen.blit(new,(8*32,0)) 168 screen.blit(new,(8*32,0))
167 169
170 #max erosion
171 MyThick.meth=-1
172 MyThick.nb=MyThick.__erodemax__
173 c=MyThick.transform_image(a)
174 b=N.asarray(N.reshape(c,(32,32))).T
175
176 new=pygame.surfarray.make_surface(b)
177 new=pygame.transform.scale2x(new)
178 new=pygame.transform.scale2x(new)
179 new=pygame.transform.scale2x(new)
180 new.set_palette(anglcolorpalette)
181 screen.blit(new,(8*2*32,0))
182
183 #random
184 print MyThick.get_settings_names(), MyThick.regenerate_parameters(1)
185 c=MyThick.transform_image(a)
186 b=N.asarray(N.reshape(c,(32,32))).T
187
188 new=pygame.surfarray.make_surface(b)
189 new=pygame.transform.scale2x(new)
190 new=pygame.transform.scale2x(new)
191 new=pygame.transform.scale2x(new)
192 new.set_palette(anglcolorpalette)
193 screen.blit(new,(8*3*32,0))
194
168 pygame.display.update() 195 pygame.display.update()
169 raw_input('Press Enter') 196 raw_input('Press Enter')
170 197
171 pygame.display.quit() 198 pygame.display.quit()