comparison transformations/thick.py @ 11:dbc806d025a2

Added a thick.py script defining a Thick class transforming randomly the thickness of the characters
author Xavier Glorot <glorotxa@iro.umontreal.ca>
date Wed, 27 Jan 2010 19:14:37 -0500
parents
children a25474d4d34f
comparison
equal deleted inserted replaced
10:faacc76d21c2 11:dbc806d025a2
1 #!/usr/bin/python
2 # coding: utf-8
3
4 '''
5 Simple implementation of random thickness deformation using morphological
6 operation of scipy.
7 Only one morphological operation applied (dilation or erosion), the kernel is random
8 out of a list of 11 symmetric kernels.
9
10 Author: Xavier Glorot
11
12 Usage:
13 '''
14
15 import scipy.ndimage.morphology
16 import numpy as N
17
18
19 class Thick():
20 def __init__(self,complexity = 1):
21 #---------- private attributes
22 self.__nx__ = 32
23 self.__ny__ = 32
24 self.__erodemax__ = 4
25 self.__dilatemax__ = 11
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]]),\
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]]),\
30 N.asarray([[1,1,1,1],[1,1,1,1],[1,1,1,1]]),\
31 N.asarray([[1,1,1],[1,1,1],[1,1,1],[1,1,1]]),\
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]])]
34 #------------------------------------------------
35
36 #---------- generation parameters
37 self.erodenb = N.ceil(complexity * self.__erodemax__)
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 #------------------------------------------------
44
45 def _get_current_parameters(self):
46 return [self.erodenb, self.dilatenb, self.Perode, self.Pdilate]
47
48 def get_settings_names(self):
49 return ['erodenb','dilatenb','Perode','Pdilate']
50
51 def regenerate_parameters(self, complexity):
52 self.erodenb = N.ceil(complexity * self.__erodemax__)
53 self.dilatenb = N.ceil(complexity * self.__dilatemax__)
54 self.Perode = self.erodenb / (self.dilatenb + self.erodenb + 1.0)
55 self.Pdilate = self.dilatenb / (self.dilatenb + self.erodenb + 1.0)
56 assert (self.Perode + self.Pdilate <= 1) & (self.Perode + self.Pdilate >= 0)
57 assert (complexity >= 0) & (complexity <= 1)
58 return self._get_current_parameters()
59
60 def transform_1_image(self,image,genparam_save = None):
61 P = N.random.uniform()
62
63 if P>1-(self.Pdilate+self.Perode):
64 maxi = float(N.max(image))
65 mini = float(N.min(image))
66
67 if maxi>1.0:
68 image=image/maxi
69
70 if P>1-(self.Pdilate+self.Perode)+self.Perode:
71 nb=N.random.randint(self.dilatenb)
72 trans=scipy.ndimage.morphology.grey_dilation\
73 (image,size=self.__structuring_elements__[nb].shape,structure=self.__structuring_elements__[nb])
74 meth = 'dilate'
75 else:
76 nb=N.random.randint(self.erodenb)
77 trans=scipy.ndimage.morphology.grey_erosion\
78 (image,size=self.__structuring_elements__[nb].shape,structure=self.__structuring_elements__[nb])
79 meth = 'erode'
80
81 #------renormalizing
82 maxit = N.max(trans)
83 minit = N.min(trans)
84 trans= numpy.asarray((trans - (minit+mini)) / (maxit - (minit+mini)) * maxi,dtype=image.dtype)
85 #--------
86 if genparam_save is not None:
87 genparam_save.update({'Thick':{'meth':meth,'nb':nb}})
88 return trans
89 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
95
96 def transform_image(self,image,genparam_save = None):
97 if image.ndim == 2:
98 newimage = N.reshape(image,(image.shape[0],self.__nx__,self.__ny__))
99 for i in range(image.shape[0]):
100 if genparam_save is not None:
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)
105 else:
106 newimage = N.reshape(image,(self.__nx__,self.__ny__))
107 if genparam_save is not None:
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)
112
113
114
115
116 #test on NIST (you need pylearn and access to NIST to do that)
117
118 if __name__ == '__main__':
119
120 from pylearn.io import filetensor as ft
121 import copy, numpy
122 import pygame
123 import time
124 datapath = '/data/lisa/data/nist/by_class/'
125 f = open(datapath+'digits/digits_train_data.ft')
126 d = ft.read(f)
127
128 pygame.surfarray.use_arraytype('numpy')
129
130 pygame.display.init()
131 screen = pygame.display.set_mode((8*2*32,8*32),0,8)
132 anglcolorpalette=[(x,x,x) for x in xrange(0,256)]
133 screen.set_palette(anglcolorpalette)
134
135 MyThick = Thick()
136
137 #debut=time.time()
138 #MyThick.transform_image(d)
139 #fin=time.time()
140 #print '------------------------------------------------'
141 #print d.shape[0],' images transformed in :', fin-debut, ' seconds'
142 #print '------------------------------------------------'
143 #print (fin-debut)/d.shape[0]*1000000,' microseconds per image'
144 #print '------------------------------------------------'
145 #print MyThick.get_settings_names()
146 #print MyThick._get_current_parameters()
147 #print MyThick.regenerate_parameters(0)
148 #print MyThick.regenerate_parameters(0.5)
149 #print MyThick.regenerate_parameters(1)
150 for i in range(10000):
151 a=d[i,:]
152 b=N.asarray(N.reshape(a,(32,32))).T
153
154 new=pygame.surfarray.make_surface(b)
155 new=pygame.transform.scale2x(new)
156 new=pygame.transform.scale2x(new)
157 new=pygame.transform.scale2x(new)
158 new.set_palette(anglcolorpalette)
159 screen.blit(new,(0,0))
160
161 dd={}
162 c=MyThick.transform_image(a,dd)
163 b=N.asarray(N.reshape(c,(32,32))).T
164
165 new=pygame.surfarray.make_surface(b)
166 new=pygame.transform.scale2x(new)
167 new=pygame.transform.scale2x(new)
168 new=pygame.transform.scale2x(new)
169 new.set_palette(anglcolorpalette)
170 screen.blit(new,(8*32,0))
171
172 pygame.display.update()
173 print dd
174 raw_input('Press Enter')
175
176 pygame.display.quit()