comparison transformations/affine_transform.py @ 51:81b9567ec4ae

transformation ready for pipeline
author Razvan Pascanu <r.pascanu@gmail.com>
date Thu, 04 Feb 2010 15:38:10 -0500
parents 17caecc92544
children d9d836d3c625
comparison
equal deleted inserted replaced
50:ff59670cd1f9 51:81b9567ec4ae
12 import numpy, Image 12 import numpy, Image
13 13
14 14
15 15
16 class AffineTransformation(): 16 class AffineTransformation():
17 def __init__( self, shape = (32,32), seed = None): 17 def __init__( self, complexity = .5):
18 self.shape = shape 18 self.shape = (32,32)
19 self.rng = numpy.random.RandomState(seed) 19 self.rng = numpy.random.RandomState()
20 self.complexity = complexity
21 params = self.rng.uniform(size=6) -.5
22 self.a = 1. + params[0]*.4*complexity
23 self.b = 0. + params[1]*.4*complexity
24 self.c = params[2]*8.*complexity
25 self.d = 0. + params[3]*.4*complexity
26 self.e = 1. + params[4]*.4*complexity
27 self.f = params[5]*8.*complexity
20 28
21 def transform(self,NIST_image):
22 29
23 im = Image.fromarray( \ 30 def _get_current_paramteres(self):
24 numpy.asarray(\ 31 return [self.a, self.b, self.c, self.d, self.e, self.g]
25 NIST_image.reshape(self.shape), dtype='uint8')) 32
33 def get_settings_names(self):
34 return ['a','b','c','d','e','f']
35
36 def regenerate_parameters(self, complexity):
26 # generate random affine transformation 37 # generate random affine transformation
27 # a point (x',y') of the new image corresponds to (x,y) of the old 38 # a point (x',y') of the new image corresponds to (x,y) of the old
28 # image where : 39 # image where :
29 # x' = params[0]*x + params[1]*y + params[2] 40 # x' = params[0]*x + params[1]*y + params[2]
30 # y' = params[3]*x + params[4]*y _ params[5] 41 # y' = params[3]*x + params[4]*y _ params[5]
31 42
32 # the ranges are set manually as to look acceptable 43 # the ranges are set manually as to look acceptable
33 params = self.rng.uniform(size = 6) -.5 44
34 params[2] *= 8. 45 self.complexity = complexity
35 params[5] *= 8. 46 params = self.rng.uniform(size=6) -.5
36 params[0] = 1. + params[0]*0.4 47 self.a = 1. + params[0]*.4*complexity
37 params[3] = 0. + params[3]*0.4 48 self.b = 0. + params[1]*.4*complexity
38 params[1] = 0 + params[1]*0.4 49 self.c = params[2]*8.*complexity
39 params[4] = 1 + params[4]*0.4 50 self.d = 0. + params[3]*.4*complexity
51 self.e = 1. + params[4]*.4*complexity
52 self.f = params[5]*8.*complexity
53 return self._get_current_parameters()
40 54
41 print params 55
42 nwim = im.transform( (32,32), Image.AFFINE, params) 56
57
58 def transform_image(self,NIST_image):
59
60 im = Image.fromarray( \
61 numpy.asarray(\
62 NIST_image.reshape(self.shape), dtype='uint8'))
63 nwim = im.transform( (32,32), Image.AFFINE, [self.a,self.b,self.c,self.d,self.e,self.f])
43 return numpy.asarray(nwim) 64 return numpy.asarray(nwim)
44 65
45 66
46 67
47 if __name__ =='__main__': 68 if __name__ =='__main__':
61 id = numpy.random.randint(30) 82 id = numpy.random.randint(30)
62 83
63 pylab.figure() 84 pylab.figure()
64 pylab.imshow(d[id].reshape((32,32))) 85 pylab.imshow(d[id].reshape((32,32)))
65 pylab.figure() 86 pylab.figure()
66 pylab.imshow(transformer.transform(d[id]).reshape((32,32))) 87 pylab.imshow(transformer.transform_image(d[id]).reshape((32,32)))
67 88
68 pylab.show() 89 pylab.show()
69 90