comparison transformations/thick.py @ 13:a25474d4d34f

Changed parameter generation in order to fit with the new specificities
author Xavier Glorot <glorotxa@iro.umontreal.ca>
date Thu, 28 Jan 2010 11:28:46 -0500
parents dbc806d025a2
children ebf61603489b
comparison
equal deleted inserted replaced
12:d511445f19da 13:a25474d4d34f
32 N.asarray([[0,0,1,0,0],[0,1,1,1,0],[1,1,1,1,1],[0,1,1,1,0],[0,0,1,0,0]]),\ 32 N.asarray([[0,0,1,0,0],[0,1,1,1,0],[1,1,1,1,1],[0,1,1,1,0],[0,0,1,0,0]]),\
33 N.asarray([[1,1,1,1],[1,1,1,1]]),N.asarray([[1,1],[1,1],[1,1],[1,1]])] 33 N.asarray([[1,1,1,1],[1,1,1,1]]),N.asarray([[1,1],[1,1],[1,1],[1,1]])]
34 #------------------------------------------------ 34 #------------------------------------------------
35 35
36 #---------- generation parameters 36 #---------- generation parameters
37 self.erodenb = N.ceil(complexity * self.__erodemax__) 37 self.regenerate_parameters(complexity)
38 self.dilatenb = N.ceil(complexity * self.__dilatemax__)
39 self.Perode = self.erodenb / (self.dilatenb + self.erodenb + 1.0)
40 self.Pdilate = self.dilatenb / (self.dilatenb + self.erodenb + 1.0)
41 assert (self.Perode + self.Pdilate <= 1) & (self.Perode + self.Pdilate >= 0)
42 assert (complexity >= 0) & (complexity <= 1)
43 #------------------------------------------------ 38 #------------------------------------------------
44 39
45 def _get_current_parameters(self): 40 def _get_current_parameters(self):
46 return [self.erodenb, self.dilatenb, self.Perode, self.Pdilate] 41 return [self.meth,self.nb]
47 42
48 def get_settings_names(self): 43 def get_settings_names(self):
49 return ['erodenb','dilatenb','Perode','Pdilate'] 44 return ['meth','nb']
50 45
51 def regenerate_parameters(self, complexity): 46 def regenerate_parameters(self, complexity):
52 self.erodenb = N.ceil(complexity * self.__erodemax__) 47 self.erodenb = N.ceil(complexity * self.__erodemax__)
53 self.dilatenb = N.ceil(complexity * self.__dilatemax__) 48 self.dilatenb = N.ceil(complexity * self.__dilatemax__)
54 self.Perode = self.erodenb / (self.dilatenb + self.erodenb + 1.0) 49 self.Perode = self.erodenb / (self.dilatenb + self.erodenb + 1.0)
55 self.Pdilate = self.dilatenb / (self.dilatenb + self.erodenb + 1.0) 50 self.Pdilate = self.dilatenb / (self.dilatenb + self.erodenb + 1.0)
56 assert (self.Perode + self.Pdilate <= 1) & (self.Perode + self.Pdilate >= 0) 51 assert (self.Perode + self.Pdilate <= 1) & (self.Perode + self.Pdilate >= 0)
57 assert (complexity >= 0) & (complexity <= 1) 52 assert (complexity >= 0) & (complexity <= 1)
53 P = N.random.uniform()
54 if P>1-(self.Pdilate+self.Perode):
55 if P>1-(self.Pdilate+self.Perode)+self.Perode:
56 self.meth = 1
57 self.nb=N.random.randint(self.dilatenb)
58 else:
59 self.meth = -1
60 self.nb=N.random.randint(self.erodenb)
61 else:
62 self.meth = 0
63 self.nb = -1
58 return self._get_current_parameters() 64 return self._get_current_parameters()
59 65
60 def transform_1_image(self,image,genparam_save = None): 66 def transform_1_image(self,image):
61 P = N.random.uniform() 67 if self.meth!=0:
62
63 if P>1-(self.Pdilate+self.Perode):
64 maxi = float(N.max(image)) 68 maxi = float(N.max(image))
65 mini = float(N.min(image)) 69 mini = float(N.min(image))
66 70
67 if maxi>1.0: 71 if maxi>1.0:
68 image=image/maxi 72 image=image/maxi
69 73
70 if P>1-(self.Pdilate+self.Perode)+self.Perode: 74 if self.meth==1:
71 nb=N.random.randint(self.dilatenb)
72 trans=scipy.ndimage.morphology.grey_dilation\ 75 trans=scipy.ndimage.morphology.grey_dilation\
73 (image,size=self.__structuring_elements__[nb].shape,structure=self.__structuring_elements__[nb]) 76 (image,size=self.__structuring_elements__[self.nb].shape,structure=self.__structuring_elements__[self.nb])
74 meth = 'dilate'
75 else: 77 else:
76 nb=N.random.randint(self.erodenb)
77 trans=scipy.ndimage.morphology.grey_erosion\ 78 trans=scipy.ndimage.morphology.grey_erosion\
78 (image,size=self.__structuring_elements__[nb].shape,structure=self.__structuring_elements__[nb]) 79 (image,size=self.__structuring_elements__[self.nb].shape,structure=self.__structuring_elements__[self.nb])
79 meth = 'erode'
80 80
81 #------renormalizing 81 #------renormalizing
82 maxit = N.max(trans) 82 maxit = N.max(trans)
83 minit = N.min(trans) 83 minit = N.min(trans)
84 trans= numpy.asarray((trans - (minit+mini)) / (maxit - (minit+mini)) * maxi,dtype=image.dtype) 84 trans= numpy.asarray((trans - (minit+mini)) / (maxit - (minit+mini)) * maxi,dtype=image.dtype)
85 #-------- 85 #--------
86 if genparam_save is not None:
87 genparam_save.update({'Thick':{'meth':meth,'nb':nb}})
88 return trans 86 return trans
89 else: 87 else:
90 meth = 'nothing'
91 nb = 0
92 if genparam_save is not None:
93 genparam_save.update({'Thick':{'meth':meth,'nb':nb}})
94 return image 88 return image
95 89
96 def transform_image(self,image,genparam_save = None): 90 def transform_image(self,image):
97 if image.ndim == 2: 91 if image.ndim == 2:
98 newimage = N.reshape(image,(image.shape[0],self.__nx__,self.__ny__)) 92 newimage = N.reshape(image,(image.shape[0],self.__nx__,self.__ny__))
99 for i in range(image.shape[0]): 93 for i in range(image.shape[0]):
100 if genparam_save is not None: 94 newimage[i,:,:] = self.transform_1_image(newimage[i,:,:])
101 newimage[i,:,:] = self.transform_1_image(newimage[i,:,:],genparam_save[i])
102 else:
103 newimage[i,:,:] = self.transform_1_image(newimage[i,:,:])
104 return N.reshape(newimage,image.shape) 95 return N.reshape(newimage,image.shape)
105 else: 96 else:
106 newimage = N.reshape(image,(self.__nx__,self.__ny__)) 97 newimage = N.reshape(image,(self.__nx__,self.__ny__))
107 if genparam_save is not None: 98 newimage = self.transform_1_image(newimage)
108 newimage = self.transform_1_image(newimage,genparam_save)
109 else:
110 newimage = self.transform_1_image(newimage)
111 return N.reshape(newimage,image.shape) 99 return N.reshape(newimage,image.shape)
112 100
113 101
114 102
115 103
132 anglcolorpalette=[(x,x,x) for x in xrange(0,256)] 120 anglcolorpalette=[(x,x,x) for x in xrange(0,256)]
133 screen.set_palette(anglcolorpalette) 121 screen.set_palette(anglcolorpalette)
134 122
135 MyThick = Thick() 123 MyThick = Thick()
136 124
137 #debut=time.time() 125 debut=time.time()
138 #MyThick.transform_image(d) 126 MyThick.transform_image(d)
139 #fin=time.time() 127 fin=time.time()
140 #print '------------------------------------------------' 128 print '------------------------------------------------'
141 #print d.shape[0],' images transformed in :', fin-debut, ' seconds' 129 print d.shape[0],' images transformed in :', fin-debut, ' seconds'
142 #print '------------------------------------------------' 130 print '------------------------------------------------'
143 #print (fin-debut)/d.shape[0]*1000000,' microseconds per image' 131 print (fin-debut)/d.shape[0]*1000000,' microseconds per image'
144 #print '------------------------------------------------' 132 print '------------------------------------------------'
145 #print MyThick.get_settings_names() 133 #print MyThick.get_settings_names()
146 #print MyThick._get_current_parameters() 134 #print MyThick._get_current_parameters()
147 #print MyThick.regenerate_parameters(0) 135 #print MyThick.regenerate_parameters(0)
148 #print MyThick.regenerate_parameters(0.5) 136 #print MyThick.regenerate_parameters(0.5)
149 #print MyThick.regenerate_parameters(1) 137 #print MyThick.regenerate_parameters(1)
156 new=pygame.transform.scale2x(new) 144 new=pygame.transform.scale2x(new)
157 new=pygame.transform.scale2x(new) 145 new=pygame.transform.scale2x(new)
158 new.set_palette(anglcolorpalette) 146 new.set_palette(anglcolorpalette)
159 screen.blit(new,(0,0)) 147 screen.blit(new,(0,0))
160 148
161 dd={} 149 print MyThick.get_settings_names(), MyThick.regenerate_parameters(1)
162 c=MyThick.transform_image(a,dd) 150 c=MyThick.transform_image(a)
163 b=N.asarray(N.reshape(c,(32,32))).T 151 b=N.asarray(N.reshape(c,(32,32))).T
164 152
165 new=pygame.surfarray.make_surface(b) 153 new=pygame.surfarray.make_surface(b)
166 new=pygame.transform.scale2x(new) 154 new=pygame.transform.scale2x(new)
167 new=pygame.transform.scale2x(new) 155 new=pygame.transform.scale2x(new)
168 new=pygame.transform.scale2x(new) 156 new=pygame.transform.scale2x(new)
169 new.set_palette(anglcolorpalette) 157 new.set_palette(anglcolorpalette)
170 screen.blit(new,(8*32,0)) 158 screen.blit(new,(8*32,0))
171 159
172 pygame.display.update() 160 pygame.display.update()
173 print dd
174 raw_input('Press Enter') 161 raw_input('Press Enter')
175 162
176 pygame.display.quit() 163 pygame.display.quit()