Mercurial > ift6266
annotate transformations/slant.py @ 72:af2f9252dd14
forgot to import sys in testmod
author | Arnaud Bergeron <abergeron@gmail.com> |
---|---|
date | Wed, 10 Feb 2010 11:34:32 -0500 |
parents | 0f1337994716 |
children |
rev | line source |
---|---|
34
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
1 #!/usr/bin/python |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
2 # coding: utf-8 |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
3 |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
4 ''' |
35
e05715e4554d
first commit, the module has been tested with testmod.py, it has no timing
youssouf
parents:
34
diff
changeset
|
5 Author: Youssouf |
e05715e4554d
first commit, the module has been tested with testmod.py, it has no timing
youssouf
parents:
34
diff
changeset
|
6 |
34
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
7 this module add a slant effect to the image. |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
8 |
40
0f1337994716
modified the test function in slant.py in order to generate multiple transformation sample
youssouf
parents:
35
diff
changeset
|
9 To obtain the slant effect, each row of the array is shifted proportionately by a step controlled by the complexity. |
34
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
10 |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
11 ''' |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
12 |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
13 import numpy |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
14 |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
15 |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
16 class Slant(): |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
17 def __init__(self, complexity=1): |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
18 #---------- private attributes |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
19 self.direction = 1 |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
20 self.angle = 0 |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
21 |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
22 #---------- generation parameters |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
23 self.regenerate_parameters(complexity) |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
24 #------------------------------------------------ |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
25 |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
26 def _get_current_parameters(self): |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
27 return [self.angle, self.direction] |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
28 |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
29 def get_settings_names(self): |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
30 return ['angle', 'direction'] |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
31 |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
32 def regenerate_parameters(self, complexity): |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
33 self.angle = numpy.random.uniform(0.0, complexity) |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
34 P = numpy.random.uniform() |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
35 self.direction = 1; |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
36 if P < 0.5: |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
37 self.direction = -1; |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
38 return self._get_current_parameters() |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
39 |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
40 |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
41 def transform_image(self,image): |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
42 if self.angle == 0: |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
43 return image |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
44 |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
45 ysize, xsize = image.shape |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
46 slant = self.direction*self.angle |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
47 |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
48 output = image.copy() |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
49 |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
50 # shift all the rows |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
51 for i in range(ysize): |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
52 line = image[i] |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
53 delta = round((i*slant)) % xsize |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
54 line1 = line[:xsize-delta] |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
55 line2 = line[xsize-delta:xsize] |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
56 |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
57 output[i][delta:xsize] = line1 |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
58 output[i][0:delta] = line2 |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
59 |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
60 |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
61 #correction to center the image |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
62 correction = (self.direction)*round(self.angle*ysize/2) |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
63 correction = (xsize - correction) % xsize |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
64 |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
65 # center the region |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
66 line1 = output[0:ysize,0:xsize-correction].copy() |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
67 line2 = output[0:ysize,xsize-correction:xsize].copy() |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
68 output[0:ysize,correction:xsize] = line1 |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
69 output[0:ysize,0:correction] = line2 |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
70 |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
71 |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
72 return output |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
73 |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
74 |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
75 # Test function |
40
0f1337994716
modified the test function in slant.py in order to generate multiple transformation sample
youssouf
parents:
35
diff
changeset
|
76 # Load an image in local and create several samples of the effect on the |
0f1337994716
modified the test function in slant.py in order to generate multiple transformation sample
youssouf
parents:
35
diff
changeset
|
77 # original image with different parameter. All the samples are saved in a single image, the 1st image being the original. |
0f1337994716
modified the test function in slant.py in order to generate multiple transformation sample
youssouf
parents:
35
diff
changeset
|
78 |
34
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
79 def test_slant(): |
40
0f1337994716
modified the test function in slant.py in order to generate multiple transformation sample
youssouf
parents:
35
diff
changeset
|
80 import scipy |
0f1337994716
modified the test function in slant.py in order to generate multiple transformation sample
youssouf
parents:
35
diff
changeset
|
81 img_name = "test_img/mnist_0.png" |
0f1337994716
modified the test function in slant.py in order to generate multiple transformation sample
youssouf
parents:
35
diff
changeset
|
82 dest_img_name = "test_img/slanted.png" |
0f1337994716
modified the test function in slant.py in order to generate multiple transformation sample
youssouf
parents:
35
diff
changeset
|
83 nb_samples = 10 |
0f1337994716
modified the test function in slant.py in order to generate multiple transformation sample
youssouf
parents:
35
diff
changeset
|
84 im = Image.open(img_name) |
34
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
85 im = im.convert("L") |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
86 image = numpy.asarray(im) |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
87 |
40
0f1337994716
modified the test function in slant.py in order to generate multiple transformation sample
youssouf
parents:
35
diff
changeset
|
88 image_final = image |
0f1337994716
modified the test function in slant.py in order to generate multiple transformation sample
youssouf
parents:
35
diff
changeset
|
89 slant = Slant() |
0f1337994716
modified the test function in slant.py in order to generate multiple transformation sample
youssouf
parents:
35
diff
changeset
|
90 for i in range(nb_samples): |
0f1337994716
modified the test function in slant.py in order to generate multiple transformation sample
youssouf
parents:
35
diff
changeset
|
91 slant.regenerate_parameters(1) |
0f1337994716
modified the test function in slant.py in order to generate multiple transformation sample
youssouf
parents:
35
diff
changeset
|
92 image_slant = slant.transform_image(image) |
0f1337994716
modified the test function in slant.py in order to generate multiple transformation sample
youssouf
parents:
35
diff
changeset
|
93 image_final = scipy.hstack((image_final,image_slant)) |
34
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
94 |
40
0f1337994716
modified the test function in slant.py in order to generate multiple transformation sample
youssouf
parents:
35
diff
changeset
|
95 im = Image.fromarray(image_final.astype('uint8'), "L") |
34
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
96 im.save(dest_img_name) |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
97 |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
98 # Test |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
99 if __name__ == '__main__': |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
100 import sys, os, fnmatch |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
101 import Image |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
102 |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
103 test_slant() |
e61a46996db6
first commit, the module has been tested with testmod.py, it has no timing
ychherawala@smets-w04
parents:
diff
changeset
|
104 |